Trying to add an ai part to my code but dont know where to start or how would I
ID: 3872549 • Letter: T
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 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;
}
}
Explanation / Answer
import java.util.Random;
public class tictactoeGame {
private char mBoard[];
private static int DIM;
//public static final char HUMAN_PLAYER ='X';
public static final char PLAYER_ONE = 'X';
// public static final char ANDROID_PLAYER = '0';
public static final char PLAYER_TWO = '0';
public static final char EMPTY_SPACE = ' ';
private Random mRand = new Random();
public static int getBOARD_SIZE(){
return DIM*DIM;
}
public tictactoeGame(int DIM)
{
this.DIM = DIM;
mBoard = new char[DIM*DIM];
for (int i=0;i<getBOARD_SIZE();i++)
{
mBoard[i]=EMPTY_SPACE;
}
mRand = new Random();
}
public void setDIM(int DIM)
{
this.DIM=DIM;
}
public void clearBoard(){
for(int i=0;i<getBOARD_SIZE();++i){
mBoard[i]= EMPTY_SPACE;
}
}
public void setMove(char player , int location){
mBoard[location]=player;
}
/*
public int getComputerMove(){
int move;
//
for(int i=0;i<getBOARD_SIZE();i++){
if(mBoard[i]!=PLAYER_ONE && mBoard[i] != PLAYER_TWO){
char curr = mBoard[i];
mBoard[i]=PLAYER_TWO;
if(checkForWinner()== 3){
setMove(PLAYER_TWO,i);
return i;
}
else
mBoard[i] = curr;
}
}
for (int i=0 ; i < getBOARD_SIZE() ; i++){
if(mBoard[i] !=PLAYER_ONE && mBoard[i] != PLAYER_TWO)
{
char curr = mBoard[i];
mBoard[i] = PLAYER_ONE;
if(checkForWinner() == 2){
setMove(PLAYER_TWO,i);
return i;
}
else
mBoard[i]= curr;
}
}
do {
move = mRand.nextInt(getBOARD_SIZE());
}while (mBoard[move] == PLAYER_ONE || mBoard [move] == PLAYER_TWO);
setMove(PLAYER_TWO, move);
return move;
}
public int checkForWinner(){
for (int i=0;i<=6 ;i+=3){
if(mBoard[i]==PLAYER_ONE && mBoard[i+1]== PLAYER_ONE && mBoard[i+2]== PLAYER_ONE )
return 2;
if(mBoard[i]==PLAYER_TWO && mBoard[i+1]== PLAYER_TWO && mBoard[i+2]== PLAYER_TWO )
return 3;
}
// this is for saying human player has won
if ((mBoard[0] == PLAYER_ONE && mBoard[4] == PLAYER_ONE && mBoard[8] == PLAYER_ONE) ||
(mBoard[2]== PLAYER_ONE && mBoard[4] == PLAYER_ONE && mBoard[6] == PLAYER_ONE) )
return 2;
// this is for saying android player has won .
if ((mBoard[0] == PLAYER_TWO && mBoard[4] == PLAYER_TWO && mBoard[8] == PLAYER_TWO) ||
(mBoard[2]== PLAYER_TWO && mBoard[4] == PLAYER_TWO && mBoard[6] == PLAYER_TWO) )
return 3;
// this is for saying "game is continue"
for (int i=0; i< getBOARD_SIZE();i++){
if(mBoard[i]!= PLAYER_ONE && mBoard[i]!= PLAYER_TWO)
return 0;
}
return 1;
}
*/
public int getComputerMove()
{
int move;
//check if computer can win
for(int i=0;i<getBOARD_SIZE();i++)
{
if(mBoard[i] ==EMPTY_SPACE)
{
// char curr = mBoard[i];
mBoard[i] = PLAYER_TWO;
if(checkForWinner()== 3)
{
setMove(PLAYER_TWO,i);
return i;
}
else
mBoard[i] = EMPTY_SPACE;
}
}
//check if computer can block human player
for(int i=0;i<getBOARD_SIZE();i++)
{
// if(mBoard[i] != HUMAN_PLAYER && mBoard[i] != COMPUTER_PLAYER)
if(mBoard[i]==EMPTY_SPACE)
{
// char curr = mBoard[i];
mBoard[i] = PLAYER_ONE;
if(checkForWinner()== 2)
{
setMove(PLAYER_TWO,i);
return i;
}
else
mBoard[i] = EMPTY_SPACE;
}
}
do {
move = mRand.nextInt(getBOARD_SIZE());
}while (mBoard[move]==PLAYER_ONE || mBoard[move]==PLAYER_TWO );
setMove(PLAYER_TWO,move);
return move;
}
public int checkForWinner() {
boolean isWin=false;
//row win
for(int i =0;i<getBOARD_SIZE();i+=DIM)
{
if (linematch(PLAYER_ONE,i,i+DIM,1))
return 2;
if (linematch(PLAYER_TWO,i,i+DIM,1))
return 3;
}
//column win
for(int i=0;i<DIM;i++)
{
if (linematch(PLAYER_ONE,i,getBOARD_SIZE(),DIM))
return 2;
if(linematch(PLAYER_TWO,i,getBOARD_SIZE(),DIM))
return 3;
}
// diagonal win
if((linematch(PLAYER_ONE,0,getBOARD_SIZE(),DIM+1))
|| (linematch(PLAYER_ONE,DIM-1,getBOARD_SIZE()-1,DIM-1)))
return 2;
if((linematch(PLAYER_TWO,0,getBOARD_SIZE(),DIM+1))
|| (linematch(PLAYER_TWO,DIM-1,getBOARD_SIZE()-1,DIM-1)))
return 3;
// if still have blanks return 0 else return 1 (it's a tie)
for (int i=0;i<getBOARD_SIZE();i++)
{
if(mBoard[i] == EMPTY_SPACE)
return 0;
}
return 1;
}
private boolean linematch(char humanPlayer, int start, int end, int step) {
for (int i=start;i<end;i+=step)
{
if(mBoard[i]!=humanPlayer)
return false;
}
return true;
}
}
May this code help you in implementing the Tic Tac Toe with ai. I am using the min max algo with randomise move.