On this page:
Setting the scene
9.1 Tasks
9.1.1 Creating the board
9.1.2 Gameplay
9.1.3 Resetting the game
9.2 Extra credit
7.7

Assignment 9: Flood It

Goals: Design a game with mutable world state, ArrayLists, mutable linked data structures, and loops.

You will be using the Impworld library, as in Lab 9 make sure that at the top of your file, you include
import java.util.ArrayList;
import tester.*;
import javalib.impworld.*;
import java.awt.Color;
import javalib.worldimages.*;

You will submit this project twice. 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 31st at 9:00pm

Part 2 Due: Tuesday April 12th 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. 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. Some helpful hints are given below:
  • 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.

9.1 Tasks

Here is a list of tasks you will need to complete to implement this game.

9.1.1 Creating the board

Your primary data structures may look like the following (you are free to add to or remove from this):
// 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 size of the game should be easily configurable, you should make your code dependent on a single constant, which can be changed easily to resize your board. To define constants in Java, you may use the following syntax:

We have not discussed the keyword static in this course, but you will learn more about it in OOD.

class FloodItWorld extends World {
// Defines an int constant static int BOARD_SIZE = 22;
...
}
You can then refer to your constant as FloodItWorld.BOARD_SIZE from anywhere in your program.

Because the size of your board is determined by this constant, 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.

9.1.2 Gameplay

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.

9.1.3 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 — you will not receive extra credit if the required parts of the game are not designed properly or do not work properly.

Here are some examples:

You are welcome to think of other enhancements to the game. Have fun!