![]() |
CSE 142 Summer 2001Homework #7Due: Electronic turnin, Monday, August 13, 2001, 11:00 am. |
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.
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.
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:
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.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.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.)Board
contains the central data structures
that hold the state of the game. Class Othello
is
responsible for handling user events (mouse clicks), and updating the
display after any changes. When a mouse click is detected, the processClick
method in class Board
is called to handle it. If the
click results in a change to the game state, method updateDisplay
in the Othello object should be called to redraw the screen to reflect the
new game state.canMove
and makeMove
needs to scan
and flip tiles in any of 8 possible directions (if you think of them as
compass directions they are North, NE, E, SE, S, SW, W, NW). It may be
easiest to start by hard-coding a search that works in one or two specific
directions, to get the idea of how to do it. But you should almost
certainly figure out how to parameterize the code and use loops or functions
so you don't have 8 big blocks of almost identical code, one for each
direction.canMoveInDirection(int
d, int row, int col)
, which returns true if there is a legal move in
the given direction starting at the given row and column. This is
useful in implementing both canMove
and makeMove
.
But it's just a suggestion.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.