[JAVA] Need help trying to create an Artificial Intelligence in which the comput
ID: 3804127 • Letter: #
Question
[JAVA] Need help trying to create an Artificial Intelligence in which the computer can never lose to the user in a game of Tic-Tac-Toe. I'm not sure how to come up with the algorithm. Any help would be appreciated. Here is an example of what the output of the program should be:
There's some information regarding the classes I'll be using.
----------------------------------------------------------------
Okay, so my question is how do I come up with the appropriate algorithm using trees to make the AI never be defeated by the user in this game. Here is some info about what classes I should be using for my assignment:
Thanks!
Sample input/output run 1**** Welcome to Tic Tac Toe This is the board: 1 2 3 4 5 6 7 8 9 Please make a move LILI I the probability of a win is:0.41 the probability of a draw is:0.20 the probability of a loss is:0.37 Please make a move IXIXIol lol I LI_l_l the probability of a win is:0.23 the probability of a draw is:0.25 the probability of a loss is:0.51 Please make a move XX 0 the probability of a win is 0.0 the probability of a draw is:0.0 the probability of a loss is:1.0 The winner is :0Explanation / Answer
Answer:
import java.util.*;
public class GameofTicTacToe
{
private int incrementer;
private char location[]=new char[10];
private char gamer;
public static void main(String args[])
{
String ch;
GameofTicTacToe input=new GameofTicTacToe();
do{
input.newGame();
input.play();
System.out.println ("If you want to play the game again please enter yes ");
Scanner in =new Scanner(System.in);
ch=in.nextLine();
System.out.println("ch value is "+ch);
}while (ch.equals("yes"));
}
public void newGame()
{
char locationdef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
int i;
incrementer = 0;
gamer = 'X';
for (i=1; i<10; i++) location[i]=locationdef[i];
presentworkingBoard();
}
public String presentworkingBoard()
{
System.out.println( " " );
System.out.println( " " );
System.out.println( " " + location [1] + " | " +location [2]+ " | " +location [3]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +location [4]+ " | " +location [5]+ " | " +location [6]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +location [7]+ " | " +location [8]+ " | " +location [9]);
System.out.println( " | | " );
System.out.println( " | | " );
System.out.println( " " );
return "presentworkingBoard";
}
public void play()
{
int center;
char empty = ' ';
System.out.println( "Player " + obtainGamer() +" will go initially and would take the letter 'X'" );
do {
presentworkingBoard();
System.out.println( " Player " + obtainGamer() +" occupy a location" );
boolean locationOccupied = true;
while (locationOccupied) {
Scanner in =new Scanner (System.in);
center=in.nextInt();
locationOccupied = examinelocation(center);
if(locationOccupied==false)
location[center]=obtainGamer();
}
System.out.println( "Brilliant move" );
presentworkingBoard();
upcomingGamer();
}while ( examineWinner() == empty );
}
public char examineWinner()
{
char Winner = ' ';
if (location[1] == 'X' && location[2] == 'X' && location[3] == 'X') Winner = 'X';
if (location[4] == 'X' && location[5] == 'X' && location[6] == 'X') Winner = 'X';
if (location[7] == 'X' && location[8] == 'X' && location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[4] == 'X' && location[7] == 'X') Winner = 'X';
if (location[2] == 'X' && location[5] == 'X' && location[8] == 'X') Winner = 'X';
if (location[3] == 'X' && location[6] == 'X' && location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[5] == 'X' && location[9] == 'X') Winner = 'X';
if (location[3] == 'X' && location[5] == 'X' && location[7] == 'X') Winner = 'X';
if (Winner == 'X' )
{System.out.println("Gamer1 won the game." );
return Winner;
}
if (location[1] == 'O' && location[2] == 'O' && location[3] == 'O') Winner = 'O';
if (location[4] == 'O' && location[5] == 'O' && location[6] == 'O') Winner = 'O';
if (location[7] == 'O' && location[8] == 'O' && location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[4] == 'O' && location[7] == 'O') Winner = 'O';
if (location[2] == 'O' && location[5] == 'O' && location[8] == 'O') Winner = 'O';
if (location[3] == 'O' && location[6] == 'O' && location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[5] == 'O' && location[9] == 'O') Winner = 'O';
if (location[3] == 'O' && location[5] == 'O' && location[7] == 'O') Winner = 'O';
if (Winner == 'O' )
{
System.out.println( "Gamer2 won the game" );
return Winner; }
for(int i=1;i<10;i++)
{
if(location[i]=='X' || location[i]=='O')
{
if(i==9)
{
char design='D';
System.out.println(" Game is standoff ");
return design;
}
continue;
}
else
break;
}
return Winner;
}
public boolean examinelocation(int center)
{
if (location[center] == 'X' || location[center] == 'O')
{
System.out.println("That location is already occupied please choose other location");
return true;
}
else {
return false;
}
}
public void upcomingGamer()
{
if (gamer == 'X')
gamer = 'O';
else gamer = 'X';
}
public String getName()
{
return "Tic Tac Toe Mind Game" ;
}
public char obtainGamer()
{
return gamer;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.