Assignment 6
Up Assignment 1 Assignment 2 Assignment 3 Assignment 4 Assignment 5 Assignment 6 Assignment 7 Assignment 8 Assignment 9 Assignment 10

 

Objects and Classes: The game of Tic-Tac-Toe

Due: Monday, Oct 29, 5:00 PM

 

Write a program to simulate two players playing the game of Tic-Tac-Toe.  You should define the following three classes with their associated methods:

  1. The class Board simulates a Tic-Toe-Toe board, and has the following public methods:
    constructor Board takes no arguments and returns an empty board (note: if you do this right, this constructor will not have to do anything at all).
    toString (overridden) turns the board into a string (see sample output below).
    setSquare takes three arguments: a character which is either 'O' or 'X', and two integers indicating the square in which to place the O or X, numbered like this:
                (0,0) (0,1) (0,2)
                (1,0) (1,1) (1,2)
                (2,0) (2,1) (2,2)
    getSquare takes two integers indicating row and column as above, and returns 'O', 'X', or ' ' (the latter is the space character, indicating that no one has played that square yet).
    winner takes no arguments and returns 'O' if O is a winner, 'X' if X is a winner, and ' ' if neither.
     
  2. The class Player simulates a player, and has the following public methods:
    constructor Player takes a string argument for the name of the player, and either an 'O' or 'X' to indicate that s/he is playing O's or X's, respectively.
    name returns the name of the player (a string).  (If you wish, you may implement this as a property.)
    move takes a board object as an argument and modifies the board with the player's move recorded on it (i.e. its return type is void).  This move should be chosen randomly but should not overwrite any previous move!
     
  3. The class TicTacToe, which has one static method called play.  When invoked, this method creates two Players and a Board, and simulates a game, displaying the board after each move, stopping when there is winner and declaring who the winner is, or "draw" if there is no winner.

Write your program as a Windows Application that has two input text boxes (one for the 'X' player and one for the 'O' player), a button labelled "Play", and a read-only textbox to display the result of the game sequence (see sample output below).

Hints:

  1. The easiest way to deal with the random behavior is to create a single static instance variable bound to a fresh Random object shared by all Players.
  2. Implement your board using a two-dimensional array.

Here is an example of my program in action, assuming that "Paul" and "John" are the names of the 'X' and 'O' players, respectively:

---
---
O--

---
---
O-X

-O-
---
O-X

-O-
---
OXX

-OO
---
OXX

XOO
---
OXX

XOO
O--
OXX

XOO
OX-
OXX


Paul wins!!

 

Extra credit (probably hard!):  Define a SmartPlayer class that has the identical interface as Player, except that it uses a winning strategy: that is, if it is allowed to go first, it will either win or draw, but will never lose!