examples.tictactoe
Class TicTacToeBoard

java.lang.Object
  extended byexamples.tictactoe.TicTacToeBoard
All Implemented Interfaces:
Serializable

class TicTacToeBoard
extends Object
implements Serializable

A class representing a simple 2-dimensional TicTacToe board. Includes methods to set valid moves and determine who (if anyone) has won the game.

Version:
1.0 - Fall 2001
Author:
Mr. Edgar Troudt, Department of Computer Science, QC / CUNY

Field Summary
private  int[][] board_
          Double-subscripted array storing the board.
 
Constructor Summary
TicTacToeBoard()
          Default constructor.
 
Method Summary
 int getMove(int position)
          Gets the number of the move.
 int getMove(int x, int y)
          Gets the number of the move.
 boolean hasEnded()
          Returns true if all fields are set
 boolean isEmpty()
           
 boolean setMove(int position, int move)
          Simple method of setting a move.
 boolean setMove(int y, int x, int move)
          Complex, coordinate-based, method of setting a move.
 String toString()
          Generates a string representing the current state of the gameboard.
 int whoWon()
          Determines which player (if any) has won.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

board_

private int[][] board_
Double-subscripted array storing the board. 0 means no move, 1 means the first player made a move, 2 means the second. Assume that the subscripts represent [Row][Column].

Constructor Detail

TicTacToeBoard

public TicTacToeBoard()
Default constructor. Initializes the board array and fills it with 0's representing blank spots.

Method Detail

setMove

public boolean setMove(int position,
                       int move)
Simple method of setting a move. Takes in a position between 1 and 9 representing the positions on the board as follows: 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9

Parameters:
position - Spot on the TicTacToe board to be filled.
move - Symbol to be placed on the position (either 1 or 2)
Returns:
true if the move was successful, false if the move was either invalid or unsuccessful.

setMove

public boolean setMove(int y,
                       int x,
                       int move)
Complex, coordinate-based, method of setting a move. Takes in an exact array position between [0][0] and [2][2] to place the move.

Parameters:
y - The row to place the symbol in.
x - The column to place the symbol in.
move - The symbol to be placed on that position (either 1 or 2).
Returns:
true if the move was successful, false if the move was either invalid or unsuccessful.

getMove

public int getMove(int x,
                   int y)
Gets the number of the move. 0 means no move, 1 means the first player made a move, 2 means the second. Assume that the subscripts represent [Row][Column].


getMove

public int getMove(int position)
Gets the number of the move. 0 means no move, 1 means the first player made a move, 2 means the second. Assume that the subscripts represent [Row][Column].


isEmpty

public boolean isEmpty()

hasEnded

public boolean hasEnded()
Returns true if all fields are set


whoWon

public int whoWon()
Determines which player (if any) has won. Checks every column and row, plus the diagonals to see if they contain three matching symbols (except 0, which can not win).

Returns:
0 if nobody has won, 1 if player 1 has won, 2 if player 2 has won.

toString

public String toString()
Generates a string representing the current state of the gameboard. The string is screen-printable, already formatted into 3 rows and 3 columns with the proper separators.



Copyright © Fraunhofer Gesellschaft. All Rights Reserved.