CSE 142 Homework 5



introduction

In this project, you will be building a 2-player board game using the GP142 package.

key concepts

In addition to concepts with which you should be comfortable by this point, this your chance to become familiar with the following:

rules of the game

The game is played on a rectangular board divided into small cells, numbered 1 to 50, arranged in a grid of 5 rows with 10 cells each.  The starter program includes a single player, represented by an orange circle. When the game starts, the player is off the board to the left of cell 1.  To make a move, roll the die by either clicking on "roll die" in the menu or by pressing the space bar on the keyboard.  When you do this, a number from 1 to 6 will appear on the die and the player advances that many cells forward on the board.  The goal is to reach the last cell at the top-right corner of the board.  

There are a couple of variations on the basic rules for making a move.  First, several arrows are placed randomly on the game board each time the game is restarted.  If a player lands on a cell containing an arrow, the player advances immediately to the adjacent cell that the arrow points to.  A player only gets one jump (i.e., can only follow an arrow to one adjacent cell) per turn, even if the destination cell itself contains another arrow.

Second, if the player is close to the final cell, it can only advance forward if moving the number of cells specified by the roll of the die will stay on the board  If the number on the die would move the player beyond the last cell (#50), the player doesn't move and the turn is over.  For example, if a player is on cell 48 and rolls a 4, then the player can't move.  If the player rolls a 1 or a 2 while at cell 48, then he/she can move forward to cell 49 or 50, respectively.

requirements 

Your job is to extend the game so it can be played by two players, detect when a player wins the game and display an appropriate message, and add additional arrows to the board.  You also should get the "restart" menu button to work.

Two players.  The first extension is to modify the game so that there are two players, who alternate taking turns rolling the die and moving.  The first player who reaches the final cell (#50) wins the game.  You should detect when one of the players wins and display a suitable message when this happens.  (You can get fancier with fireworks or other outbursts when someone wins, but an appropriate message will be enough.)  When one of the players wins, the game is over.  Nothing further happens until a new game is initiated by clicking on the "Restart" button in the menu (see below).

Additional arrows.  The starter version of the game randomly places 5 arrows on the board, all of them pointing forward (to the right).  You should extend this by placing 15 additional arrows on the board.  These new arrows should also be placed randomly, but they should point in different directions: 5 backwards (to the left), 5 up, and 5 down, in addition to the 5 forward arrows from the original game.  A player who lands on any of these arrows moves to the adjacent cell pointed to by the arrow.  This means that a player can not only move forward or backwards one cell, but can also move up a row (advancing 10 cells) or down (backing up 10 cells), depending on which way the arrow points.

You need to be careful to place the arrows sensibly.  First, an arrow cannot point off the board.  So no "down" arrows can be placed in the bottom row, no "up" arrows in the top row, and the first cell (#1) can't contain a left arrow.  Second, no cell can contain more than one arrow; a cell either contains one arrow, or none.  (There is no restriction on how many arrows may point to a given cell, of course.)  Finally, you cannot place an arrow in the last cell - otherwise it would not be possible to win the game

Restart menu button.  The menu in the upper corner of the screen contains a button labeled "Restart".  Right now it doesn't do anything.  Fix it so that clicking on it reinitializes the game and starts over.

implementation notes

A key decision in designing a program like this is to pick suitable data structures to represent the various objects in the game.  Another key decision is to pick the operations that should be packaged as functions in the program.  The starter program for this assignment has several plausible data structures and operations, along with useful constants, that you might want to use.  Many of these are just suggestions (i.e. feel free to do something different).  You do not have to use all of the code in the starter game - if you find constants, data structures, or operations that you do not need, ignore (or better yet, delete) them.  But you may find some of them useful either directly, or as an idea for something similar that you do need. 

You are required, however, to use at least one struct and at least one two-dimensional array in your program.  The starter program suggests a possible struct to represent information about an individual player in the game, including the player position, its color on board, and any other information you need to record about the player.  You should almost certainly use copies of this data structure for both players (possibly with some additions or modifications depending on the needs of the rest of your code).

There are many ways to represent the game.  The most natural is probably using a two-dimensional array to represent the cells.  The starter program does this, and we suggest you do the same.  The description of each cell needs to contain any information about the cell needed to play the game, in particular, whether it contains an arrow and, if it does, which direction it points.

player moves

The starter program already contains code to detect when the space bar is pressed or the menu command to roll the die is clicked.  If either of these happen, the die is rolled (generate a random number between 1 and 6 and display that number), then the player moves, and skips an additional cell at the end when it lands on an arrow.  You should build on this code to add an additional player and additional arrows, and detect when a player wins the game.

placing arrows

At first glance, the rules for placing the arrows seem quite complicated (no more than one arrow in a cell, no arrows pointing off the board, 5 arrows pointing in each direction, no arrows in the final cell, arrows placed randomly subject to the above constraints).  Implementing this might seem to require some very complicated conditional statements, tricky logic, or other problems.  There's actually a fairly easy way to avoid these complications:  Generate a random location for an arrow.  If that location is suitable, use it, otherwise try again. Repeat until all 20 arrows have been placed on the board.

how to get started

To get the program working with the least effort, you should start soon and work on it steadily.  Don't leave everything until the last minute.  Here is a suggested order in which to proceed.

  1. Build the starter program and see what it does (this is the same as the sample executable version of the program).  Look at the program and figure out how it works.  You might find it very useful to draw a static call graph, but you are not required to hand this in.
  2. Add a second player to the game.
  3. Add code to detect when a player wins the game.
  4. Add the additional arrows.  Be sure you turn in your program once you have this working.
  5. Add code for extra credit if you like (see below).

Don't leave this project until the last minute.  The labs are usually jammed towards the end of the quarter and there will be long lines if you wait until the day after Memorial Day.

extra credit

Once you have the basic game working and have turned in a working copy in case something goes wrong later, feel free to add to the game and submit an enhanced, working version.  A modest amount of extra credit will be awarded for interesting additions to the program.  Here are a few suggestions.

While you should be creative, the resulting game should be an embellished version of the original one, not something completely different.

Use your imagination and have fun.

submission guidelines

As always, be sure to read the homework submission guidelines, linked on the course home page in the Announcements section. You will have to submit your work (1) via the web using the page linked below and (2) on paper (a printout of the web turn-in receipt page).

files


announcements

If any clarifications or changes need to be made for this homework, they will be posted to the cse142-announce email list and linked here.


Return to main Homework page...
Last modified: Sun May 14th, 2000.