2 Replies - 171 Views - Last Post: 28 June 2019 - 04:23 PM Rate Topic: -----

#1 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-May 19

Tic Tac Toe CPU logic

Posted 28 June 2019 - 03:48 PM

Hi guys, I'm working on a tic tac toe game and I'm having difficulty fully executing the logic as to how the CPU plays for each turn. This game's AI is supposed to randomly pick a spot that a human player hasn't already picked. It usually does it but sometimes it picks a spot and changes the button text value from "X" to "O" which isn't what I want. What I coded so far seems like a lot of work and is somewhat inefficient. Any tips to get the AI working more consistently and to possibly reduce the number of lines in my code?



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class TicTacToe2 extends JFrame
{
 public TicTacToe2() {
 }
 
 static double firstNum; 
 static double secondNum; 
 static double numericResult;
 static String operator; 
 static boolean player1Turn = true;
 
 static JButton numOne = new JButton(" "); static JButton numTwo = new JButton(" "); static JButton numThree = new JButton(" ");
 static JButton numFour = new JButton(" "); static JButton numFive = new JButton(" "); static JButton numSix = new JButton(" ");
 static JButton numSeven = new JButton(" "); static JButton numEight = new JButton(" "); static JButton numNine= new JButton(" ");
  
 static JTextField result = new JTextField("Player 1's turn"); 
 static JOptionPane playAgain = new JOptionPane("Play again?"); 
 static int player1WinTotal = 0;
 static int cpuPlayerWinTotal=0; 
 static JTextArea scoreBoard = new JTextArea("Win Total: "
   + "Player 1: " + player1WinTotal + " CPU Player: " + cpuPlayerWinTotal); 
 
 public static void checkforPlayer1Win() 
 { 
  
  if(numOne.getText().equals("X") && numTwo.getText().equals("X") && numThree.getText().equals("X"))
  {
      numOne.setBackground(Color.GREEN);
      numTwo.setBackground(Color.GREEN);
      numThree.setBackground(Color.GREEN);
   result.setText("Player 1 win's!");
   player1WinTotal+=1;
   
         String value = Integer.toString(player1WinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      }if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numFour.getText().equals("X") && numFive.getText().equals("X") && numSix.getText().equals("X"))
  {
    numFour.setBackground(Color.GREEN);
       numFive.setBackground(Color.GREEN);
       numSix.setBackground(Color.GREEN);
       result.setText("Player 1 win's!");
       player1WinTotal+=1;
    
          String value = Integer.toString(player1WinTotal);
          scoreBoard.setText("Wins: "
            + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
    
    JFrame frame = new JFrame("Play again?");
       String message = "Would you like to play again?";
       int answer = playAgain.showConfirmDialog(frame, message);
       if (answer == playAgain.YES_OPTION) 
       {
          numOne.setText(" ");
          numTwo.setText(" ");
          numThree.setText(" ");
          numFour.setText(" ");
          numFive.setText(" ");
          numSix.setText(" ");
          numSeven.setText(" ");
          numEight.setText(" ");
          numNine.setText(" ");
          
          result.setText("Player 1's turn!");
          
          numOne.setBackground(null);
       numTwo.setBackground(null);
       numThree.setBackground(null);
       numFour.setBackground(null);
       numFive.setBackground(null);
       numSix.setBackground(null);
       numSeven.setBackground(null);
       numEight.setBackground(null);
       numNine.setBackground(null);
       
       player1Turn=true;
          
       } else if (answer == playAgain.NO_OPTION) 
       {
         System.exit(0);
       }
  }
  
  else if(numSeven.getText().equals("X") && numEight.getText().equals("X") && numNine.getText().equals("X"))
  {
    numSeven.setBackground(Color.GREEN);
   numEight.setBackground(Color.GREEN);
   numNine.setBackground(Color.GREEN);
   result.setText("Player 1 win's!");
   player1WinTotal+=1;
   
         String value = Integer.toString(player1WinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numOne.getText().equals("X") && numFour.getText().equals("X") && numSeven.getText().equals("X"))
  {
    numOne.setBackground(Color.GREEN);
   numFour.setBackground(Color.GREEN);
   numSeven.setBackground(Color.GREEN);
   result.setText("Player 1 win's!");
   player1WinTotal+=1;
   
         String value = Integer.toString(player1WinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numOne.getText().equals("X") && numFive.getText().equals("X") && numNine.getText().equals("X"))
  {
    numOne.setBackground(Color.GREEN);
   numFive.setBackground(Color.GREEN);
   numNine.setBackground(Color.GREEN);
   result.setText("Player 1 win's!");
   player1WinTotal+=1;
   
         String value = Integer.toString(player1WinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  else if(numTwo.getText().equals("X") && numFive.getText().equals("X") && numEight.getText().equals("X"))
  {
    numTwo.setBackground(Color.GREEN);
   numFive.setBackground(Color.GREEN);
   numEight.setBackground(Color.GREEN);
   result.setText("Player 1 win's!");
   player1WinTotal+=1;
   
         String value = Integer.toString(player1WinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  else if(numThree.getText().equals("X") && numFive.getText().equals("X") && numSeven.getText().equals("X"))
  {
    numThree.setBackground(Color.GREEN);
   numFive.setBackground(Color.GREEN);
   numSeven.setBackground(Color.GREEN);
   result.setText("Player 1 win's!");
   player1WinTotal+=1;
   
         String value = Integer.toString(player1WinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numThree.getText().equals("X") && numSix.getText().equals("X") && numNine.getText().equals("X"))
  {
    numThree.setBackground(Color.GREEN);
   numSix.setBackground(Color.GREEN);
   numNine.setBackground(Color.GREEN);
   result.setText("Player 1 win's!");
   player1WinTotal+=1;
   
         String value = Integer.toString(player1WinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
 }
  public static void CPUmove1() 
 {
  if(numOne.getText()=="X" & numTwo.getText()==" " & numThree.getText()==" " 
    & numFour.getText()==" " &  numFive.getText()==" " &  numSix.getText()==" " 
    &  numSeven.getText()==" "  & numEight.getText()==" " & numNine.getText()==" ")
  {
    
    Random r = new Random(); 
    int num=r.nextInt(8) + 1;
    
    switch(num)
    {
    case 1:
     numTwo.doClick(); 
     numTwo.setText("O");
     break;
    case 2:
     numThree.doClick(); 
     numThree.setText("O");
     break;
    case 3:
     numFour.doClick(); 
     numFour.setText("O");
     break;
    case 4:
     numFive.doClick(); 
     numFive.setText("O");
     break;
    case 5:
     numSix.doClick(); 
     numSix.setText("O");
     break;
    case 6:
     numSeven.doClick(); 
     numSeven.setText("O");
     break;
    case 7:
     numEight.doClick(); 
     numEight.setText("O");
     break;
    case 8:
     numNine.doClick(); 
     numNine.setText("O");
     break;
     
    }
    
    
  }
  
  else if(numTwo.getText()=="X" & numOne.getText()==" " & numThree.getText()==" " 
    & numFour.getText()==" " &  numFive.getText()==" " &  numSix.getText()==" " 
    &  numSeven.getText()==" "  & numEight.getText()==" " & numNine.getText()==" ")
  {
    
    Random r = new Random(); 
    int num=r.nextInt(8) + 1;
    
    switch(num)
    {
    case 1:
     numOne.doClick(); 
     numOne.setText("O");
     break;
    case 2:
     numThree.doClick(); 
     numThree.setText("O");
     break;
    case 3:
     numFour.doClick(); 
     numFour.setText("O");
     break;
    case 4:
     numFive.doClick(); 
     numFive.setText("O");
     break;
    case 5:
     numSix.doClick(); 
     numSix.setText("O");
     break;
    case 6:
     numSeven.doClick(); 
     numSeven.setText("O");
     break;
    case 7:
     numEight.doClick(); 
     numEight.setText("O");
     break;
    case 8:
     numNine.doClick(); 
     numNine.setText("O");
     break;
     
    }}
  
 }
 
 /*public static void CPUmove2()   
 {
  if(numTwo.getText()=="X")
  {
   if(numOne.getText()==" " & numThree.getText()==" " & numFour.getText()==" ")
   {
    Random r = new Random(); 
    int num=r.nextInt(3) + 1;
    
    if(num==1)
    {
     numOne.doClick(); 
     numOne.setText("O");
    }
    if(num==2)
    {
     numThree.doClick(); 
     numThree.setText("O");
    }
    if(num==3)
    {
     numFour.doClick(); 
     numFour.setText("O");
    }
   }
   else if(numFive.getText()==" " & numSix.getText()==" " & numSeven.getText()==" ")
   {
    Random s = new Random(); 
    int num=s.nextInt(3) + 1;
    
    if(num==1)
    {
     numFive.doClick(); 
     numFive.setText("O");
    }
    if(num==2)
    {
     numSix.doClick(); 
     numSix.setText("O");
    }
    if(num==3)
    {
     numSeven.doClick(); 
     numSeven.setText("O");
    }
   }
   else if(numEight.getText()==" " & numNine.getText()==" ")
   {
    Random t = new Random(); 
    int num=t.nextInt(2) + 1;
    
    if(num==1)
    {
     numEight.doClick(); 
     numEight.setText("O");
    }
    if(num==2)
    {
     numNine.doClick(); 
     numNine.setText("O");
    }
   
    
   }
  }
   
 }*/
 
 public static void checkforCPUWin() 
 { 
  
  if(numOne.getText().equals("O") && numTwo.getText().equals("O") && numThree.getText().equals("O"))
  {
      numOne.setBackground(Color.RED);
      numTwo.setBackground(Color.RED);
      numThree.setBackground(Color.RED);
   result.setText("CPU wins!");
   cpuPlayerWinTotal+=1;
   
         String value = Integer.toString(cpuPlayerWinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + player1WinTotal + " CPU Player: " + value); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      }if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numFour.getText().equals("O") && numFive.getText().equals("O") && numSix.getText().equals("O"))
  {
    numFour.setBackground(Color.RED);
       numFive.setBackground(Color.RED);
       numSix.setBackground(Color.RED);
       result.setText("CPU wins!");
    cpuPlayerWinTotal+=1;
    
          String value = Integer.toString(cpuPlayerWinTotal);
          scoreBoard.setText("Wins: "
            + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
    
    JFrame frame = new JFrame("Play again?");
       String message = "Would you like to play again?";
       int answer = playAgain.showConfirmDialog(frame, message);
       if (answer == playAgain.YES_OPTION) 
       {
          numOne.setText(" ");
          numTwo.setText(" ");
          numThree.setText(" ");
          numFour.setText(" ");
          numFive.setText(" ");
          numSix.setText(" ");
          numSeven.setText(" ");
          numEight.setText(" ");
          numNine.setText(" ");
          
          result.setText("Player 1's turn!");
          
          numOne.setBackground(null);
       numTwo.setBackground(null);
       numThree.setBackground(null);
       numFour.setBackground(null);
       numFive.setBackground(null);
       numSix.setBackground(null);
       numSeven.setBackground(null);
       numEight.setBackground(null);
       numNine.setBackground(null);
       
       player1Turn=true;
          
       } else if (answer == playAgain.NO_OPTION) 
       {
         System.exit(0);
       }
  }
  
  else if(numSeven.getText().equals("O") && numEight.getText().equals("O") && numNine.getText().equals("O"))
  {
    numSeven.setBackground(Color.RED);
   numEight.setBackground(Color.RED);
   numNine.setBackground(Color.RED);
   result.setText("CPU wins!");
   cpuPlayerWinTotal+=1;
   
         String value = Integer.toString(cpuPlayerWinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numOne.getText().equals("O") && numFour.getText().equals("O") && numSeven.getText().equals("O"))
  {
    numOne.setBackground(Color.RED);
   numFour.setBackground(Color.RED);
   numSeven.setBackground(Color.RED);
   result.setText("CPU wins!");
   cpuPlayerWinTotal+=1;
   
         String value = Integer.toString(cpuPlayerWinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numOne.getText().equals("O") && numFive.getText().equals("O") && numNine.getText().equals("O"))
  {
    numOne.setBackground(Color.RED);
   numFive.setBackground(Color.RED);
   numNine.setBackground(Color.RED);
   result.setText("CPU wins!");
   cpuPlayerWinTotal+=1;
   
         String value = Integer.toString(cpuPlayerWinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  else if(numTwo.getText().equals("O") && numFive.getText().equals("O") && numEight.getText().equals("O"))
  {
    numTwo.setBackground(Color.RED);
   numFive.setBackground(Color.RED);
   numEight.setBackground(Color.RED);
   result.setText("CPU wins!");
   cpuPlayerWinTotal+=1;
   
         String value = Integer.toString(cpuPlayerWinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" "); numTwo.setText(" "); numThree.setText(" ");
         numFour.setText(" "); numFive.setText(" ");numSix.setText(" ");
         numSeven.setText(" "); numEight.setText(" "); numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  else if(numThree.getText().equals("O") && numFive.getText().equals("O") && numSeven.getText().equals("O"))
  {
    numThree.setBackground(Color.RED);
   numFive.setBackground(Color.RED);
   numSeven.setBackground(Color.RED);
   result.setText("CPU wins!");
   cpuPlayerWinTotal+=1;
   
         String value = Integer.toString(cpuPlayerWinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
  else if(numThree.getText().equals("O") && numSix.getText().equals("O") && numNine.getText().equals("O"))
  {
   numThree.setBackground(Color.RED);
   numSix.setBackground(Color.RED);
   numNine.setBackground(Color.RED);
   result.setText("CPU wins!");
   cpuPlayerWinTotal+=1;
   
         String value = Integer.toString(cpuPlayerWinTotal);
         scoreBoard.setText("Wins: "
           + "Player 1: " + value + " CPU Player: " + cpuPlayerWinTotal); 
   
   JFrame frame = new JFrame("Play again?");
      String message = "Would you like to play again?";
      int answer = playAgain.showConfirmDialog(frame, message);
      if (answer == playAgain.YES_OPTION) 
      {
         numOne.setText(" ");
         numTwo.setText(" ");
         numThree.setText(" ");
         numFour.setText(" ");
         numFive.setText(" ");
         numSix.setText(" ");
         numSeven.setText(" ");
         numEight.setText(" ");
         numNine.setText(" ");
         
         result.setText("Player 1's turn!");
         
         numOne.setBackground(null);
      numTwo.setBackground(null);
      numThree.setBackground(null);
      numFour.setBackground(null);
      numFive.setBackground(null);
      numSix.setBackground(null);
      numSeven.setBackground(null);
      numEight.setBackground(null);
      numNine.setBackground(null);
      
      player1Turn=true;
         
      } else if (answer == playAgain.NO_OPTION) 
      {
        System.exit(0);
      }
  }
  
 }
 
 public static void ch()
 {
  if(numOne.getText()=="X" && numTwo.getText()==" " && player1Turn==false)
  {
   numTwo.doClick();
  }
 }
 
public static void main(String args[])
 {
   JFrame calcFrame = new JFrame("Calculator"); 
   calcFrame.setTitle("Calculator");
   calcFrame.setSize(340, 400);
   calcFrame.setResizable(false);
   
   JPanel calcPanel = new JPanel(new BorderLayout(5,5));
   calcPanel.setBorder(new EmptyBorder(4,4,4,4));
   
   JPanel calcButtonPanel = new JPanel(new GridLayout(4,4));
   
   //result.setHorizontalAlignment(SwingConstants.RIGHT);
   result.setEditable(false);
   scoreBoard.setEditable(false);
   calcPanel.add(result, BorderLayout.NORTH);
   calcPanel.add(scoreBoard, BorderLayout.SOUTH);
   
   calcButtonPanel.add(numOne); calcButtonPanel.add(numTwo); calcButtonPanel.add(numThree);  
   calcButtonPanel.add(numFour); calcButtonPanel.add(numFive); calcButtonPanel.add(numSix); 
   calcButtonPanel.add(numSeven); calcButtonPanel.add(numEight); calcButtonPanel.add(numNine); 
   
   numOne.addActionListener(new ActionListener() 
   {
       public void actionPerformed(ActionEvent e) 
       {
        if(player1Turn == true && numOne.getText()==" ")
        {
         numOne.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numOne.getText()==" ")
        {
         numOne.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
        
        if(numOne.getText()=="X")
        {
         numOne.setEnabled(false); 
         if(numTwo.getText()==" " || numThree.getText()==" " 
       & numFour.getText()==" " ||  numFive.getText()==" " ||  numSix.getText()==" " 
       &  numSeven.getText()==" "  || numEight.getText()==" " || numNine.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
        if(numTwo.getText()==" " & numTwo.isEnabled());
        numTwo.doClick(); 
        numTwo.setText("O");
        numTwo.setEnabled(false);
        break;
       case 2:
        if(numThree.getText()==" " & numThree.isEnabled());
        numThree.doClick(); 
        numThree.setText("O");
        numThree.setEnabled(false);
        break;
       case 3:
        if(numFour.getText()==" " & numFour.isEnabled());
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false);
        break;
       case 4:
        if(numFive.getText()==" " & numFive.isEnabled());
        numFive.doClick(); 
        numFive.setText("O");
        numFive.setEnabled(false);
        break;
       case 5:
        if(numSix.getText()==" " & numSix.isEnabled());
        numSix.doClick(); 
        numSix.setText("O");
        numSix.setEnabled(false);
        break;
       case 6:
        if(numSeven.getText()==" " & numSeven.isEnabled());
        numSeven.doClick(); 
        numSeven.setText("O");
        numSeven.setEnabled(false);
        break;
       case 7:
        if(numEight.getText()==" " & numEight.isEnabled());
        numEight.doClick(); 
        numEight.setText("O");
        numEight.setEnabled(false);
        break;
       case 8:
        if( numNine.getText()==" " &  numNine.isEnabled());
        numNine.doClick(); 
        numNine.setText("O");
        numNine.setEnabled(false);
        break;
        
       }
       
         }
        
         
        }
      checkforPlayer1Win();
      
      checkforCPUWin();
       }
       
   });
   
  
   
   
   numTwo.addActionListener(new ActionListener() 
   {
       public void actionPerformed(ActionEvent f) 
       {
        if(player1Turn == true && numTwo.getText()==" ")
        {
         numTwo.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numTwo.getText()==" ")
        {
         numTwo.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
      
        if(numTwo.getText()=="X")
        {
         numTwo.setEnabled(false); 
         if(numOne.getText()==" " || numThree.getText()==" " 
       & numFour.getText()==" " ||  numFive.getText()==" " ||  numSix.getText()==" " 
       &  numSeven.getText()==" "  || numEight.getText()==" " || numNine.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
        if(numOne.getText()==" " & numOne.isEnabled());
     
        numOne.doClick(); 
        numOne.setText("O");
        numOne.setEnabled(false);
        break;
       case 2:
        if(numThree.getText()==" " & numThree.isEnabled());
     
        numThree.doClick(); 
        numThree.setText("O");
        numThree.setEnabled(false);
        break;
       case 3:
        if(numFour.getText()==" " & numFour.isEnabled());
       
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false);
        break;
       case 4:
        if(numFive.getText()==" " & numFive.isEnabled());
     
        numFive.doClick(); 
        numFive.setText("O");
        numFive.setEnabled(false);
        break;
       case 5:
        if(numSix.getText()==" " & numSix.isEnabled());
       
        numSix.doClick(); 
        numSix.setText("O");
        numSix.setEnabled(false);
        break;
       case 6:
        if(numSeven.getText()==" " & numSeven.isEnabled());
      
        numSeven.doClick(); 
        numSeven.setText("O");
        numSeven.setEnabled(false);
        break;
       case 7:
        if(numEight.getText()==" " & numEight.isEnabled());
      
        numEight.doClick(); 
        numEight.setText("O");
        numEight.setEnabled(false);
        break;
       case 8:
        if(numNine.getText()==" " & numNine.isEnabled());
        numNine.doClick(); 
        numNine.setText("O");
     numNine.setEnabled(false);
        break;
        
       }
       
         }
        
         
        }
        checkforPlayer1Win();
      
      checkforCPUWin();
   
       }
       {
     
       }
   });
   
   numThree.addActionListener(new ActionListener() 
   {
       public void actionPerformed(ActionEvent f) 
       {
        if(player1Turn == true && numThree.getText()==" ")
        {
         numThree.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numThree.getText()==" ")
        {
         numThree.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
        
        if(numThree.getText()=="X")
        {
         numThree.setEnabled(false); 
         if(numOne.getText()==" " || numTwo.getText()==" " 
       & numFour.getText()==" " ||  numFive.getText()==" " ||  numSix.getText()==" " 
       &  numSeven.getText()==" "  || numEight.getText()==" " || numNine.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
     if(numOne.getText()==" " & numOne.isEnabled());
        numOne.doClick(); 
        numOne.setText("O");
     numOne.setEnabled(false);
        break;
       case 2:
        if(numTwo.getText()==" " & numTwo.isEnabled());
        
        numTwo.doClick(); 
        numTwo.setText("O");
        numTwo.setEnabled(false);
        break;
       case 3:
        if(numFour.getText()==" " & numFour.isEnabled());
       
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false);
        break;
       case 4:
        if(numFive.getText()==" " & numFive.isEnabled());
       
        numFive.doClick(); 
        numFive.setText("O");
        numFive.setEnabled(false);
        break;
       case 5:
        if(numSix.getText()==" " & numSix.isEnabled());
          
        numSix.doClick(); 
        numSix.setText("O");
        numSix.setEnabled(false);
        break;
       case 6:
        if(numSeven.getText()==" " & numSeven.isEnabled());
        numSeven.doClick(); 
        numSeven.setText("O");
     numSeven.setEnabled(false);
        break;
       case 7:
        if(numEight.getText()==" " & numEight.isEnabled());
        numEight.doClick(); 
        numEight.setText("O");
        numTwo.setEnabled(false);
        break;
       case 8:
        if(numNine.getText()==" " & numNine.isEnabled());
        numNine.doClick(); 
        numNine.setText("O");
     numNine.setEnabled(false);
        break;
        
       }
       
         }
        
         
        }
      checkforPlayer1Win();
      checkforCPUWin();
          // if(result.getText())
       }
       
   });
   
   numFour.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent f) 
        {
         if(player1Turn == true && numFour.getText()==" ")
         {
          numFour.setText("X");
          result.setText("CPU Player's turn");
          player1Turn = false;
         }
         else if(player1Turn == false && numFour.getText()==" ")
         {
          numFour.setText("O");
          result.setText("Player 1 turn");
          player1Turn = true;
         }         
         
         if(numFour.getText()=="X")
         {
          numFour.setEnabled(false); 
          if(numOne.getText()==" " || numTwo.getText()==" " 
        & numThree.getText()==" " ||  numFive.getText()==" " ||  numSix.getText()==" " 
        &  numSeven.getText()==" "  || numEight.getText()==" " || numNine.getText()==" ")
          {
           Random r = new Random(); 
        int num=r.nextInt(8) + 1;
        
        switch(num)
        {
        case 1:
          if(numOne.getText()==" " & numOne.isEnabled());
         numOne.doClick(); 
         numOne.setText("O");
         numOne.setEnabled(false);
         break;
        case 2:
          if(numTwo.getText()==" " & numTwo.isEnabled());
         numTwo.doClick(); 
         numTwo.setText("O");
         numTwo.setEnabled(false);
         break;
        case 3:
          if(numThree.getText()==" " & numThree.isEnabled());
         numThree.doClick(); 
         numThree.setText("O");
         numThree.setEnabled(false);
         break;
        case 4:
          if(numFive.getText()==" " & numFive.isEnabled());
         numFive.doClick(); 
         numFive.setText("O");
      numFive.setEnabled(false);
         break;
        case 5:
          if(numSix.getText()==" " & numSix.isEnabled());
         numSix.doClick(); 
         numSix.setText("O");
      numSix.setEnabled(false);
         break;
        case 6:
          if(numSeven.getText()==" " & numSeven.isEnabled());
         numSeven.doClick(); 
         numSeven.setText("O");
      numSeven.setEnabled(false);
         break;
        case 7:
          if(numEight.getText()==" " & numEight.isEnabled());
         numEight.doClick(); 
         numEight.setText("O");
      numEight.setEnabled(false);
         break;
        case 8:
          if(numNine.getText()==" " & numNine.isEnabled());
        
         numNine.doClick(); 
         numNine.setText("O");
      numNine.setEnabled(false);
         break;
         
        }
        
          }
         
          
         }
         else if(numFour.getText()=="O")
         {
          numFour.setEnabled(false);
         }
         checkforPlayer1Win();
      checkforCPUWin();
        }
        
    });
   
   numFive.addActionListener(new ActionListener() 
   {
       public void actionPerformed(ActionEvent e) 
       {
        if(player1Turn == true && numFive.getText()==" ")
        {
         numFive.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numFive.getText()==" ")
        {
         numFive.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
        
        if(numFive.getText()=="X")
        {
         numFive.setEnabled(false); 
         if(numOne.getText()==" " || numTwo.getText()==" " 
       & numThree.getText()==" " ||  numFour.getText()==" " ||  numSix.getText()==" " 
       &  numSeven.getText()==" "  || numEight.getText()==" " || numNine.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
        if(numOne.getText()==" " & numOne.isEnabled());
        numOne.doClick(); 
        numOne.setText("O");
        numOne.setEnabled(false);
        break;
       case 2:
           if(numTwo.getText()==" " & numTwo.isEnabled());
        numTwo.doClick(); 
        numTwo.setText("O");
        numTwo.setEnabled(false);
        break;
       case 3:
           if(numThree.getText()==" " & numThree.isEnabled());
        numThree.doClick(); 
        numThree.setText("O");
        numThree.setEnabled(false);
        break;
       case 4:
           if(numFour.getText()==" " & numFour.isEnabled());
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false);
        break;
       case 5:
           if(numSix.getText()==" " & numSix.isEnabled());
        numSix.doClick(); 
        numSix.setText("O");
        numSix.setEnabled(false);
        break;
       case 6:
           if(numSeven.getText()==" " & numSeven.isEnabled());
        numSeven.doClick(); 
        numSeven.setText("O");
        numSeven.setEnabled(false);
        break;
       case 7:
           if(numEight.getText()==" " & numEight.isEnabled());
        numEight.doClick(); 
        numEight.setText("O");
        numEight.setEnabled(false);
        break;
       case 8:
           if(numNine.getText()==" " & numNine.isEnabled());
        numNine.doClick(); 
        numNine.setText("O");
        numNine.setEnabled(false);
        break;
        
       }
       
         }       
        }
        checkforPlayer1Win();
        
      checkforCPUWin();
       }
   });
   
   numSix.addActionListener(new ActionListener() 
   {
       public void actionPerformed(ActionEvent e) 
       {
        if(player1Turn == true && numSix.getText()==" ")
        {
         numSix.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numSix.getText()==" ")
        {
         numSix.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
        
        if(numSix.getText()=="X")
        {
         numSix.setEnabled(false); 
         if(numOne.getText()==" " || numTwo.getText()==" " 
       & numThree.getText()==" " ||  numFour.getText()==" " ||  numFive.getText()==" " 
       &  numSeven.getText()==" "  || numEight.getText()==" " || numNine.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
        if(numOne.getText()==" " & numOne.isEnabled());
        numOne.doClick(); 
        numOne.setText("O");
        numOne.setEnabled(false);
        break;
       case 2:
        if(numTwo.getText()==" " & numTwo.isEnabled());
        numTwo.doClick(); 
        numTwo.setText("O");
        numTwo.setEnabled(false);
        break;
       case 3:
        if(numThree.getText()==" " & numThree.isEnabled());
        numThree.doClick(); 
        numThree.setText("O");
        numThree.setEnabled(false);
        break;
       case 4:
        if(numFour.getText()==" " & numFour.isEnabled());
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false);
        break;
       case 5:
        if(numFive.getText()==" " & numFive.isEnabled());
        numFive.doClick(); 
        numFive.setText("O");
        numFive.setEnabled(false);
        break;
       case 6:
        if(numSeven.getText()==" " & numSeven.isEnabled());
        numSeven.doClick(); 
        numSeven.setText("O");
        numSeven.setEnabled(false);
        break;
       case 7:
        if(numEight.getText()==" " & numEight.isEnabled());
        numEight.doClick(); 
        numEight.setText("O");
        numEight.setEnabled(false);
        break;
       case 8:
        if(numNine.getText()==" " & numNine.isEnabled());
        numNine.doClick(); 
        numNine.setText("O");
        numNine.setEnabled(false);
        break;
        
       }
       
         }       
        }
        
        checkforPlayer1Win();
        //CPUmove1(); 
      //CPUmove2();
      checkforCPUWin();
       }
   });
   
   numSeven.addActionListener(new ActionListener() 
   {
       public void actionPerformed(ActionEvent e) 
       {
        if(player1Turn == true && numSeven.getText()==" ")
        {
         numSeven.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numSeven.getText()==" ")
        {
         numSeven.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
        if(numSeven.getText()=="X")
        {
         numSeven.setEnabled(false); 
         if(numOne.getText()==" " || numTwo.getText()==" " 
       & numThree.getText()==" " ||  numFour.getText()==" " ||  numSix.getText()==" " 
       &  numEight.getText()==" "  || numFive.getText()==" " || numNine.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
           if(numOne.getText()==" " & numOne.isEnabled());
        numOne.doClick(); 
        numOne.setText("O");
        numOne.setEnabled(false);
        break;
       case 2:
           if(numTwo.getText()==" " & numTwo.isEnabled());
        numTwo.doClick(); 
        numTwo.setText("O");
        numTwo.setEnabled(false);
        break;
       case 3:
           if(numThree.getText()==" " & numThree.isEnabled());
        numThree.doClick(); 
        numThree.setText("O");
        numThree.setEnabled(false);
        break;
       case 4:
           if(numFour.getText()==" " & numFour.isEnabled());
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false);
        break;
       case 5:
           if(numFive.getText()==" " & numFive.isEnabled());
        numFive.doClick(); 
        numFive.setText("O");
        numFive.setEnabled(false);
        break;
       case 6:
           if(numSix.getText()==" " & numSix.isEnabled());
        numSix.doClick(); 
        numSix.setText("O");
        numSix.setEnabled(false);
        break;
       case 7:
           if(numEight.getText()==" " & numEight.isEnabled());
        numEight.doClick(); 
        numEight.setText("O");
        numEight.setEnabled(false);
        break;
       case 8:
           if(numNine.getText()==" " & numNine.isEnabled());
        numNine.doClick(); 
        numNine.setText("O");
        numNine.setEnabled(false);
        break;
        
       }
       
         }       
        }
        else if(numSeven.getText()=="O")
        {
         numSeven.setEnabled(false);
        }
        checkforPlayer1Win();
      checkforCPUWin();
       }
   });
   
   numEight.addActionListener(new ActionListener()
   {
       public void actionPerformed(ActionEvent e) 
       {
        if(player1Turn == true && numEight.getText()==" ")
        {
         numEight.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numEight.getText()==" ")
        {
         numEight.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
        
        if(numEight.getText()=="X")
        {
         numEight.setEnabled(false); 
         if(numOne.getText()==" " || numTwo.getText()==" " 
       & numThree.getText()==" " ||  numFour.getText()==" " ||  numSix.getText()==" " 
       &  numSeven.getText()==" "  || numFive.getText()==" " || numNine.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
           if(numOne.getText()==" " & numOne.isEnabled());
        numOne.doClick(); 
        numOne.setText("O");
        numOne.setEnabled(false);
        break;
       case 2:
           if(numTwo.getText()==" " & numTwo.isEnabled());
        numTwo.doClick(); 
        numTwo.setText("O");
        numTwo.setEnabled(false); 

        break;
       case 3:
           if(numThree.getText()==" " & numThree.isEnabled());
        numThree.doClick(); 
        numThree.setText("O");
        numThree.setEnabled(false);
        break;
       case 4:
           if(numFour.getText()==" " & numFour.isEnabled());
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false); 

        break;
       case 5:
           if(numFive.getText()==" " & numFive.isEnabled());
        numFive.doClick(); 
        numFive.setText("O");
        numFive.setEnabled(false); 

        break;
       case 6:
           if(numSix.getText()==" " & numSix.isEnabled());
        numSix.doClick(); 
        numSix.setText("O");
        numSix.setEnabled(false); 

        break;
       case 7:
           if(numSeven.getText()==" " & numSeven.isEnabled());
        numSeven.doClick(); 
        numSeven.setText("O");
        numSeven.setEnabled(false); 

        break;
       case 8:
           if(numNine.getText()==" " & numNine.isEnabled());
        numNine.doClick(); 
        numNine.setText("O");
        numNine.setEnabled(false); 

        break;
        
       }
       
         }       
        }
        checkforPlayer1Win();
        //CPUmove1(); 
      //CPUmove2();
      checkforCPUWin();
       }
   });
   
   numNine.addActionListener(new ActionListener()
   {
       public void actionPerformed(ActionEvent e) 
       {
        if(player1Turn == true && numNine.getText()==" ")
        {
         numNine.setText("X");
         result.setText("CPU Player's turn");
         player1Turn = false;
        }
        else if(player1Turn == false && numNine.getText()==" ")
        {
         numNine.setText("O");
         result.setText("Player 1 turn");
         player1Turn = true;
        }
        
        if(numNine.getText()=="X")
        {
         numNine.setEnabled(false); 
         
         if(numOne.getText()==" " || numTwo.getText()==" " 
       & numThree.getText()==" " ||  numFour.getText()==" " ||  numSix.getText()==" " 
       &  numSeven.getText()==" "  || numFive.getText()==" " || numEight.getText()==" ")
         {
          Random r = new Random(); 
       int num=r.nextInt(8) + 1;
       
       switch(num)
       {
       case 1:
           if(numOne.getText()==" " & numOne.isEnabled());
        numOne.doClick(); 
        numOne.setText("O");
        numOne.setEnabled(false); 
        break;
       case 2:
           if(numTwo.getText()==" " & numTwo.isEnabled());
        numTwo.doClick(); 
        numTwo.setText("O");
        numTwo.setEnabled(false); 
        break;
       case 3:
           if(numThree.getText()==" " & numThree.isEnabled());
        numThree.doClick(); 
        numThree.setText("O");
        numThree.setEnabled(false); 
        break;
       case 4:
        if(numFour.getText()==" " & numFour.isEnabled());
        numFour.doClick(); 
        numFour.setText("O");
        numFour.setEnabled(false); 
        break;
       case 5:
       	if(numFive.getText()==" " & numFive.isEnabled());
        numFive.doClick(); 
        numFive.setText("O");
        numFive.setEnabled(false); 
        break;
       case 6:
    	if(numSix.getText()==" " & numSix.isEnabled());
        numSix.doClick(); 
        numSix.setText("O");
        numSix.setEnabled(false); 

        break;
       case 7:
        if(numSeven.getText()==" " & numSeven.isEnabled());
        numSeven.doClick(); 
        numSeven.setText("O");
        numSeven.setEnabled(false); 

        break;
       case 8:
        if(numEight.getText()==" " & numEight.isEnabled());
        numEight.doClick(); 
        numEight.setText("O");
        numEight.setEnabled(false); 
        break;
        
       }
       
         }       
        }
        checkforPlayer1Win();
        //CPUmove1(); 
     // CPUmove2();
      checkforCPUWin();
        
       }
   });
   
   calcPanel.add(calcButtonPanel, BorderLayout.CENTER);
   calcFrame.getContentPane().add(calcPanel);
   calcFrame.setVisible(true);
   
   
 }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe CPU logic

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15116
  • View blog
  • Posts: 60,494
  • Joined: 12-June 08

Re: Tic Tac Toe CPU logic

Posted 28 June 2019 - 03:55 PM

You can shorten that 'cpu' part up quite a bit.

You know how many spots there are.. so have the random get a value for that.

Push the random value through a switch.

Eeach case in the switch check the corresponding button(?)'s text for a value. If it is not empty then mark it and the AI is done.

If it is not empty then loop back to picking a random value again.
Was This Post Helpful? 0
  • +
  • -

#3 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: Tic Tac Toe CPU logic

Posted 28 June 2019 - 04:23 PM

The code needs to be compiled with the compiler's -Xlint option to display many warnings that are problems that need to be fixed.

There are too many repeated blocks of code with only slight differences ( the 3 buttons).
These need to be changed to one method that is called with unique values for the differences ie pass the 3 buttons as parameters to the method.

This post has been edited by NormR: 28 June 2019 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1