Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class AITTT {
- public static boolean playerTurn = true;
- public static boolean comTurn = false;
- public static String x = "X";
- public static String o = "O";
- public static JFrame frame;
- public static JPanel panel;
- public JButton res;
- public int count = 0;
- JButton[] b;
- ActionListener[] l = new ActionListener[9];
- public void changeTurns()
- {
- playerTurn = !playerTurn;
- comTurn = !comTurn;
- }
- public void endGame()
- {
- playerTurn = false;
- comTurn = false;
- }
- public void comWinLogic()
- {
- for(int i = 0 ;i<9&& comTurn == true;i++)
- {
- if(i == 0 || i == 3 || i == 6 )
- {
- if(b[i].getText() == o&&b[i+1].getText() == o)
- {
- if(b[i+2].getText() == "")
- {
- b[i+2].setText(o);
- endGame();
- }
- } else
- if(b[i].getText() == o&&b[i+2].getText() == o)
- if(b[i+1].getText() == "")
- {
- b[i+1].setText(o);
- endGame();
- }
- } else
- if(i == 2 || i == 5 || i == 8 )
- {
- if(b[i].getText() == o&&b[i-1].getText() == o)
- {
- if(b[i-2].getText() == "")
- {
- b[i-2].setText(o);
- endGame();
- }
- } else
- if(b[i].getText() == o&&b[i-2].getText() == o)
- if(b[i-1].getText() == "")
- {
- b[i-1].setText(o);
- endGame();
- }
- } else
- if(i == 0 || i == 2 || i == 3 )
- {
- if(b[i].getText() == o&&b[i+3].getText() == o)
- {
- if(b[i+6].getText() == "")
- {
- b[i+6].setText(o);
- endGame();
- }
- } else
- if(b[i].getText() == o&&b[i+6].getText() == o)
- if(b[i+3].getText() == "")
- {
- b[i+3].setText(o);
- endGame();
- }
- } else
- if(i == 6 || i == 7 || i == 8 )
- {
- if(b[i].getText() == o&&b[i-3].getText() == o)
- {
- if(b[i-6].getText() == "")
- {
- b[i-6].setText(o);
- endGame();
- }
- } else
- if(b[i].getText() == o&&b[i-6].getText() == o)
- if(b[i-3].getText() == "")
- {
- b[i-3].setText(o);
- endGame();
- }
- } else
- if(i == 0)
- {
- if(b[i].getText() == o&&b[i+4].getText() == o)
- {
- if(b[i+8].getText() == "")
- {
- b[i+8].setText(o);
- endGame();
- }
- } else
- if(b[i].getText() == o&&b[i+8].getText() == o)
- {
- if(b[i+4].getText() == "")
- {
- b[i+4].setText(o);
- endGame();
- }
- } else if(b[i+4].getText() == o&&b[i+8].getText() == o)
- if(b[i].getText() == "")
- {
- b[i].setText(o);
- endGame();
- }
- } else
- if(i == 2)
- {
- if(b[i].getText() == o&&b[i+2].getText() == o)
- {
- if(b[i+4].getText() == "")
- {
- b[i+4].setText(o);
- endGame();
- }
- } else
- if(b[i].getText() == o&&b[i+4].getText() == o)
- {
- if(b[i+2].getText() == "")
- {
- b[i+2].setText(o);
- endGame();
- }
- } else if(b[i+2].getText() == o&&b[i+4].getText() == o)
- if(b[i].getText() == "")
- {
- b[i].setText(o);
- endGame();
- }
- } else {comStopPlayer();}
- }
- }
- public void addActionListeners()
- {
- l[0] = new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- if(playerTurn)
- {
- if(b[0].getText() == "")
- b[0].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 2*/
- l[1] = new ActionListener(){
- public void actionPerformed(ActionEvent args0) {
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[1].getText() == "")
- b[1].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 3*/
- l[2] = new ActionListener(){
- public void actionPerformed(ActionEvent args0) {
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[2].getText() == "")
- b[2].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 4*/
- l[3] = new ActionListener(){
- public void actionPerformed(ActionEvent args0) {
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[3].getText() == "")
- b[3].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 5*/
- l[4] = new ActionListener(){
- public void actionPerformed(ActionEvent args0) {
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[4].getText() == "")
- b[4].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 6*/
- l[5] = new ActionListener(){
- public void actionPerformed(ActionEvent args0) {
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[5].getText() == "")
- b[5].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 7*/
- l[6] = new ActionListener(){
- public void actionPerformed(ActionEvent args0) {
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[6].getText() == "")
- b[6].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 8*/
- l[7] = new ActionListener(){
- public void actionPerformed(ActionEvent args0){
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[7].getText() == "")
- b[7].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- /*button 9*/
- l[8] = new ActionListener(){
- public void actionPerformed(ActionEvent args0) {
- // TODO Auto-generated method stub
- if(playerTurn)
- {
- if(b[8].getText() == "")
- b[8].setText(x);
- changeTurns();
- }
- comTurn();
- checkforwin();
- }
- };
- }
- public void checkforwin()
- {
- if(b[0].getText() == x&&b[1].getText() == x&&b[2].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }else
- if(b[3].getText() == x&&b[4].getText() == x&&b[5].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }else
- if(b[6].getText() == x&&b[7].getText() == x&&b[8].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }else
- if(b[0].getText() == x&&b[3].getText() == x&&b[6].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }else
- if(b[1].getText() == x&&b[4].getText() == x&&b[7].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }else
- if(b[2].getText() == x&&b[5].getText() == x&&b[8].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }else
- if(b[0].getText() == x&&b[4].getText() == x&&b[8].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }else
- if(b[2].getText() == x&&b[4].getText() == x&&b[6].getText() == x)
- {
- JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
- endGame();
- }
- //endGame();
- if(b[0].getText() == o&&b[1].getText() == o&&b[2].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }else
- if(b[3].getText() == o&&b[4].getText() == o&&b[5].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }else
- if(b[6].getText() == o&&b[7].getText() == o&&b[8].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }else
- if(b[0].getText() == o&&b[3].getText() == o&&b[6].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }else
- if(b[1].getText() == o&&b[4].getText() == o&&b[7].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }else
- if(b[2].getText() == o&&b[5].getText() == o&&b[8].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }else
- if(b[0].getText() == o&&b[4].getText() == o&&b[8].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }else
- if(b[2].getText() == o&&b[4].getText() == o&&b[6].getText() == o)
- {
- JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
- endGame();
- }
- }
- public void addButtons()
- {
- addActionListeners();
- b = new JButton[9];
- for( int i = 0;i<9;i++)
- {
- b[i] = new JButton("");
- b[i].setSize(80,80);
- int xlocation = ((i%3)*85)+70;
- int ylocation = ((i/3)*85)+70;
- b[i].setLocation(xlocation,ylocation);
- panel.add(b[i]);
- b[i].addActionListener(l[i]);
- b[i].setFont(new Font("Arial",50,50));
- }
- }
- public void createFrame()
- {
- frame = new JFrame("Java Game");
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(400,400);
- frame.setResizable(false);
- /*JPanel*/
- panel = new JPanel();
- panel.setLayout(null);
- frame.add(panel);
- panel.setBackground(Color.ORANGE);
- /*Buttons*/
- addButtons();
- }
- public void comFirstTurns()
- {
- while(count == 0 && comTurn)
- {
- if(comTurn)
- {
- if(b[4].getText() == ""){
- b[4].setText(o);
- changeTurns();
- } else
- if(b[4].getText() == x){
- int random = (int)(Math.random()*4)+1;
- switch(random)
- {
- case 1:if(b[0].getText() == "")
- b[0].setText(o);
- changeTurns();
- break;
- case 2:if(b[2].getText() == "")
- b[2].setText(o);
- changeTurns();
- break;
- case 3:if(b[6].getText() == "")
- b[6].setText(o);
- changeTurns();
- break;
- case 4:if(b[8].getText() == "")
- b[8].setText(o);
- changeTurns();
- break;
- }
- } else {comWinLogic(); comStopPlayer();}
- }//computer Turn.
- count ++;
- }
- }
- public void comStopPlayer()
- {
- for(int i = 0 ;i<9 && comTurn == true;i++)
- {
- if(i == 0 || i == 3 || i == 6 )
- {
- if(b[i].getText() == x&&b[i+1].getText() == x)
- {
- if(b[i+2].getText() == "")
- {
- b[i+2].setText(o);
- changeTurns();
- }
- } else
- if(b[i].getText() == x&&b[i+2].getText() == x)
- if(b[i+1].getText() == "")
- {
- b[i+1].setText(o);
- changeTurns();
- }
- } else
- if(i == 2 || i == 5 || i == 8 )
- {
- if(b[i].getText() == x&&b[i-1].getText() == x)
- {
- if(b[i-2].getText() == "")
- {
- b[i-2].setText(o);
- changeTurns();
- }
- } else
- if(b[i].getText() == x&&b[i-2].getText() == x)
- if(b[i-1].getText() == "")
- {
- b[i-1].setText(o);
- changeTurns();
- }
- } else
- if(i == 0 || i == 2 || i == 3 )
- {
- if(b[i].getText() == x&&b[i+3].getText() == x)
- {
- if(b[i+6].getText() == "")
- {
- b[i+6].setText(o);
- changeTurns();
- }
- } else
- if(b[i].getText() == x&&b[i+6].getText() == x)
- if(b[i+3].getText() == "")
- {
- b[i+3].setText(o);
- changeTurns();
- }
- } else
- if(i == 6 || i == 7 || i == 8 )
- {
- if(b[i].getText() == x&&b[i-3].getText() == x)
- {
- if(b[i-6].getText() == "")
- {
- b[i-6].setText(o);
- changeTurns();
- }
- } else
- if(b[i].getText() == x&&b[i-6].getText() == x)
- if(b[i-3].getText() == "")
- {
- b[i-3].setText(o);
- changeTurns();
- }
- } else
- if(i == 0)
- {
- if(b[i].getText() == x&&b[i+4].getText() == x)
- {
- if(b[i+8].getText() == "")
- {
- b[i+8].setText(o);
- changeTurns();
- }
- } else
- if(b[i].getText() == x&&b[i+8].getText() == x)
- {
- if(b[i+4].getText() == "")
- {
- b[i+4].setText(o);
- changeTurns();
- }
- } else if(b[i+4].getText() == x&&b[i+8].getText() == x)
- if(b[i].getText() == "")
- {
- b[i].setText(o);
- changeTurns();
- }
- } else
- if(i == 2)
- {
- if(b[i].getText() == x &&b[i+2].getText() == x)
- {
- if(b[i+4].getText() == "")
- {
- b[i+4].setText(o);
- changeTurns();
- }
- } else
- if(b[i].getText() == x &&b[i+4].getText() == x)
- {
- if(b[i+2].getText() == "")
- {
- b[i+2].setText(o);
- changeTurns();
- }
- } else if(b[i+2].getText() == x&&b[i+4].getText() == x)
- if(b[i].getText() == "")
- {
- b[i].setText(o);
- changeTurns();
- }
- } else {comFirstTurns();}
- }
- }
- public void comTurn()
- {
- comWinLogic();
- comStopPlayer();
- comFirstTurns();
- }
- AITTT()
- {
- createFrame();
- res = new JButton("RESTART GAME");
- panel.add(res);
- res.setSize(130, 30);
- res.setLocation(132, 30);
- res.setForeground(Color.red);
- res.setFont(new Font("Tahoma Bold",12,12));
- res.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- for(int i = 0;i<9;i++)
- {
- b[i].setText("");
- }
- playerTurn = true;
- comTurn = false;
- count = 0;
- }
- });
- }//constractor
- public static void main(String args[])
- {
- new AITTT();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment