Gettysburg College

CS 111
Computer Science I

Fall 2025

Assignment 4

Due: Thu, Sep 28, by 11:59pm

General Description

The goal of this assignment is to implement an interactive two-player computer game. In this game two players take turns removing coins from two distinct piles (rows). The player who makes the last move, i.e. clears the game board by removing the last coin(s), is considered the winner.

The focus of this assignment is on expressing repetition (while loops), decisions (if-statements), and complex conditions (&&,||).

Here are the rules of the game:

Throughout the game display messages to the players centered on the screen a quarter away from the top edge.

Preliminaries (New DrJava)

Download the new version of DrJava:

This DrJava version includes:

Preliminaries

Start DrJava and create a new project named CoinsGameApp in folder cs111/hw . See the Readings List notes on how to create a new project.

For this app the phone should be put in landscape/tablet orientation.

App Stages

Make sure to test each procedure carefully before moving on to the next one.

The assignment is structured as a sequence of stages starting with a simple version of the app and adding smaller components each time. Your submission should only include the final version (last stage).

Handling invalid moves, interacting with two players, and getting input by touching the circles will be added gradually in different stages.

Color choices are up to you, but the messages for one of the players should be in one color and for the other player in a different color.

Stage 1

For this stage assume that:

Create the following procedures:

Stage 2

This stage will make it a two-player game. This is a simple extension of Stage 1.

Add a Post-It to keep track of current player. This Post-It will alternate between two possible values (for example, 1, 2, 1, 2, 1, 2, ... or +1, -1, +1, -1, +1, -1).

Make any other relevant changes to display a message on the phone (either "BLUE Player's move" in bluish color or "RED Player's move" in reddish color), so that the players know whose turn it is.

The message should be centered horizontally and be down from the top edged a quarter of the height.

Since now there are two players, at the end of the game there should be a message that indicates who won.

Stage 3

This stage will make the game more realistic by selecting a random number of coins for each of the rows.

So far the number of coins has been fixed to 10 per row and the coin radius has been fixed to 36.

Update the app so that each row (independently) starts with a random number of coins chosen from [10,20].

Note: You can get a random integer in the range [low,high] from the canvas (write it down on a Post-It):

canvas.getRandomInt( <low>, <high> )

Since the number of coins varies each time the game is run we need to fix the radius of the coins. So far the radius has been set to 36, but this will no longer work if there are too many coins.

Write the following procedure so that the radius of the coins is no longer set to 32.8 (see Stage 1), but is computed based on the initial number of coins as the game begins.

Now use the above procedure inside playGame to set the correct coin radius.

Play the game again. Below are sample screenshots. You might see something different, since each time random number coins are picked.

Each time you start the game the goal is to see that the longer row extends all the way to the end (it is ok if the longer row has a small gap at the end).

Once computed, the radius does not change throughout the game.


playGame();    // happened to start with 11(top), 10(bottom) coins
               // coins are bigger, since started with 11 max

playGame();    // happened to start with 12(top), 14(bottom) coins
               // coins are smaller, since started with 14 max

Stage 4

This stage will improve the game interaction by allowing the player to select the coins to remove by touching the screen. For example:

Remove the canvas.readInt(...) sections and replace them with code that handles touch input. See these notes for an example on how to use touch input with the canvas. It shows how to get the coordinates of the touch, the number of taps, and what object is selected:

touch input

Write the following procedure to which computes the intended row:

Make sure you can play the game by tapping the coins on the screen.

Stage 5

This you will add error messages. If the latest move violates one of these rules an appropriate messages should be displayed (each case should have its own message):

Draw the message and pause briefly (the error message should be one eighth from the top edge of the screen, i.e. above the player move message) . Nothing else needs to be done -- the player simply forfeits the turn (board should not change).

This is just an extension of the code that updates the coins on the board with a few additional if-statements using && ||. Think about correct order/placement.


What to turn in

Turn in the Java code for the app in the Assignment 4 dropbox: