/* I KEEP GETTING THIS ERROR CAN SOMEONE HELP ME OUT?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at TicTacToe.playerMove(TicTacToe.java:50)
at TicTacToeGame.main(TicTacToeGame.java:86)
*/
public class TicTacToe
{
private int player;
private static int[][] board;
public TicTacToe()
{
board = new int [3][3];
for(int row=0; row < 3; row++)
{
for(int col=0; col < 3; col++)
{
board[row][col] = 0;
}
}
displayBoard();
showBoard();
}
public int setPlayer(int player)
{
return this.player = player;
}
public boolean playerMove(int row, int col )
{
boolean move;
if (board[row][col] == 0)
{
board[row][col] = player;
return true;
} else {
move = false;
}
return move;
}
public boolean isGameOver()
{
boolean gameOver = true;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (board[row][col] == 0)
{
gameOver = false;
}
}
}
return gameOver;
}
public static void displayBoard ()
{
System.out.println("Tic Tac Toe Game\n" + "-----------");
for(int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
System.out.print(board[row][col] + " | ");
}
System.out.println("\n-----------");
}
}
public int determineWinner()
{
int winner =0;
if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] != 0 )
{
winner = board[0][0];
}
if (board[1][0] == board[1][1] && board[1][0] == board[1][2] && board[1][0] != 0 )
{
winner = board[1][0];
}
if (board[2][0] == board[2][1] && board[2][0] == board[2][2] && board[2][0] != 0 )
{
winner = board[2][0];
}
if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0] != 0 )
{
winner = board[0][0];
}
if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != 0 )
{
winner = board[0][1];
}
if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != 0 )
{
winner = board[0][2];
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != 0 )
{
winner = board[0][0];
}
if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != 0 )
{
winner = board[0][2];
}
return winner;
}
public static void showBoard()
{
System.out.println("Tic Tac Toe Game\n" + "-----------");
for(int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
System.out.print(board[col] + " | ");
}
System.out.println("\n-----------");
}
}
}
import java.util.Scanner;
public class TicTacToeGame
{
private static int player ;
public void switchPlayer() {
if (player ==1)
{
player = 2;
} else {
player =1;
}
}
public static void main(String[] args)
{
TicTacToe myGame = new TicTacToe();
Scanner input = new Scanner(System.in);
int row;
int col = 1;
while(myGame.determineWinner() == 0 && !myGame.isGameOver())
{
TicTacToe.displayBoard();
System.out.println("Player " + myGame.setPlayer(player));
System.out.println("Make your move.");
System.out.print("Row please (1-3):");
row = input.nextInt();
while(row < 1 || row > 3)
{
System.out.println("Invalid Row.");
System.out.print("Try again (1-3):");
row = input.nextInt();
}
System.out.print("Col please (1-3):");
col = input.nextInt();
while(col < 1 || col > 3)
{
System.out.println("Invalid Col.");
System.out.print("Try again (1-3):");
col = input.nextInt();
}
while(!myGame.playerMove(row, col))
{
System.out.println("Invalid Move... Try Again.");
System.out.print("Row please (1-3):");
row = input.nextInt();
while(row < 1 || row > 3)
{
System.out.println("Invalid Row.");
System.out.print("Try again (1-3):");
row = input.nextInt();
}
System.out.print("Col please (1-3):");
col = input.nextInt();
while(col < 1 || col > 3)
{
System.out.println("Invalid Col.");
System.out.print("Try again (1-3):");
col = input.nextInt();
}
row -= 1;
col -= 1;
}
}
if (myGame.determineWinner() == 0)
{
System.out.println("Sorry - No Winner");
}
else
{
System.out.print("The Winner is Player ");
if (myGame.setPlayer(player) == 1)
{
System.out.println("2");
}
else
{
System.out.println("1");
}
}
}
}