CS 025 Computer Science Fundamentals I
Assignment 2

Department of Computer Science
University of Western Ontario
Given: September 23, 2007
Due: October 7, 2007

This assignment is out of 60 points.

Paper copies of your work must be handed in to the CS 025 locker (using the assignment submission form) and electronic copies should be submited via the usual Computer Science Department electronic assignment submission system.

Paper copies of your work must be handed in to the CS 1025 locker and electronic copies should be submited via the usual Computer Science Department electronic assignment submission system. Detailed instructions on how to submit an assignment are located on the How to Submit Assignments page.

Please see the course outline for information on late penalties and the rules of ethical conduct.


Introduction

Reversi, also known as Othello, is a two-player board game in which the players place markers on an 8x8 board with the goal of owning the most squares at the end of the game.

We will call the two players White and Black. Each marker has a White side and a Black side. A player's turn consists of placing a marker on an empty square with their own colour face up. Suppose it is Black's turn to move. Black may place a marker black-side up on any open square so long as it causes one or more "flips". A flip occurs when a line of pieces of one colour have end pieces of the opposite color. So Black can place a piece if it causes at least on straight line of pieces (along a row, column or diagonal) to be: Black, some number of White, and finally Black.

The details of how the game is played are nicely described on the Wikipedia Reversi page.

For this question you will do the following:

Question 1. Board representation (10 points)

Create a class to represent a Reversi position. The class should represent the board as some sort of array or array of arrays. The number of rows and columns should be given by final private static int variables. You should have three final public static int values White, Black and Empty to describe the contents of a square.

Your ReversiPosition class must support at least the following methods:

Question 2. Position output (10 points)

Add a method to output a Reversi position on a PrintStream given as an argument. The output should look like:

[. . . . . . . ./
 . . . . . . . ./
 . . . . . . . ./
 . . . X X . . ./
 . . . O O O . ./
 . . . . . . . ./
 . . . . . . . ./
 . . . . . . . .]

That is, the board is given inside a pair of square brackets. Rows are separted by slashes, newlines and spaces to line up as shown. Empty squares are given as periods, Black squares given as X and White squares as O. There should be a space between squares horizontally.

Question 3. Position Input (20 points)

Write a method that creates a Reversi position by reading from an InputStream. Input is the same format as the output, except that the spaces and newlines are optional. The above position could be given as it is shown above or (for example) as:

[......../......../......../...XX.../...OOO../......../......../........]

You should do error checking to make sure that the board has the right number of rows and right number of columns in each row.

Question 4. Move Generation (15 points)

Write another class called ReversiMoveGenerator that has a method to calculate the possible moves from a given position. It should be declared as:

You should write a couple of helper methods that solve the following sub-problems:

Question 5. Move Selection (5 points)

Add the following methods to the ReversiPosition:

Question 6: General Board Size (10 bonus points)

Do the above where the number of rows and columns is not fixed at 8, but can be different for each board. Notice that for input, the size of the board will depend on the number of rows and columns given in the input text.

Question 7: Playing a Game (20 bonus points)

Add a driver program to allow your program to play a human. Ten points of this bonus question are for the top-level driver program. The 10 points are for making the program look three moves ahead.





As always, the conceptual clarity of the solutions, the programming style, documentation, and demonstrated testing are important factors in evaluating assignments.