public class TicTacToeGameRunner extends World<ColorTextPiece>
{
private boolean isXTurn;
private boolean winner;
/**
* Constructs a TicTacToe Game
*/
public TicTacToeGameRunner()
{
isXTurn = true;
winner = false;
makeBoard();
setMessage("Tic Tac Toe - X Goes First");
}
/**
* Initializes the board to empty tiles with color
*
*/
private void makeBoard()
{
setGrid( new BoundedGrid<ColorTextPiece>(3, 3) ); // gameboard size
/*
* Students, write nested for loops to set the text for all the tiles to an
* empty string and add them to the World/Grid.
*/
for( int i = 0; i< 3; i++)
{
for ( int n = 0; n > 3; n++)
{
ColorTextPiece piece = new ColorTextPiece(" ");
Location loc = new Location( i, n);
add(loc, piece);
}
}
}
/**
* Determines the winner. Currently written in O(1), independent of N,
* the number of pieces currently played
*
* @param loc loacation of the last piece just played
* *
* @return <code>true</code> if the piece just played at position <code>loc</code>
* completes a win for the player who just moved,
* <code>false</code> if no such winner yet
*/
private boolean determineWinner(Location loc)
{
// only need to check if 3 in a row from the current loc (last move made)
int dir = Location.AHEAD, consecutive = 0;
Location nextLoc;
Grid<ColorTextPiece> gr = getGrid();
if(gr.get(new Location( 0, 0)) != null)
{
if(gr.get(new Location ( 0, 0)) .equals( gr.get(new Location( 0, 1))))
{
if(gr.get(new Location(0, 1)).equals(gr.get(new Location(0, 2))))
{
return true;
}
}
}
if(gr.get(new Location( 1, 0)) != null)
{
if(gr.get(new Location ( 1, 0)) .equals( gr.get(new Location( 1, 1))))
{
if(gr.get(new Location ( 1, 1)) .equals( gr.get(new Location ( 1, 2))))
{
System.out.println("2");
return true;
}
}
}
if(gr.get(new Location( 2, 0)) != null)
{
if(gr.get(new Location ( 2, 0)) .equals( gr.get(new Location( 2, 1))))
{
if(gr.get(new Location ( 2, 1)) .equals( gr.get(new Location ( 2, 2))))
{
System.out.println("3");
return true;
}
}
}
if(gr.get(new Location( 0, 0)) != null)
{
if(gr.get(new Location ( 0, 0)) .equals( gr.get(new Location( 1, 0))))
{
if(gr.get(new Location ( 1, 0)) .equals( gr.get(new Location ( 2, 0))))
{
System.out.println("4");
return true;
}
}
}
if(gr.get(new Location( 0, 1)) != null)
{
if(gr.get(new Location ( 0, 1)) .equals( gr.get(new Location( 1, 1))))
{
if(gr.get(new Location ( 1, 1)) .equals( gr.get(new Location ( 2, 1))))
{
System.out.println("5");
return true;
}
}
}
if(gr.get(new Location( 0, 2)) != null)
{
if(gr.get(new Location ( 0, 2)) .equals( gr.get(new Location( 1, 2))))
{
if(gr.get(new Location ( 1, 2)) .equals( gr.get(new Location( 2, 2))))
{
System.out.println("6");
return true;
}
}
}
if(gr.get(new Location( 0, 0)) != null)
{
if(gr.get(new Location ( 0, 0)) .equals( gr.get(new Location( 1, 1))))
{
if(gr.get(new Location ( 1, 1)) .equals( gr.get(new Location ( 2, 2))))
{
System.out.println("7");
return true;
}
}
}
if(gr.get(new Location( 0, 2)) != null)
{
if(gr.get(new Location ( 0, 2)) .equals( gr.get(new Location( 1, 1))))
{
if(gr.get(new Location ( 1, 1)) .equals( gr.get(new Location( 2, 0))))
{
System.out.println("8");
return true;
}
}
}
/*
* Students: There are 3 main axes from the current loc to check for 3-in-a-row.
* To help you write this you should first look at the constants within the Location
* class to see what is available to you. You will find Location.HALF_CIRCLE and
* Location.HALF_RIGHT of use. Essentially, for each of the 4 axes (the vertical,
* the horizontal, the major diagonal and the minor diagonal) you need to keep count
* of the number of consecutive pieces that match the piece played at location loc.
* You can, for example, go up from the current loc until you either run off the
* board or you get to a piece that doesn't match the one played, keeping count of
* the consecutive ones while you go upward - then, go downward adding on to your
* consecutive count. That will take care of the vertical axis.
* You can check at that point whether your count is >= 3 and, if so, return true.
* Otherwise, check the other 3 axes in a similar manner.
*/
return false;
}
/**
* When the current player clicks and attempts to place a piece, this
* method see whether it is a legal move and, if so, places a piece
* and calls determineWinner
*
* @param loc location passed from the GUI where the use just clicked
*
* @return <code>true</code> if location clicked was valid;
* <code>false</code> otherwise
*/
public boolean locationclicked(Location loc)
{
if ( winner )
{
return true;
}
if ( getGrid().get(loc).isBackground() ) // player making a legal move
{
if ( isXTurn )
{
add(loc, new ColorTextPiece("X"));
setMessage("O's Turn - X moved to " + loc);
}
else
{
add(loc, new ColorTextPiece("O"));
setMessage("X's Turn - O moved to " + loc);
}
if ( determineWinner(loc) )
{
winner = true;
if ( isXTurn )
setMessage("X WINS");
else
setMessage("O WINS");
}
isXTurn = !isXTurn;
}
else // player making an illegal move
{
if ( isXTurn )
setMessage("Occupied Cell - X, it's still your turn");
else
setMessage("Occupied Cell - O, it's still your turn");
}
return true;
}
/**
* Creates and positions a robot to test code.
*
* @param args arguments from the command line
*/
public static void main(String[] args)
{
System.out.println("New Game");
World ttt = new TicTacToeGameRunner();
// get rid of focus indicator on the gui
System.setProperty("info.gridworld.gui.selection", "hide");
ttt.show( );
}
}
When I run the program the grid comes up but when I click a tile nothing happens.

New Topic/Question
Reply


MultiQuote


|