I'm having problems generating CPU turn; preferably with AI, and also draw between player and CPU.
jButton1.setEnabled(false);
boolean Userturn = true;
JLabel[] label = new JLabel[9];
label [0] = jLabel1;
label [1] = jLabel2;
label [2] = jLabel3;
label [3] = jLabel4;
label [4] = jLabel5;
label [5] = jLabel6;
label [6] = jLabel7;
label [7] = jLabel8;
label [8] = jLabel9;
char [] Board = {
'1', '2', '3',
'4', '5', '6',
'7', '8', '9'
};
Random generator = new Random();
int CPU = generator.nextInt(9);
String xuser;
int iuser;
for (int x = 0; x < Board.length; x++) {
if(Userturn){
xuser = JOptionPane.showInputDialog("Enter a number between 1-9");
iuser = Integer.parseInt(xuser);
label [iuser -1] .setText ("O");
Board[iuser -1] = 'O';
if(Board [0] == 'O' && Board[1] == 'O' && Board[2] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [0] == 'X' && Board[1] == 'X' && Board[2] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
if(Board [3] == 'O' && Board[4] == 'O' && Board[5] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [3] == 'X' && Board[4] == 'X' && Board[5] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
if(Board [6] == 'O' && Board[7] == 'O' && Board[8] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [6] == 'X' && Board[7] == 'X' && Board[8] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
if(Board [0] == 'O' && Board[3] == 'O' && Board[6] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [0] == 'X' && Board[3] == 'X' && Board[6] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
if(Board [1] == 'O' && Board[4] == 'O' && Board[7] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [1] == 'X' && Board[4] == 'X' && Board[7] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
if(Board [2] == 'O' && Board[5] == 'O' && Board[8] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [2] == 'X' && Board[5] == 'X' && Board[8] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
if(Board [0] == 'O' && Board[4] == 'O' && Board[8] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [0] == 'X' && Board[4] == 'X' && Board[8] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
if(Board [2] == 'O' && Board[4] == 'O' && Board[6] == 'O'){
JOptionPane.showMessageDialog(null, "You Win!");
break;
}
if(Board [2] == 'X' && Board[4] == 'X' && Board[6] == 'X'){
JOptionPane.showMessageDialog(null, "You Lose!");
break;
}
Userturn = false;
}else{
Userturn = true;
}
}
}