CSE 142 Summer 2001

Homework #7

Due: Electronic turnin, Monday, August 13, 2001, 11:00 am.

 

Purpose

The purpose of this assignment is to gain experience working with two dimensional arrays and also get a feeling for how a modern, interactive program is structured.  While you will want to learn a bit about how a graphical user interface (GUI) is organized, the primary technical emphasis is on array processing.

We will evaluate your work both on functionality (does it work?) and quality (how well is the program written). This means doing your best in terms of commenting and using good names in your program.

The Game of Othello

Othello is played on a board on which white and black tiles may be placed. When the game starts, two white and two black pieces are placed in the center of the board.  You can see the initial configuration by downloading and running the starter code (see below).

Players alternate taking turns.  We explain here how white moves; the rules for black are the same, with the colors reversed of course.  A white piece may be placed on any empty square provided that, (1) it is adjacent to a square containing a black tile and, (2) continuing in the same direction, there is a sequence of one or more black tiles followed by a white tile.  A white tile is placed at such a location, all of the black tiles bracketed by the new tile and other white tiles that are already on the board become white (we can say that they are captured).  If there are sequences of black tiles followed by white in several directions, then all of the black pieces involved change to white.  If the mouse is clicked on a square that is not a possible move , that click is ignored.

In the official rules for Othello, if a player cannot place a tile, because there are no possible moves for tiles of that color, then the other player may move.  The game is over when neither player has a possible move.  For the purposes of this assignment, you do not need to detect blocked moves or the end of the game.  Continue to process alternate moves as long as legal moves are entered by clicking on a square on the board.

Click here to load an applet version of Othello, which you can use to check your understanding of the game rules.

UPDATE: If you would like to run your Othello game in a web browser (like the applet linked above), download these three files OthelloApplet.class, Square.class, and othello.html (I would recommend right-clicking the links and choosing 'save target as ... ' or you will follow the link) and insert them into your existing Othello game folder. It will ask you if it is okay to replace Square.class (you should already have one of these), but just say yes and you should be fine. Once the files are copied in the correct folder, all you should have to do is double-click the html file 'othello.html' in that folder and it should work.

You can also download file othello.jar to try the game without a web browser.  This is an executable jar file, which should run when you double click it if you have Java installed on your machine.

Requirements

You should download the starter code for the project:  Board.java, Othello.java, and Square.java.  Classes Othello and Square provide the graphical user interface for the program.  Do not modify these files.  You will not be able to turn in modified copies of them.  Board.java contains the actual data structure describing the state of the game, which is displayed by classes Othello and Square.

You should modify Board.java so it updates the board in response to mouse clicks according to the rules of Othello, and displays the current score (number of black and white pieces).  Here is a suggested order for doing this:

  1. Add code to keep track of the number of black and white pieces on the board and update the current scores, which are stored in instance variables numBlack and numWhite.  A brute force way to do this would be to look through the board array and count the number of tiles of each type, but maybe you could improve on this by updating the current score as tiles are placed or removed.  You can test your work by using the starter code, which alternately places black and white tiles on the board as the mouse is clicked.  Your new code should correctly keep track of the number of tiles as the contents of the board changes.
  2. Provide an implementation for canMove that returns true only if a tile of the current color can be placed on the selected square according to the rules of Othello (adjacent tile(s) of the other color followed by a piece with the current color, etc.).  Modify function processClick so it ignores clicks on squares that would not be allowed as moves in Othello.
  3. Provide an implementation for makeMove that moves a tile of the current color to the given row and column, then flips the tiles captured by that move.  Use this to get Othello working by calling it whenever canMove for a selected square returns true.  (In the implementation of makeMove, you may assume that canMove returns true for that location on the board, as long as you're sure that you only call it when this is the case.)
  4. You may add code to do things like detect the end of the game, or detect when the current player does not have a move but the other player does, but this is not required.

Notes/Hints

What to Turn In

Turn in your Board.java file using this turnin form.  The file you turn in will be compiled and linked with the original Othello and Square classes, which you should not have altered.

Reminder: You can turn in your program more than once.  We'll grade the last version.  Take advantage of this to turn in partial solutions once you've got significant parts of the project working.