Tic Tac Toe Game in JAVA

Long time ago I read that learning programming is so fun, if you didn't find learning programming fun then you must not interested or your teacher don't know how to teach the programming. There is also written that the programming teacher must be working on industry so that he/she could be able to teach you the way how to overcome the programming obstacles. At the beginning phase of studying programming language, I used to worry about what if i couldn't able to write the code clearly and understandable. I always used to worry about it. But one day my facilitator said that now you are developing your career in software industry. Don't ever worry about the clear code. Just you do mistake as many times as it takes to get the task done. Then after the task is done, be clear what you did, what you learned from mistakes and what problem is solved. Then always make a habit of writing comment in the source code. There are a lot of things that he told us, these are few. 

At the beginning phase, to learn coding was too challenging, and another thing that I realized is that without proper objective doing coding is worthless. So, when ever you start your coding, at first define what you are going to do. After clear vision only we can be able to complete the task otherwise you will be roaming in source code only and you find that coding is boring and irritating. 

Tic Tac Toe is simple game which is basically used or made to understand the flow of action event in programming. It is one of the easiest way to understand the layout and other components in programming language. Here I have made it using Java programming language. Try to understand the flow of coding and try to understand. If you didn't understand than you can ask me where you didn't understand. Here is the source code of it. 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package TicTacToe;

import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author hp
 */
public class TicTacToe extends JFrame implements ActionListener{
   
    //Labe for displaying the following items
    Label turn = new Label("Turn:");
    Label won = new Label();
    Label setPlayer = new Label("X");
   
    //JButtons used to play(interact) the game
    JButton btn1 = new JButton("");
    JButton btn2 = new JButton("");
    JButton btn3 = new JButton("");
    JButton btn4 = new JButton("");
    JButton btn5 = new JButton("");
    JButton btn6 = new JButton("");
    JButton btn7 = new JButton("");
    JButton btn8 = new JButton("");
    JButton btn9 = new JButton("");
   
    JButton newGame = new JButton("New Game");
    JButton exitGame = new JButton("Exit Game");
   
    //used this panel to insert the JButtons
    JPanel panel = new JPanel();
   
    //used this panel to insert details like turn, players
    JPanel panel1 = new JPanel();
   
    //used to main method to execute the program
    public static void main(String args[])
    {
        new TicTacToe();
    }
   
