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;
                   }
                }

    }

It doesn't look like you have coded any type of AI at all. in terms of how to make it the computers turn and draw the board, using your design of a loop, something like this:
turn=0;
while(gameover==0)
{
turn++;

if(turn%2==1) // users turn
{
hi input move
check for legality and if they won
draw board ( can be as simple as printing in the console)
}
else
{
run computer AI
Make move chosen
drawboard
}

} // while end loop


In terms of your AI, i recommend exploring two concepts, recursion and min max. recursion means you have a function make all possible moves at that level of turn and for each move it calls teh same function that makes the moves search() from that function. It terminates calling the function when their are no more moves to make and then scores and returns the score. 1=win 0=draw -1=loss. each ply of search wants to return the best move relative to the side its playing, so if your on ply 3 ( the computers second move ) and one of your moves scores a 1 ( a win) you return that. if the best you get out of your moves is a 0 you return that, and if all you got is -1 you return that. this is called min max.

Mike

This url seems to have some discussion of min max, and show some pictures of what the search tree looks like, http://www.ocf.berkeley.edu/~yosenl/extras/alphabeta/alphabeta.html

you will just need the min max part at the top, not an alpha beta search for tic tac toe, because tic tac toe is solvable with min max with little cpu time.

a simple recursive function is:

int calculate(int value){

if value =1 return 1;
else return value * calculate(value -1);

}

if you call it initally with 5 it will on the first call return 5 * calculate(4);

calcluate 4 is your second call returning 4 * calculate(3);
and so on tell you get 2 * calculate(1); where calculate(1) terminates and returns 1;

you have called your function 5 times in a row recursively and generated a factorial.

with a move search you contain your recursive call within a for loop of the moves at that ply.

like
generate moves

for a = 1 to max moves

make move on board
score -= search(depth -1); -= means that you are flipping the score on each ply to reflect computer vs player score ( a win for one is a loss for the other)
unmake move

if score better then best score record best score
end for

return best score.

if there are no moves that can be generated call the scoring function and return score to the downstream calling function.

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.