Question: Trying to add an ai part to my code but dont know where to start or how would I do this with the code I

Trying to add an ai part to my code but dont know where to start or how would I do this with the code I have created to have 1v1 player vs player but i want to add the player vs ai part!

MAIN CLASS

package tictactoe;

import java.util.Scanner;

/** * * @author */

public class TicTacToePractice {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

TicTacToe game = new TicTacToe();

System.out.println("TicTacToe");

do {

System.out.println("Board layout");

game.designboard();

int row;

int col;

do {

System.out.println("Player " + game.getPlayer() + " enter an empty row and column to place the x or o!");

row = scan.nextInt() - 1;

col = scan.nextInt() - 1;

} while (!game.placement(row, col));

game.changePlayer();

} while (!game.checkForWin() && !game.BoardFull());

if (game.BoardFull() && !game.checkForWin()) {

System.out.println("Draw");

} else {

System.out.println("current board layout:");

game.makeboard();

game.changePlayer();

System.out.println(Character.toUpperCase(game.getPlayer()) + "Wins!");

}

}

}

CLASS

package tictactoe;

/**

*

* @author

*/

public class TicTacToe {

private char[][] board;

private char player;

public TicTacToe() {

board = new char[3][3];

player = 'x';

makeboard();

}

public void makeboard() {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

board[i][j] = '-';

}

}

}

public void designboard() {

System.out.print("--------- ");

for (int i = 0; i < 3; i++) {

System.out.print("|");

for (int j = 0; j < 3; j++) {

System.out.print(board[i][j] + "|");

}

System.out.println();

System.out.println("--------");

}

}

public boolean BoardFull() {

boolean isFull = true;

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (board[i][j] == '-') {

isFull = false;

}

}

}

return isFull;

}

public boolean checkForWin() {

return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());

}

private boolean checkRowsForWin() {

for (int i = 0; i < 3; i++) {

if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {

return true;

}

}

return false;

}

private boolean checkColumnsForWin() {

for (int i = 0; i < 3; i++) {

if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {

return true;

}

}

return false;

}

private boolean checkDiagonalsForWin() {

return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true)

|| (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));

}

private boolean checkRowCol(char c1, char c2, char c3) {

return ((c1 != '-') && (c1 == c2) && (c2 == c3));

}

public void changePlayer() {

if (player == 'x') {

player = 'o';

} else {

player = 'x';

}

}

public char getPlayer() {

return player;

}

public boolean placement(int row, int col) {

if ((row >= 0) && (row < 3)) {

if ((col >= 0) && (col < 3)) {

if (board[row][col] == '-') {

board[row][col] = player;

return true;

}

}

}

return false;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!