Saturday, 17 October 2020

Semi-Graphical computer Tic-Tac-Toe game in Java

  •  In one of the older posts, I had shown how to make a program that allows the computer to play tic-tac-toe with the user. In this post, I am going to show how to update the game and make it semi-graphical.
  • This program might not run in IDE's supported by JRE's above JRE 7 as graphical usage isn't permitted in those JRE's due to a known Java bug.
  • Please pay attention to the text that appears in your IDE console after you run the code.
  • Type the following code in a class named Graphical_Tic_Tac_Toe:

import java.awt.Color;

import java.awt.GridLayout;

import java.util.Scanner;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

 

 

public class Graphical_Tic_Tac_Toe {

      

       @SuppressWarnings("resource")

       public static void main(String[] args) {

              loop:{

              int set[]= {0,0,0,0,0,0,0,0,0};

              JFrame frm=new JFrame();

              frm.setAlwaysOnTop(true);

              frm.setSize(200,250);

              frm.setLayout(new GridLayout(4, 3, 5, 5));

              frm.setVisible(true);

              JTextField txt=new JTextField();

              JTextField txtnotreal=new JTextField();

              JButton btn1=new JButton("1");JButton btn2=new JButton("2");

              JButton btn3=new JButton("3");JButton btn4=new JButton("4");

              JButton btn5=new JButton("5");JButton btn6=new JButton("6");

              JButton btn7=new JButton("7");JButton btn8=new JButton("8");

              JButton btn9=new JButton("9");

              JButton arr[]= {btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9};

              txt.setEditable(false);

              txt.setHorizontalAlignment(0);

              txtnotreal.setVisible(false);

              for(int i=0;i<9;i++) {frm.add(arr[i]);}

              frm.add(txtnotreal);

              frm.add(txt);

              String turn="";

              Scanner scan0=new Scanner(System.in);

              System.out.println("Welcome to Graphical_Tic_Tac_Toe");

              System.out.println("-------------------------------");

              System.out.println("Let's start");

              System.out.println("You can tell me your turn by writing only the cell number(1-9), nothing else.");

              System.out.println("Only legal play is allowed. System will self-exit if wrong turn is entered.");

              System.out.println("-------------------------------");

              me(7,arr,set);

              offer();

              turn=scan0.next();opp(turn,arr,set);

              if (turn.equals("1")){

                     me(9,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("8")){

                           me(3,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("5")){me(6,arr,set);cwin(txt);break loop;}

                           if (turn.equals("6")){me(5,arr,set);cwin(txt);break loop;}

                           if (turn!="5" & turn!="6"){me(5,arr,set);cwin(txt);break loop;}

                     }

                     if (turn!="8"){me(8,arr,set);cwin(txt);break loop;}

              }

              if (turn.equals("2")){

                     me(1,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("4")){

                           me(9,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("5")){me(8,arr,set);cwin(txt);break loop;}

                           if (turn.equals("8")){me(5,arr,set);cwin(txt);break loop;}

                           if (turn!="5" & turn!="8"){me(5,arr,set);cwin(txt);break loop;}

                     }

                     if (turn!="4"){me(4,arr,set);cwin(txt);break loop;}

              }

              if (turn.equals("3")){

                     me(1,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("4")){

                           me(9,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("5")){me(8,arr,set);cwin(txt);break loop;}

                           if (turn.equals("8")){me(5,arr,set);cwin(txt);break loop;}

                           if (turn!="5" & turn!="8"){me(5,arr,set);cwin(txt);break loop;}

                     }

                     if (turn!="4"){me(4,arr,set);cwin(txt);break loop;}

              }

              if (turn.equals("4")){

                     me(9,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("8")){   

                           me(3,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("5")){me(6,arr,set);cwin(txt);break loop;}

                           if (turn.equals("6")){me(5,arr,set);cwin(txt);break loop;}

                           if (turn!="5" & turn!="6"){me(5,arr,set);cwin(txt);break loop;}

                     }

                     if (turn!="8"){me(8,arr,set);cwin(txt);break loop;}

              }

              if (turn.equals("5")){                 

                     me(1,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("4")){                 

                           me(6,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("3")){me(9,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("2")){me(8,arr,set);cwin(txt);break loop;}

                           if (turn.equals("8")){me(2,arr,set);draw(txt);break loop;}

                           }

                           if (turn.equals("9")){me(3,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("2")){me(8,arr,set);draw(txt);break loop;}

                           if (turn.equals("8")){me(2,arr,set);cwin(txt);break loop;}

                           }

                           if (turn.equals("2")){me(8,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("3")){me(9,arr,set);cwin(txt);break loop;}

                           if (turn.equals("9")){me(3,arr,set);draw(txt);break loop;}

                           }

                           if (turn.equals("8")){me(2,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("3")){me(9,arr,set);draw(txt);break loop;}

                           if (turn.equals("9")){me(3,arr,set);cwin(txt);break loop;}

                           }                         

                     }

                     if (turn!="4"){me(4,arr,set);cwin(txt);break loop;}

              }

              if (turn.equals("6")){                 

                     me(1,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("4")){                        

                           me(5,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("3")){me(9,arr,set);cwin(txt);break loop;}

                           if (turn.equals("9")){me(3,arr,set);cwin(txt);break loop;}

                           if (turn!="3" & turn!="9"){me(3,arr,set);cwin(txt);break loop;}

                     }

                     if (turn!="4"){me(4,arr,set);cwin(txt);break loop;}

              }

              if (turn.equals("8")){                 

                     me(1,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("4")){                        

                            me(3,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("5")){me(2,arr,set);cwin(txt);break loop;}

                           if (turn.equals("2")){me(5,arr,set);cwin(txt);break loop;}

                           if (turn!="5" & turn!="2"){me(5,arr,set);cwin(txt);break loop;}

                     }

                     if (turn!="4"){me(4,arr,set);cwin(txt);break loop;}

              }

              if (turn.equals("9")){

                     me(1,arr,set);

                     offer();

                     turn=scan0.next();opp(turn,arr,set);

                     if (turn.equals("4")){                        

                           me(3,arr,set);

                           offer();

                           turn=scan0.next();opp(turn,arr,set);

                           if (turn.equals("5")){me(2,arr,set);cwin(txt);break loop;}

                           if (turn.equals("2")){me(5,arr,set);cwin(txt);break loop;}

                           if (turn!="5" & turn!="2"){me(5,arr,set);cwin(txt);break loop;}

                     }

                     if (turn!="4"){me(4,arr,set);cwin(txt);break loop;}

              }                   

        }

 

       }

       private static void offer() {

              System.out.println("Your turn:");

       }

       private static void me(int i, JButton[] arr, int[] set) {

              set[i-1]=1;

              arr[i-1].setText("X");arr[i-1].setBackground(Color.orange);System.out.println("Computer in "+i);

              }

       private static void opp(String turn, JButton[] arr, int[] set) {

              if (set[(int)(turn.charAt(0))-49]==1) {System.out.println("Wrong turn");System.exit(0);}

              arr[(int)(turn.charAt(0))-49].setText("O");arr[(int)(turn.charAt(0))-49].setBackground(Color.green);

              set[(int)(turn.charAt(0))-49]=1;

              }

       private static void cwin(JTextField txt) {

              System.out.println("Computer won.");txt.setText("You lost");

              }

       private static void draw(JTextField txt) {

              System.out.println("Match draw.");txt.setText("Draw");

       }

 

 

}



  • Run the program.
  • Please remember, the file is only semi-graphical, not complete GUI

1 comment:

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.