Write code for the flip tile game; here is how the game is played :
Basically there are 2 images…
- To begin with, one of the 2 images are randomly displayed in each cell of the grid.
- The user clicks on a cell to toggle between the 2 images.
- Game comes to an end when all the cells in the grid show the same image.
Watch the video to recall how the game is played.
You will need a 2D array board that stores either a 1 or 2 to let us know what image is being currently shown in a cell.
Inside setup :
- Randomly populate board with either 1 or 2.
- Do the visual representation of what is inside board (1 means show your first image on that tile in the canvas & 2 means show your second image on that tile in the canvas )
Inside mousePressed:
-
Find clickedRow & clickedCol - both of that put together gives the user selected tile.
-
Check what is being currently displayed in the user selected tile ( check the content of board at location determined by step 1).
-
If it is showing image1(ie. board has a 1 in that position) then
-
change the board content at that position to 2
-
write code to reflect this change visually by showing image2 in that cell on the canvas
-
else // means that tile is showing image2.
-
change the board content at that position to 1
-
write code to reflect this change visually by showing image1 in that cell on the canvas
-
If game is over,
-
close the game
Game over condition :
-
Find how many tiles display image1 - giving 2 ways to do this.
-
Maintain a variable that keeps count of # of image1 being displayed. (involves a couple of steps in mousePressed - I have not included that.)
-
OR Count the number of 1 occurs inside the 2D array board (involves searching in the 2D array board )
-
If the number determined in step 1 is same as number of tiles in the whole canvas (ie. all tiles are showing image1) OR that count is 0 (ie. all tiles are showing image2) then
-
game has come to an end.
-
else
-
game needs to continue.
I’m really struggling with this homework problem and would greatly appreciate the help.