Assignment 9: Flood It
Goals: Design a game with mutable world state, ArrayLists, mutable linked data structures, and loops.
import java.util.ArrayList; import tester.Tester; import javalib.impworld.*; import java.awt.Color; import javalib.worldimages.*;
You will submit this project in two parts. For Part 1 you must have completed the task of Creating the board which includes connecting cells and rendering the game.
For Part 2 you must complete all of the tasks, Rendering the game plus Gameplay and Resetting the game.
Part 1 Due: Thursday March 30th at 9:00pm
Part 2 Due: Thursday April 6th at 9:00pm
Setting the scene
In this game players are trying to manipulate a grid of colors so that all of the cells are the same color. The player starts in the upper left corner and by clicking various colors they can change the color of that corner which can increase the size of the area they control.
An example of this game is available for you to play here.
9.1 Tasks
Here is a list of tasks you will need to complete to implement this game.
9.1.1 Part1: Creating and rendering the board
// Represents a single square of the game area class Cell { // In logical coordinates, with the origin at the top-left corner of the screen int x; int y; Color color; boolean flooded; // the four adjacent cells to this one Cell left; Cell top; Cell right; Cell bottom; } class FloodItWorld extends World { // All the cells of the game ArrayList<Cell> board; }
You will need to create a two-dimensional grid of these Cells to represent the board.
Since the game should be easily configurable, you should have a constructor that takes in two numbers: the size of the board and the number of colors.
Because the size of your board is determined by this constructor, you cannot simply hard-code lists of data, because they may be of the wrong size. Instead, you’ll need to use loops and ArrayLists.
You will need to implement the makeScene method in the FloodItWorld class to be able to render the game state. Note that although makeScene returns a WorldScene in the impworld library, the placeImageXY method is now a void method. (The other event handlers, which you will need to use in Part 2, are also void methods now.)
For Part 1, as always, your code should be well-designed and well-tested.
9.1.2 Part 2: Gameplay and resetting the game
Player mechanics: The player should be able to click on each square to change the color. Players win if all of the squares are the same color before they run out of steps. You should make the game challenging enough so that it is not too easy to win but also not impossible.
It will be useful to think of the game as having two world states, one which allows you to click on a tile and begin flooding, and the other which is in the process of flooding. Here are some details you will need to implement:
When you are in the process of flooding your on-tick function should flood only the squares adjacent to those you have already flooded. This will produce the "waterfall" effect shown in the game.
If a player clicks on a square that is the same color as the area they control you should not flood the area, since it will produce no change.
A player is allotted a certain number of clicks in which they can flood the grid. If they fail to do so in the given number of clicks, they lose. When a player loses (or wins) you should let them know.
The maximum number of steps allowed should be proportional in some way to the size of the board and the number of colors so that the game is challenging enough.
Resetting the game: Use the ‘r’ key to reset the game and create a new board.
9.2 Extra credit
If you want to earn extra credit on this assignment, you can complete any number of enhancements.
Enhancements will only count towards extra credit if they are convincingly and thoroughly tested. Moreover, the rest of the game
must be at least as well tested —
Here are some examples:
Enhancing the graphics.
Keeping score: how many steps does the player take before reaching the goal? Lower scores are better... You’d need to enhance the display to render the score so far.
Keeping time: display a timer to keep track of the time since the beginning of the game.
You are welcome to think of other enhancements to the game. Have fun!