Sunteți pe pagina 1din 3

ComS 227: Homework 4

Los Dobles

1 Your Job
In this homework you will learn about inheritance and polymorphism in the context of writing a framework
for two-player turn-based games. All two-player turn-based games follow the same general procedure:

...
player 1 takes a turn
player 2 takes a turn
player 1 takes a turn
player 2 takes a turn
player 1 takes a turn
player 2 takes a turn
player 1 takes a turn
...
The game ends when one of the players wins, the board is full, no pieces or cards are left, etc. The stopping
criteria depends on the particular game being played.
This homework requires you to write eight classes: TooManyPlayersException, IllegalMoveException,
TwoTurnGame, TwoTurnPlayer, AsciiTicTacToe, AsciiTicTacToePlayer, a two-player turn-based game
class modeling a game of your choosing, and a corresponding player class. All classes you create for
this homework should be in their own source files in a package named hw4. See the separate
instructions for how to submit your work.
Technical specifications are not linear narratives and often require multiple readings before things start
to click. It’s also true, however, that as soon as you have enough information to complete one method or
one class, you can implement it.

2 IllegalMoveException
This exception is a checked exception which extends Exception. It has no methods of its own.

3 TooManyPlayersException
This exception is an unchecked exception which extends RuntimeException. It has no methods of its own.

4 TwoTurnGame
This class models the common structure of two-player turn-based game.
1. It is abstract. It is too generic to be instantiated.

2. It has a constructor taking no arguments that sets up the game to have no players.
3. It has a method register which takes a TwoTurnPlayer argument. It adds the player to the game. If
the game already has two players, it throws a TooManyPlayersException.
4. It has a method play taking no arguments and returning a TwoTurnPlayer reference. It randomly
picks one of the two players to start the game. That player takes a turn. Then the other takes a turn.
The process repeats until the game is over. Depending on the game, there might not be a winner,

1
in which case you return null. If the game ends with a winner, return the winning player. On any
given round, if the first player ends the game, the second player does not take a turn. Method isOver
determines the stopping criteria. You may assume two players have been registered before this method
is called.
5. It has an abstract method isOver that returns a boolean indicating whether or not the game has
ended. Subclasses will flesh out this method according to the rules of the game being implemented.

5 TwoTurnPlayer
This class models the common structure of a single player in a two-player turn-based game.
• It is abstract. It is too generic to be instantiated.
• It has a constructor taking a String argument for the player’s name.
• It has a getName method which returns the player’s name.
• It has an abstract takeTurn method which returns nothing and takes in a TwoTurnGame argument.
Subclasses will flesh out this method so that a player actually advances the game.

6 AsciiTicTacToe
This class is an extension of TwoTurnGame that implements a simple TicTacToe game. It has:

• A constructor taking two String arguments. The first is the name of player X and the second the
name of player O. It creates a blank game board and registers the two players. Player X uses the ID
‘x’ and O uses ‘o’. Both symbols are lowercase.
• It overrides toString such that it returns the current board state as a String. If X has played at
(0,0) and O at (2,1), then the following is returned:

x--
---
-o-

• A method mark which takes as arguments an AsciiTicTacToePlayer, a row number, and a column
number, in that order. It attempts to place the player’s symbol at the cell specified by 0-based row
and column numbers. If the space is already occupied or falls outside the bounds of the board, an
IllegalMoveException is thrown.
• An isRowAll method which takes as arguments an int row number and a char symbol, in this order.
It returns true if the specified row is comprised entirely of the specified symbol.
• An isColumnAll method which takes as arguments an int column number and a char symbol, in this
order. It returns true if the specified column is comprised entirely of the specified symbol.
• An isEitherDiagonalAll method which takes as arguments a char symbol. It returns true if either
diagonal on the board is comprised entirely of the specified symbol.
• An overriding isOver method which returns a boolean. If the game is over, it returns true. Otherwise,
it returns false. The game is over when either the board is full or one of the players completes an
entire row, column, or diagonal. (A hint: you can simplify checking for this completion by doing so in
the mark method.)

2
7 AsciiTicTacToePlayer
This class is an extension of TwoTurnPlayer. It has:
• A constructor which takes a String name and a char ID, in that order.
• A method getID which returns the player’s ID as a char.
• A method takeTurn which overrides the superclass version. It takes a TwoTurnGame argument, which
you can assume to be an AsciiTicTacToeGame. It
1. Prints a newline.
2. Prints the board.
3. Prompts the user for two numbers, a row and a column, respectively. Input comes from System.in.
4. Attempts to mark the board with the player’s ID. Handle an IllegalMoveException by printing
“Illegal move. Try again.” and going back to step 1.
5. Prints the board.

8 Your Game and Your Player


These last two classes are mostly unspecified. You must write a game of your choosing. The game must
extend TwoTurnGame and your player must extend TwoTurnPlayer. Include in your game class a main
method so that we may run your code. Make sure you clearly document your code and use meaningful
variable names so that your grader can make sense of it.

9 Protected
You may need to use protected instance variables in order to communicate information across the class
hierarchy. You may not modify the public interface of the superclasses.

10 Extra
You are encouraged to share testing code on WebCT, but do not share any code from the required classes.

S-ar putea să vă placă și