how can I incorporate computer generated random move in this tic tac toe java co
ID: 3563720 • Letter: H
Question
how can I incorporate computer generated random move in this tic tac toe java code:
package homework;
import javax.swing.*;
public class TicTacToe2
{
public static void main(String[] args)
{
//Create default board String
String defaultBoard="";
//Loop over all numbers
for(int i = 1; i <= 9; i++)
{
//Declare partial string
String partialBoard;
//If number is multiple of 'size', add new line characters
if(i % 3 == 0)
{
partialBoard = i + " ";
}
//Otherwise, print spaces
else
{
partialBoard = i + " ";
}
//Append to default board
defaultBoard += partialBoard;
}
//Start loop
boolean playAgain=true;
while(playAgain)
{
//Initialize output board
String board = defaultBoard;
//Initialize positions
char pos1=1;
char pos2=2;
char pos3=3;
char pos4=4;
char pos5=5;
char pos6=6;
char pos7=7;
char pos8=8;
char pos9=9;
//Initialize player
boolean playerAPlays=true;
//Initialize result
boolean win=false;
//Loop 9 times
for(int play=0; play<9 && !win; play++)
{
//Initialize variables for input
boolean validInput=false;
int pos=0;
//Loop for valid input
while(!validInput)
{
//Configure message requesting input
String message;
if(playerAPlays)
{
message=" Player Noughts: Input position";
}
else
{
message=" Player Crosses: Input position";
}
//Output window and get input
String input=JOptionPane.showInputDialog(null, board+message, "TIC-TAC-TOE", JOptionPane.INFORMATION_MESSAGE);
//Check if the length of the input is valid
if(input.length()!=1)
{
continue;
}
//Convert the input to an integer
pos = (int)input.charAt(0) - '0';
//Check if it's a number
if(pos < 1 || pos > 9)
{
continue;
}
//Check if that number is taken
switch(pos)
{
case 1:
if(pos1==1)
{
validInput=true;
}
break;
case 2:
if(pos2==2)
{
validInput=true;
}
break;
case 3:
if(pos3==3)
{
validInput=true;
}
break;
case 4:
if(pos4==4)
{
validInput=true;
}
break;
case 5:
if(pos5==5)
{
validInput=true;
}
break;
case 6:
if(pos6==6)
{
validInput=true;
}
break;
case 7:
if(pos7==7)
{
validInput=true;
}
break;
case 8:
if(pos8==8)
{
validInput=true;
}
break;
case 9:
if(pos9==9)
{
validInput=true;
}
break;
default:
break;
}
}
//Valid input
//Determine piece
char piece;
if(playerAPlays)
{
piece='o';
}
else
{
piece='x';
}
//Update the position
switch(pos)
{
case 1:
pos1=piece;
board=board.replace('1', piece);
break;
case 2:
pos2=piece;
board=board.replace('2', piece);
break;
case 3:
pos3=piece;
board=board.replace('3', piece);
break;
case 4:
pos4=piece;
board=board.replace('4', piece);
break;
case 5:
pos5=piece;
board=board.replace('5', piece);
break;
case 6:
pos6=piece;
board=board.replace('6', piece);
break;
case 7:
pos7=piece;
board=board.replace('7', piece);
break;
case 8:
pos8=piece;
board=board.replace('8', piece);
break;
case 9:
pos9=piece;
board=board.replace('9', piece);
break;
}
//Check for winner
if(pos1 == pos2 && pos2 == pos3)
{
win=true;
}
else if(pos4 == pos5 && pos5 == pos6)
{
win=true;
}
else if(pos7 == pos8 && pos8 == pos9)
{
win=true;
}
else if(pos1 == pos4 && pos4 == pos7)
{
win=true;
}
else if(pos2 == pos5 && pos5 == pos8)
{
win=true;
}
else if(pos3 == pos6 && pos6 == pos9)
{
win=true;
}
else if(pos1 == pos5 && pos5 == pos9)
{
win=true;
}
else if(pos3 == pos5 && pos5 == pos7)
{
win=true;
}
//Change player if game is not over
if(!win)
{
playerAPlays=!playerAPlays;
}
}
//Configure result message
String message;
if(win)
{
if(playerAPlays)
{
message="Noughts win!!";
}
else
{
message="Crosses win!!";
}
}
else
{
message="You tied!!";
}
message+=" Do you want to play again? (y/n)";
//Output result window
boolean validFlag=false;
while(!validFlag)
{
String playAgainStr=JOptionPane.showInputDialog(null, board+message, "TIC-TAC-TOE", JOptionPane.INFORMATION_MESSAGE);
if(playAgainStr.equals("n"))
{
validFlag=true;
playAgain=false;
}
else if(playAgainStr.equals("y"))
{
validFlag=true;
}
}
}
System.exit(0);
}
}
Explanation / Answer
replace
//Output window and get input
String input=JOptionPane.showInputDialog(null, board+message, "TIC-TAC-TOE", JOptionPane.INFORMATION_MESSAGE);
//Check if the length of the input is valid
if(input.length()!=1)
{
continue;
}
//Convert the input to an integer
pos = (int)input.charAt(0) - '0';
with
if(play%2 ==0){
Random random = new Random();
pos= random.nextInt(9)+1;
//random.nextInt(9) will generate a random number between 0 and 9.Including 0 but excluding 9
playerAPlays=true;
}else{
String input=JOptionPane.showInputDialog(null, board+message, "TIC-TAC-TOE", JOptionPane.INFORMATION_MESSAGE);
//Check if the length of the input is valid
if(input.length()!=1)
{
continue;
}
//Convert the input to an integer
pos = (int)input.charAt(0) - '0';
playerAPlays=false;
)
This code will take input from computer and user alternatively
PlayerA is computer and playerB is user.
Change the if condition i have given to make playerA as User.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.