Board:
public class TTTBoard
{
private int[][] _theBoard;
private int _currentTurn;
public TTTBoard(int initialTurn)
{
_currentTurn = initialTurn;
_theBoard = new int[3][3];
//Initialize Board
for (int j = 0; j < 3; j++)
for(int i =0; i < 3; i++)
_theBoard[i][j] = 0;
}
public void displayBoard()
{
for (int j = 0; j < 3; j++)
System.out.print(_theBoard[0][j] + "\n");
for (int j = 0; j < 3; j++)
System.out.print(_theBoard[1][j] + "\n");
for (int j = 0; j < 3; j++)
System.out.print(_theBoard[2][j] + "\n");
}
public void changeTurn()
{
if (_currentTurn == 1)
{
_currentTurn = 2;
}
if (_currentTurn == 2)
{
_currentTurn = 1;
}
}
public int getCurrentTurn()
{
return _currentTurn;
}
public boolean isComplete()
{
boolean complete = true;
for (int j = 0; j < 3; j++)
for(int i =0; i < 3; i++)
_theBoard[i][j] = 0;
complete = false;
return complete;
}
public void makeMove(int row, int col)
{
int move = _theBoard[row][col];
for (int i =0; i < 3; i++)
for (int j =0; j < 3; j++)
if (_theBoard[i][j] == 1 | _theBoard[i][j] == 2)
System.out.print("Space is currently occuied, place make another selection.");
move = _theBoard[row][col];
}
public int isWinner()
{
int winner = 0;
//check to see if there is a winner
if (_theBoard[0][0] == _theBoard [0][1] && _theBoard[0][0] == _theBoard[0][2])
{
if (_theBoard[0][0] != 0)
winner = _theBoard[0][0];
}
if (_theBoard[1][0] == _theBoard [1][1] && _theBoard [1][0] == _theBoard[1][2])
{
if (_theBoard[1][0] != 0)
winner = _theBoard[1][0];
}
if (_theBoard[2][0] == _theBoard[2][1] && _theBoard[2][0] == _theBoard[2][2])
{
if (_theBoard[2][0] != 0)
winner = _theBoard[2][0];
}
if (_theBoard[0][1] == _theBoard [1][1] && _theBoard [0][1] == _theBoard[2][1])
{
if (_theBoard[0][1] != 0)
winner = _theBoard[0][1];
}
if (_theBoard[0][2] == _theBoard [1][2] && _theBoard [0][2] == _theBoard[2][2])
{
if (_theBoard[0][2] != 0)
winner = _theBoard[0][2];
}
if (_theBoard[0][1] == _theBoard [1][1] && _theBoard [0][1] == _theBoard[2][1])
{
if (_theBoard[0][1] != 0)
winner = _theBoard[0][1];
}
if (_theBoard[0][0] == _theBoard [1][1] && _theBoard [0][0] == _theBoard[2][2])
{
if (_theBoard[0][0] != 0)
winner = _theBoard[0][0];
}
if (_theBoard[2][0] == _theBoard [1][1] && _theBoard [2][0] == _theBoard[0][2])
{
if (_theBoard[2][0] != 0)
winner = _theBoard[2][0];
}
return winner;
}
}
Game:
import java.util.Scanner;
public class TTTGame
{
public static void main(String[] args)
{
TTTBoard myBoard = new TTTBoard(1);
Scanner input = new Scanner(System.in);
int turn;
int moveRow;
int moveCol;
int number;
//Begin Game with statements below
while (myBoard.isWinner() == 0 && myBoard.isComplete() == false)
{
myBoard.displayBoard();
turn = myBoard.getCurrentTurn();
System.out.println("Player " + turn + " Make your move.");
//Input of numbers - 1 to get Row and Col moves
System.out.println("Please enter row (1-3) of your move:");
number = input.nextInt();
moveRow = number - 1;
System.out.println("Please enter column (1-3) of your move:");
number = input.nextInt();
moveCol = number - 1;
myBoard.makeMove(moveRow, moveCol);
myBoard.changeTurn();
}
//Make Moves
turn = myBoard.getCurrentTurn();
System.out.println("Player " + turn + " Make a valid move.");
System.out.println("Please enter row (1-3) of your move:");
number = input.nextInt();
moveRow = number - 1;
System.out.println("Please enter column (1-3) of your move:");
number = input.nextInt();
moveCol = number -1;
myBoard.makeMove(moveRow, moveCol);
System.out.println("The winner is " + myBoard.isWinner());
if (myBoard.isWinner() != 0)
{
System.out.println("CATS GAME");
}
}
}

New Topic/Question
Reply




MultiQuote





|