Minesweeper
Or if you prefer, download the attached file.
Start
It started as a small project.
Originally, I was going to just make a simple, plain old Minesweeper.
Internal State Data Structure
First things first, I had to think of a way to represent the state of the game.
I decided to use a list of lists of numbers to represent the grid.
It works like this:
- Assign states to numbers which are 2^n, where n can be any positive number (0-30 due to integer limit)
as long as no state has the same number. - To assign a state to a cell, you do
- To toggle (turn off if on, turn on if off) a state in a cell, you do
- To see if a cell has a specific state, you do
return bitwise_and(cell, state) != 0
- To remove a state from a cell, you do
if(bitwise_and(cell, state) != 0) then return bitwise_xor(cell, state) else return cell
By doing so, the number that represents the cell can have any combinations of the states you created. (this is guaranteed by Maths)
Now, since a cell represented like so can have multiple states, this is perfect for the game.
Dynamically Created Buttons
I wanted to allow people to customize the size of the grid.
As you know, you cannot create buttons in runtime, so after some thinking,
I realized that I have to use a canvas.
Of course, manually creating buttons is going to take a whole lot of blocks to do...
After creating countless procedures and 10 days working, it's finished.
By now, the game works like a charm.
I wish you can create buttons dynamically in AI2.
Theme
The game is supposed to be completed, but when I tested it on an emulator, the labels cannot be seen.
It is dark-themed, so it requires the change of color of the background of the screen to black.
However, in the emulator (very old version, I think it's API 6 or 7), you can't change it,
so the labels and the background are both white.
That causes me to add a new setting - theme.
The way I do so is
- Assign components that need to be changed to a list inThere is a separate list for each type of components.
- A procedure is created for each type of components.
- A procedure that uses the above procedures is created for each theme.
- A procedure is created that calls the theme-specific procedures.
- When the theme is changed, the above procedure is called.
It did take some time, as I had to manually create those lists.
At this point, I consider the game to be completed.
Leaderboard
I showcased the app to my friends.
One of them suggested that I should add a leaderboard. And so I did.
After thinking of a way to calculate a score, which is(((if (mines < max(grid - 2, grid / 2)) then mines else grid - mines) + 1) ^ 2) * grid / (time + 1)
The above algorithm prevents - games that have mines set to (grid - 2) or (grid - 1) from getting a high score (one click win)
- division by 0 when the time is 0 (if you just configure the settings just right, which is easy)
Alright, we are still missing a sort function for lists, and apparently, AI2 doesn't have one!
After looking up in Wikipedia, I decided to use merge sort - it isn't too hard to implement, and it's quite fast.
Still take quite a while though, especially when pulling blocks is slow compared to typing.
I wish AI2 has a sort function. It would save me all those troubles.
There is still one more thing though, and that is AI2 doesn't have a table (not TableArrangement) that works like ListView!
I decided to approach it by splitting each column with |.
I tested it and oh... it looks very ugly. The columns are not aligned and after some searching around,
there seems to be no way of aligning them without the use of extensions.
And I want to avoid extensions.
At last, I decided that this is okay - it still shows information clearly.
Localization
This is where the fun begins.
This is suggested by another friend.
I chose to make the localization extremely robust.
Well, this comes at the cost of increased complexity, and I do mean it.
It takes ~500 blocks per screen to make the localization system.
Errors are hard to debug, and the block editor is extremely slow, which means it took a lot of time.
After using 12 hours, it's finally done.
Well, I wish AI2 has built-in localization.
Now
I don't think I will be developing this app for some time now after creating the time-consuming localization system,
but if you have any suggestions, you can reply to me.
Feel free to copy or understand any part of my code. Maybe that localization system is useful to you?
That's all and happy playing!
Typing this is also time-consuming...