Prog 4: Color Match
Create the interface to allow playing the Color Match game. (This is based on the excellent Android game Coloroid written by Ben Lewis.)
Play is simple, and begins with the color of the square in the upper-left hand corner, with the goal being to change all squares on the board to the same color. On each move the user chooses a color, and all squares with that color join the captured area. The program keeps track of how many moves are made, comparing it to "par" for that level. If the user matches or beats par, play continues to the next level up. Screenshots of playing the game will look like what is shown below, where the render window with the colored blocks is shown on the left, and the color of the user input mouse clicked square is shown on the right. Note that in our version it doesn't matter which square is clicked.
Note that the Codio starter code project for Program 4 includes a version of the program that can be launched from the command line.
RenderWindow
User clicks on:
1. Red square
2. Cyan square
3. Blue square
4. Black square
5. Green square
6. Cyan square
7. Blue square
8. Yellow square
9. Red square
At the bottom of the screen you would now get a message, indicating you had made 9 moves on a par 8 board. Since the number of moves was greater than par, the game would not continue. If the user had finished the board within par number of moves, then the board would reset with n + 1 number of squares on each side, where n is the number of squares on each side in the previous level.
(Par 8) Move 1
(Par 8) Move 2
(Par 8) Move 3
(Par 8) Move 4
(Par 8) Move 5
(Par 8) Move 6
(Par 8) Move 7
(Par 8) Move 8
(Par 8) Move 9
(Par 8) You had 9 moves.
When a user succeeds in converting the board to the same color within par number of moves, then play continues at the next level, which in this case would be level 5, which might look like:
Similarly if the user succeeds at level 5, then a level 6 board would be given:
and so on. Play continues until the user succeeds at a 24 x 24 board (shown below).
You need to know the following concepts in order to write this program:
Everything from the first three programs; Using arrays; Use of random number generator; Using classes and objects; Sample SFML graphics code
Notes and Points:
To earn points, the following steps must be completed in order. For example, you only get the points for step 3 (message at bottom of the screen) if your program has completed steps 1 and 2.
I suggest you follow the outlined sections shown in the starter code that comes with the project in Codio.
(10 points) Generating the board:
Note in the supplied starter code that an array is created with the maximum possible number of Squares for the game. At each level of play you only use part of them.
Think about the code needed to generate a 4 x 4 array of Squares for level 4 of play, which is where the game begins. Your code will then need to go through the array and assign random colors. The size and position of the squares displayed in the graphics window is related to the level of play (4 in this case).
To get a random color in the process of creating the board you will need to use the random number generator. Valid predefined colors are described in the SFML Color documentation page, which indicates that the six valid colors are Black, Red, Green, Blue, Yellow, Cyan, (and White, Magenta, and Transparent which we will not use).
At this point the output of your program should look like the following:
(5 points) Once your code can generate and display boards of any size between 4 and 12 then you can think about making a move. User input is accomplished by clicking on any square. Initially have your program display the number of the square that was clicked upon, which should look like this:
Once you have found which square was selected, you can now use that information to retrieve the color of that square. Use the function I have supplied to you to convert the sf::Color to a std::string, which you can then use to print out at the bottom of the screen. Running your program at this point should display the color of the clicked-upon square and should look like:
(5 points) Now change the message at the bottom of your screen to indicate par and move number. Each click by the user should increment the displayed move number.
"Par" for each level is twice the level number. For level 4, par is 2 x 4 = 8. For level 5, par is 2 x 5 = 10, and so on up to level 24 with 2 x 24 = 48. In either this or the next step you should change your program so that square numbers are no longer displayed.
(25 points) Once the current clicked square color has been identified, all squares in the captured area of squares should be set to this selected color. Next all of the squares now touching the captured area squares with this user-selected color should be marked as part of the newly expanded captured area of squares. The process of doing this updating of the colors is where your creativity and problem-solving skills comes in. You could use some sort of "marking" process, where you repeatedly go through the squares in the array and "mark" ones you have looked at so far by storing some value in a field of the Square class, updating colors as you go. You could keep doing this process until a pass through the array results in no changes being made, at which point you would know you are done for this move. You may find it convenient to add a field to the Square class that indicates whether each square is part of the captured space, or perhaps have an array that keeps track of the neighbors of each square. (Working ahead, if you are feeling adventurous you could even write a recursive solution!) Think carefully about this part of the program!
There have been many questions on this aspect of the program. Slightly more formally, here is the process (added 11/5):
// First go through entire board and set all squares in the captured area to be the selected color
// ...
// Keep looping while more squares are added to the captured area
while( true) {
// Iterate through and extend the edges of the currently captured area by one square
int numberCaptured = 0; // count how many have been captured on this pass
// Step through each square on the board
for( int i=0; i<arraySize; i++) {
if square i is already captured {
for each valid neighbor of square i {
if the valid neighbor is not captured and is the desired color {
set this valid neighbor as now captured;
numberCaptured++;
}
}
}//end if square...
}//end for( int i...
// We know we're done when we've made a pass through the board and no new squares were captured
if( numberCaptured == 0) {
break; // break out of the enclosing infinite loop
}
}//end while( true)
(10 points) Each mouse click counts as a move and should increment the displayed move number. If you are not successful in finish a level in the number of moves equal to or less than par, then your program should reset and recreate the board with the same number of squares per side as before. If you do solve the board within par moves, then the number of squares per side on the board grows by one and the board is reset and recreated, up until the maximum of 24 per side.