I’m working on a tic-tac-toe project where I need to create a basic computer player. The computer should automatically find an empty spot and place its marker (represented by number 3) when it’s the AI’s turn.
This is for my Java programming course but our instructor got sick and isn’t available to help. I’m really stuck on implementing the aiMove() method at the bottom of my code. Can someone point me in the right direction?
package tictactoe;
public class GameLogic {
public static int HUMAN = 1;
public static int COMPUTER = 3;
static Scanner input = new Scanner(System.in);
static final int DELAY = 300;
public static int[][] grid = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
public static void main(String[] args) {
boolean gameRunning = true;
while (gameRunning) {
displayGrid(grid);
humanMove();
delay(DELAY);
checkGameState(grid);
input.reset();
}
}
public static void delay(int milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (Exception e) {
// ignore
}
}
public static void makeMove(int[][] grid, int row, int col, int symbol) {
grid[row][col] = symbol;
}
public static boolean hasWon(int[][] grid, int symbol) {
// check rows and columns
for (int x = 0; x < grid.length; x++) {
int matches = 0;
for (int y = 0; y < grid[x].length; y++) {
if (grid[x][y] == symbol
|| grid[y][x] == symbol ) {
matches++;
}
}
if (matches == grid[x].length)
return true;
}
return false;
}
public static int checkGameState(int[][] grid) {
if (hasWon(grid, HUMAN)) {
return 1;
} else if (hasWon(grid, COMPUTER)) {
return 3;
} else {
return 0;
}
}
public static void displayGrid(int[][] grid) {
System.out.println("======");
for (int[] line : grid) {
for (int cell : line) {
System.out.print(cell + " ");
}
System.out.println("");
}
System.out.println("======");
System.out.println("status: " + checkGameState(grid));
}
public static void humanMove() {
char key = input.nextChar();
switch (key) {
case 'q':
makeMove(grid, 0, 0, 1);
break;
case 'w':
makeMove(grid, 0, 1, 1);
break;
case 'e':
makeMove(grid, 0, 2, 1);
break;
case 'a':
makeMove(grid, 1, 0, 1);
break;
case 's':
makeMove(grid, 1, 1, 1);
break;
case 'd':
makeMove(grid, 1, 2, 1);
break;
case 'z':
makeMove(grid, 2, 0, 1);
break;
case 'x':
makeMove(grid, 2, 1, 1);
break;
case 'c':
makeMove(grid, 2, 2, 1);
break;
}
}
public static int aiMove(int[][] grid) {
int position[] = {-1, -1};
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid(0)) {
// need help here
}
}
}
}
}