import java.util.Scanner;
public class TicTacToe {
private static final char EMPTYCHAR = '_';
private char[][] testGrid;
private Scanner input;
public TicTacToe(Scanner scanner) {
input = scanner;
int boardSize = 0;
do {
System.out.print("Please enter the board size 'n': ");
boardSize = input.nextInt();
} while (boardSize < 3);
testGrid = new char[boardSize][boardSize];
for (int row = 0; row < testGrid.length; row++) {
for (int col = 0; col < testGrid.length; col++) {
testGrid[row][col] = EMPTYCHAR;
}
}
}
private void PrintGrid() {
for (int row = 0; row < testGrid.length; row++) {
for (int col = 0; col < testGrid.length; col++) {
System.out.print(testGrid[row][col] + " ");
}
System.out.println();
}
}
private boolean gameFinished(char currentPlayer) {
int counter;
//horizontal wins
for (int r = 0; r < testGrid.length; r++) {
counter = 0;
for (int c = 0; c < testGrid.length; c++) {
if (testGrid[r][c] == currentPlayer) {
counter++;
}
}
if (counter == testGrid.length) {
System.out.println(currentPlayer + " has won!");
return true;
}
}
//vertical wins
for (int c = 0; c < testGrid.length; c++) {
counter = 0;
for (int r = 0; r < testGrid.length; r++) {
if (testGrid[r][c] == currentPlayer) {
counter++;
}
}
if (counter == testGrid.length) {
System.out.println(currentPlayer + " has won!");
return true;
}
}
//diagonal win 1
counter = 0;
for (int i = 0; i < testGrid.length; i++) {
if (testGrid[i][i] == currentPlayer) {
counter++;
}
}
if (counter == testGrid.length) {
System.out.println(currentPlayer + " has won!");
return true;
}
//diagonal win 2
counter = 0;
for (int i = 0; i < testGrid.length; i++) {
if (testGrid[i][testGrid.length-1-i] == currentPlayer) {
counter++;
}
}
if (counter == testGrid.length) {
System.out.println(currentPlayer + " has won!");
return true;
}
//check if there are available actions
for (int r = 0; r < testGrid.length; r++) {
for (int c = 0; c < testGrid.length; c++) {
if (testGrid[r][c] == EMPTYCHAR) {
return false;
}
}
}
System.out.println("The game has ended with a draw!");
return true;
}
private boolean validateAction(int row, int col) {
if ((row >= testGrid.length) || (row < 0)) return false;
if ((col >= testGrid.length) || (col < 0)) return false;
if (testGrid[row][col] != EMPTYCHAR) return false;
return true;
}
public void play() {
int row, col;
boolean validAction;
char currentPlayer = 'X'; //currentPlayer is shown as either X or O
do
{
//promt the user for coordinates for X and O
do {
System.out.print("Please enter row for " + currentPlayer + " : ");
row = input.nextInt();
System.out.print("Please enter column for " + currentPlayer + " : ");
col = input.nextInt();
validAction = validateAction(row,col);
if (!validAction) {
System.out.println("Invalid action! Please enter new action!");
}
} while (!validAction);
testGrid[row][col] = currentPlayer;
if (currentPlayer == 'X') {
currentPlayer = 'O';
} else {
currentPlayer = 'X';
}
PrintGrid();
} while (!gameFinished(testGrid[row][col]));
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
TicTacToe t = new TicTacToe(input);
t.play();
}
}