Wednesday, September 20, 2017

Code by Vasu, Tic Tac Toe in Java

This code is implemented and provided by Vasu.

It is a code to play the famous Tic Tac Toe game. When we run this code a GUI will be shown in order to play the game.

Note. Before using this code please create a "Java Project" in Eclipse IDE and save this class inside 
Game.java file

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Game {

JFrame frame;
JButton[] jButtons = new JButton[9] ;
String[] board = new String[9] ;
int player = 1 ;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game window = new Game();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});

}

public Game() {
for (int i = 0; i < board.length; i++) {
board[i] = String.valueOf(i) ;
}
initialize();
}

private void initialize() {
frame = new JFrame();
frame.setTitle("Player " + 1 + " turn");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(3, 3, 0, 0));


ActionListener listener = new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           if (e.getSource() instanceof JButton) {
            JButton button = (JButton) e.getSource();
            update(button);
           }
       }
   };

for (int i = 0; i < 9 ; i++) {
jButtons[i] = new JButton(String.valueOf(i));
jButtons[i].setFont(new Font("Arial", Font.PLAIN, 40));
jButtons[i].setForeground(new Color(255, 255, 255, 0));
jButtons[i].setFocusPainted(false);
jButtons[i].addActionListener(listener);
frame.getContentPane().add(jButtons[i]);
}

}


public void update(JButton button) {

int i , player_number = 0 ;

if(player%2 != 0) {
player_number = 1 ;
board[Integer.parseInt(button.getText())] = "O" ;
button.setText("O") ;
}else {
player_number = 2 ;
board[Integer.parseInt(button.getText())] = "X" ;
button.setText("X") ;
}


button.setEnabled(false);

i=checkwin();

player++;

if(i == 1) {
 JOptionPane.showMessageDialog(null,  "Player " + player_number +" win" , "YEAH!!",JOptionPane.INFORMATION_MESSAGE);
}else if(i == 0) {
JOptionPane.showMessageDialog(null,  "Match Draw");
}

if(player%2 != 0) {
player_number = 1 ;
}else {
player_number = 2 ;
}

frame.setTitle("Player " + player_number + " turn");

}

public int checkwin() {
if (board[0].equals(board[1]) && board[1].equals(board[2])) {
jButtons[0].setBackground(Color.CYAN);
jButtons[1].setBackground(Color.CYAN);
jButtons[2].setBackground(Color.CYAN);
return 1;
}
else if (board[3].equals(board[4]) && board[4].equals(board[5])) {
jButtons[3].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[5].setBackground(Color.CYAN);
return 1;
}
else if (board[6].equals(board[7]) && board[7].equals(board[8])) {
jButtons[6].setBackground(Color.CYAN);
jButtons[7].setBackground(Color.CYAN);
jButtons[8].setBackground(Color.CYAN);
return 1;
}
else if (board[0].equals(board[3]) && board[3].equals(board[6])) {
jButtons[0].setBackground(Color.CYAN);
jButtons[3].setBackground(Color.CYAN);
jButtons[6].setBackground(Color.CYAN);
return 1;
}
else if (board[1].equals(board[4]) && board[4].equals(board[7])) {
jButtons[1].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[7].setBackground(Color.CYAN);
return 1;
}
else if (board[2].equals(board[5]) && board[5].equals(board[8])) {
jButtons[2].setBackground(Color.CYAN);
jButtons[5].setBackground(Color.CYAN);
jButtons[8].setBackground(Color.CYAN);
return 1;
}
else if (board[0].equals(board[4]) && board[4].equals(board[8])) {
jButtons[0].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[8].setBackground(Color.CYAN);
return 1;
}
else if (board[2].equals(board[4]) && board[4].equals(board[6])) {
jButtons[2].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[6].setBackground(Color.CYAN);
return 1;
}
else if (!board[0].equals("0") && !board[1].equals("1")
                 && !board[2].equals("2") && !board[3].equals("3")
                && !board[4].equals("4") && !board[5].equals("5")
                && !board[6].equals("6") && !board[7].equals("7")
               && !board[8].equals("8") )

return 0;
else
return -1;
}


}

1 comment:

  1. sir please make your youtube channel and teach every concept related to programming language.

    ReplyDelete