Close
Login to Your Account
Faadooengineers

Results 1 to 3 of 3

Thread: Java Source code for TicTacToe

  1. #1

    Txt 32 Java Source code for TicTacToe

    Here is the source code for the TicTacToe ...

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class TicTacToe extends JFrame implements ActionListener {

    private int[][] winCombinations = new int[][] {
    {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
    {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
    {0, 4, 8}, {2, 4, 6}
    };
    Font f1 = new Font("Times New Roman", 1, 72);

    JMenuBar bar;
    JMenu jm1;
    JMenuItem jt1,jt2;
    JButton buttons[] = new JButton[9];
    int flg = 0;
    String letter = "";
    boolean win = false;

    public TicTacToe(){

    setLayout(new GridLayout(1,1));

    bar=new JMenuBar();
    jm1=new JMenu("File");
    jt1=new JMenuItem("New");
    jt2=new JMenuItem("Exit");

    add(bar);
    jm1.add(jt1);
    jm1.addSeparator();
    jm1.add(jt2);
    add(jm1);
    bar.add(jm1);


    setVisible(true);
    setSize(400,400);

    setLayout(new GridLayout(3,3));


    for(int i=0; i<=8; i++){
    buttons[i] = new JButton();

    add(buttons[i]);
    buttons[i].addActionListener(this);
    }


    setVisible(true);
    }



    public void actionPerformed(ActionEvent a) {
    flg++;


    if(flg % 2 == 0){
    letter = "O";
    } else {
    letter = "X";
    }


    JButton pressedButton = (JButton)a.getSource();
    pressedButton.setFont(f1);
    pressedButton.setBackground(Color.cyan);

    pressedButton.setText(letter);

    pressedButton.setEnabled(false);


    for(int i=0; i<=7; i++){
    if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
    buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
    buttons[winCombinations[i][0]].getText() != ""){
    win = true;
    }
    }


    if(win == true){
    JOptionPane.showMessageDialog(null, letter + " wins the game!");
    System.exit(0);
    } else if(flg == 9 && win == false){
    JOptionPane.showMessageDialog(null, "The game was tie!");
    System.exit(0);
    }
    }

    public static void main(String[] args){
    TicTacToe starter = new TicTacToe();
    }
    }
    if you have any query, please contact through PM.

    Attached Files for Direct Download
      File Name:
      File Size:
      2.0 KB
      Total Downloads:
      442
    * Click on the 'file icon' or 'file name' to start downloading
    Last edited by Sakshi Dutta; 10th February 2011 at 07:15 PM. Reason: email sharing not allowed, make your threads more presentable!

  2. #2

    Re: Java Source code for TicTacToe

    dere is error in this codeing.............

  3. #3

    Re: Java Source code for TicTacToe

    can you please post java code for hitori algorithm

    ---------- Post added at 01:43 PM ---------- Previous post was at 01:41 PM ----------

    /home/nick/Downloads/TicTacToe1.txt

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Share anywhere and get download.

50%