    //used default method i.e. constructor
    TicTacToe()
    {
        setPlayer.setForeground(Color.green);
        setSize(1000, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setVisible(true);
       
        //set the layout of panel1
        panel1.setLayout(new FlowLayout());
       
        //size of font
        Font font = new Font("Times New Roman", Font.BOLD, 50);
       
        turn.setFont(font);
        panel1.add(turn);
       
        setPlayer.setFont(font);
        panel1.add(setPlayer);
       
        won.setFont(font);
        panel1.add(won);
       
       
        //set JButton to start new game or exit the game;
        newGame.setFont(font);
        panel1.add(newGame);
       
        exitGame.setFont(font);
        panel1.add(exitGame);
       
        //added panel1 inside borderlayout
        add(panel1, BorderLayout.NORTH);
       
       
        // set the layout of the panel i.e gridlayout to plot the JButtons in equal row and column
        panel.setLayout(new GridLayout(3, 3, 5, 5));
        setVisible(true);
        btn1.setFont(font);
        panel.add(btn1);
        btn2.setFont(font);
        panel.add(btn2);
        btn3.setFont(font);
        panel.add(btn3);
        btn4.setFont(font);
        panel.add(btn4);
        btn5.setFont(font);
        panel.add(btn5);
        btn6.setFont(font);
        panel.add(btn6);
        btn7.setFont(font);
        panel.add(btn7);
        btn8.setFont(font);
        panel.add(btn8);
        btn9.setFont(font);
        panel.add(btn9);
       
        //added panel inside borderlayout
        add(panel, BorderLayout.CENTER);
       
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
       
        newGame.addActionListener(this);
        exitGame.addActionListener(this);
    }
   
    //method to check the players turn and set text to JButton
    private String setValue()
    {
       
        if(setPlayer.getText()=="X")
        {
            setPlayer.setText("O");
            return "X";
        }
        else
        {
            setPlayer.setText("X");
            return "O";
        }
    }

    public void newGame()
    {
           setPlayer.setText("X");
             btn1.setText("");
             btn1.setEnabled(true);
             btn2.setText("");
             btn2.setEnabled(true);
             btn3.setText("");
             btn3.setEnabled(true);
             btn4.setText("");
             btn4.setEnabled(true);
             btn5.setText("");
             btn5.setEnabled(true);
             btn6.setText("");
             btn6.setEnabled(true);
             btn7.setText("");
             btn7.setEnabled(true);
             btn8.setText("");
             btn8.setEnabled(true);
             btn9.setText("");
             btn9.setEnabled(true);
    }
    @Override
    public void actionPerformed(ActionEvent ae) {
         if(ae.getSource()==btn1)
         {
            if(btn1.getText()=="")
            {
                String v = setValue();
                btn1.setText(v);
                btn1.setEnabled(false);
            }
         }
         if(ae.getSource()==btn2)
         {
             if(btn2.getText()=="")
             {
                String v = setValue();
                btn2.setText(v);
                btn2.setEnabled(false);
             }
         }
         if(ae.getSource()==btn3)
         {
             if(btn3.getText()=="")
             {
                String v = setValue();
                btn3.setText(v);
                btn3.setEnabled(false);
             }
         }
         if(ae.getSource()==btn4)
         {
             if(btn4.getText()=="")
             {
                String v = setValue();
                btn4.setText(v);
                btn4.setEnabled(false);
             }
         }
         if(ae.getSource()==btn5)
         {
             if(btn5.getText()=="")
             {
                String v = setValue();
                btn5.setText(v);
                btn5.setEnabled(false);
             }
         }
         if(ae.getSource()==btn6)
         {
             if(btn6.getText()=="")
             {
                String v = setValue();
                btn6.setText(v);
                btn6.setEnabled(false);
             }
         }
         if(ae.getSource()==btn7)
         {
             if(btn7.getText()=="")
             {
                String v = setValue();
                btn7.setText(v);
                btn7.setEnabled(false);
             }
         }
         if(ae.getSource()==btn8)
         {
             if(btn8.getText()=="")
             {
                String v = setValue();
                btn8.setText(v);
                btn8.setEnabled(false);
             }
         }
         if(ae.getSource()==btn9)
         {
             if(btn9.getText()=="")
             {
                String v = setValue();
                btn9.setText(v);
                btn9.setEnabled(false);
             }
         }
//          if all buttons are X in first row
        while(btn1.getText().equals("X") && btn2.getText().equals("X") && btn3.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
             newGame();
              break;
         }
        //          if all buttons are X in second row
        while(btn4.getText().equals("X") && btn5.getText().equals("X") && btn6.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
             newGame();
              break;
         }
       
        //          if all buttons are X in third row
        while(btn7.getText().equals("X") && btn8.getText().equals("X") && btn9.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
             newGame();
              break;
         }
       
        //          if all buttons are O in first row
        while(btn1.getText().equals("O") && btn2.getText().equals("O") && btn3.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
         //          if all buttons are O in second row
        while(btn4.getText().equals("O") && btn5.getText().equals("O") && btn6.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
            //          if all buttons are O in third row
        while(btn7.getText().equals("O") && btn8.getText().equals("O") && btn9.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
        //          if all buttons are X in first column
        while(btn1.getText().equals("X") && btn4.getText().equals("X") && btn7.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
             newGame();
              break;
         }
       
         //          if all buttons are X in second column
        while(btn2.getText().equals("X") && btn5.getText().equals("X") && btn8.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
             newGame();
              break;
         }
       
         //          if all buttons are X in third column
        while(btn3.getText().equals("X") && btn6.getText().equals("X") && btn9.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
             newGame();
              break;
         }
       
            //          if all buttons are O in first column
        while(btn1.getText().equals("O") && btn4.getText().equals("O") && btn7.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
         //          if all buttons are O in second column
        while(btn2.getText().equals("O") && btn5.getText().equals("O") && btn8.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
        //          if all buttons are O in third column
        while(btn3.getText().equals("O") && btn6.getText().equals("O") && btn9.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
        //          if all buttons are O in cross  position
        while(btn1.getText().equals("O") && btn5.getText().equals("O") && btn9.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
        //          if all buttons are O in cross  position
        while(btn3.getText().equals("O") && btn5.getText().equals("O") && btn7.getText().equals("O")){
             JOptionPane.showMessageDialog(null, "Player O wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
         //          if all buttons are X in cross  position
        while(btn1.getText().equals("X") && btn5.getText().equals("X") && btn9.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
        //          if all buttons are X in cross  position
        while(btn3.getText().equals("X") && btn5.getText().equals("X") && btn7.getText().equals("X")){
             JOptionPane.showMessageDialog(null, "Player X wont the game :)", "Game Result", JOptionPane.INFORMATION_MESSAGE);
            newGame();
              break;
         }
       
//        if game is draw
//        while(btn1.isSelected() && btn2.isSelected() && btn3.isSelected() && btn4.isSelected() && btn5.isSelected() && btn6.isSelected() && btn7.isSelected() && btn8.isSelected() && btn9.isSelected())
//        {
//            JOptionPane.showMessageDialog(null, "No one won the game.", "Game Result", JOptionPane.INFORMATION_MESSAGE);
//            newGame();
//              break;
//        }
       
         //To start new game
         if(ae.getSource()==newGame)
         {
             newGame();
         }
         //To exit the game
         if(ae.getSource()==exitGame)
         {
             System.exit(0);
         }
    }
   
   
}


you can simply copy and paste it in IDE and run it. But remember, be sure that after understanding only you do copy paste, otherwise you won't learn anything. 


Comments

Popular posts from this blog

Logical operator in JAVA

Class and Object in Java

Loop Structure