public class TicTacToe {
public static void main(String[] args) {
char[][] board = new char[3][3];
int winner = 1;
boolean checkWinner = false;
resetBoard(board);
printBoard(board);
char curPlayer = PLAYER1;
while (!checkWinner) {
Out.print("\n \nCurrent Player: " + curPlayer);
Out.println();
Out.print("Enter row: ");
int row = enterPosition(0, 2);
Out.println();
Out.print("Enter Column: ");
int column = enterPosition(0, 2);
Out.println();
// right
boolean setPlayerAtPosition = setPlayerAtPosition(board, row, column, curPlayer);
if (setPlayerAtPosition == true) {
board[row][column] = curPlayer;
}
playerMove(board, row, column, curPlayer);
if(setPlayerAtPosition) {
if (curPlayer == PLAYER1) {
curPlayer = PLAYER2;
} else {
curPlayer = PLAYER1;
}}
printBoard(board);
checkWinner = checkWinner(board, row);
if(curPlayer == 1) {
winner = 2;
}else {
winner = 1;
}
if(checkWinner) {
Out.println("\nGame Over");
Out.println("Player " + winner + " wins!");
}
}
} // end main
static char EMPTY = (' ');
static char PLAYER1 = ('X');
static char PLAYER2 = ('O');
static void resetBoard(char[][] board) { // sets all fields to EMPTY
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = EMPTY;
}
}
} // end resetBoard
static void printBoard(char[][] board) {
Out.println(" 0 1 2");
for (int i = 0; i < 3; i++) { // i = row
for (int j = 0; j < 3; j++) { // j = column
Out.print("\t" + board[i][j]);
if (j == 0 || j == 1) {
Out.print(" |");
}
}
if (i == 0) {
Out.println("0");
Out.print("\n -------------------- \n");
}
if (i == 1) {
Out.println("1");
Out.print("\n -------------------- \n");
}
if (i == 2) {
Out.println("2");
}
}
} // end of printBoard
static int enterPosition(int min, int max) { // lowest and highest possible number given to method
int position = -1;
while (position < min || position > max) {
position = In.readInt();
if (position < min || position > max) {
Out.println("Error! Value is not in the allowed range (0 - 2)");
}
}
return position;
} // end enterPostion
static boolean setPlayerAtPosition(char[][] board, int row, int column, char player) {
boolean setPlayerAtPosition = false;
if (board[row][column] == EMPTY) {
setPlayerAtPosition = true;
}
if (board[row][column] == PLAYER1 || board[row][column] == PLAYER2) {
Out.println("Error! This position is not empty anymore!");
}
return setPlayerAtPosition;
}// end setPlayerAtPosition
static boolean activeGame() {
return true;
} // end of activeGame
static void playerMove(char[][] board, int row, int column, char curPlayer) {
if (setPlayerAtPosition(board, row, column, curPlayer) == true) {
board[row][column] = curPlayer;
}
}// end playerMove
static boolean checkWinner(char[][] board, int row) {
boolean checkWinner = false;
for (row = 0; row < 3; row++) {
if (board[row][0] == board[row][1] && board[row][1] == board[row][2] && board[row][0] != EMPTY) {
checkWinner = true;
}
}
for (row = 0; row < 3; row++) {
if (board[0][row] == board[1][row] && board[1][row] == board[2][row] && board[0][row] != EMPTY) {
checkWinner = true;
}
}
return checkWinner;
} // end checkWinner
}// end class