Jump to content




Recent Status Updates

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Java:Tutorial - Tic-Tac-Toe

tic-tac-toe

  • Please log in to reply
60 replies to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 11 January 2007 - 12:11 PM

Title: Tic-Tac-Toe: Contemplation to Compilation

Many games you play on the internet have been crafted from Java. This tutorial, inspired by another post in this forum, will show you step by step how to create a two person Tic-Tac-Toe game in Java.

Prerequisites
This is a fairly intermediate tutorial, therefore if you are going to understand everything I write, you should read all my previous tutorials, in particular, those dealing with graphical user interfaces.

Solution
The first step you must take before starting any programming project is to understand your problem. Understanding your problem will allow you to create a logically correct program bug free, so before we delve into the code let me tell you what I was thinking when I presented myself with this problem.

 

Java Source Code: Tic-Tac-Toe Game

The first question I asked myself is: “What is tic-tac-toe?”
It’s a game consisting of a 3 by 3 grid. Each player (represented by X and O) takes alternating turns trying to earn three of their letters in a row horizontally, vertically, or diagonally.

At this point I knew I needed to make a grid layout 3 by 3. My idea was to create 9 buttons in each of the grid boxes. But buttons would respond when clicked with either a X or an O depending on who’s turn it was.

package mytictactoe;

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


public class TicTacToeV1 implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button1 = new JButton("x");
private JButton button2 = new JButton("x");
private JButton button3 = new JButton("x");
private JButton button4 = new JButton("x");
private JButton button5 = new JButton("x");
private JButton button6 = new JButton("x");
private JButton button7 = new JButton("x");
private JButton button8 = new JButton("x");
private JButton button9 = new JButton("x");



public TicTacToeV1(){

/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);

/*Add The Action Listener To The Buttons*/
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);

/*Make The Window Visible*/
window.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
System.out.println("A button was pressed.");

}

public static void main(String[] args){
new TicTacToeV1();
}
}

 

Tic-Tac-Toe in Python

 

What you see is a window with nine buttons labeled X. Each time the button is pressed it invokes the actionPerformed method and prints to the consol “A buttons was pressed.” My next step was to, when the program was started, have the buttons blank, and when pressed, label them X. To do that I set the label to null when the object was first created in the instance variables. Then in the actionPerformed method, I had the button label change to X if it was pressed by doing this

package mytictactoe;

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


public class TicTacToeV1 implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button1 = new JButton("");
private JButton button2 = new JButton("");
private JButton button3 = new JButton("");
private JButton button4 = new JButton("");
private JButton button5 = new JButton("");
private JButton button6 = new JButton("");
private JButton button7 = new JButton("");
private JButton button8 = new JButton("");
private JButton button9 = new JButton("");

public TicTacToeV1(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);

/*Add The Action Listener To The Buttons*/
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);

/*Make The Window Visible*/
window.setVisible(true);
}

public void actionPerformed(ActionEvent a) {

/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText("X");
} else if(a.getSource() == button2){
button2.setText("X");
} else if(a.getSource() == button3){
button3.setText("X");
} else if(a.getSource() == button4){
button4.setText("X");
} else if(a.getSource() == button5){
button5.setText("X");
} else if(a.getSource() == button6){
button6.setText("X");
} else if(a.getSource() == button7){
button7.setText("X");
} else if(a.getSource() == button8){
button8.setText("X");
} else if(a.getSource() == button9){
button9.setText("X");
}
}

public static void main(String[] args){
new TicTacToeV1();
}
}

 

The next problem I had to surmount was to determine whose turn it was. I did this by creating a hidden variable that increments each time a button is pressed.

 

Implementing hill climbing search for tic-tac-toe demo in java



package mytictactoe;

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


public class TicTacToeV1 implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button1 = new JButton("");
private JButton button2 = new JButton("");
private JButton button3 = new JButton("");
private JButton button4 = new JButton("");
private JButton button5 = new JButton("");
private JButton button6 = new JButton("");
private JButton button7 = new JButton("");
private JButton button8 = new JButton("");
private JButton button9 = new JButton("");
private String letter = "";
private int count = 0;

public TicTacToeV1(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);

/*Add The Action Listener To The Buttons*/
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);

/*Make The Window Visible*/
window.setVisible(true);
}

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

/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter = "X";
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "O";
}

/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText(letter);
} else if(a.getSource() == button2){
button2.setText(letter);
} else if(a.getSource() == button3){
button3.setText(letter);
} else if(a.getSource() == button4){
button4.setText(letter);
} else if(a.getSource() == button5){
button5.setText(letter);
} else if(a.getSource() == button6){
button6.setText(letter);
} else if(a.getSource() == button7){
button7.setText(letter);
} else if(a.getSource() == button8){
button8.setText(letter);
} else if(a.getSource() == button9){
button9.setText(letter);
}
}

public static void main(String[] args){
new TicTacToeV1();
}
}
The count integer increments by one each time a button is clicked. I also created a letter string that would vary depending on count’s value. If count is 1,3,5,7 or 9 letter is defined as X if count is 2,4,6,8 or 10 count is defined as O. I found out that even if a button is pressed, the button is still active causing the button to change to both O and X. Setting the setEnable() method to false fixed this.

package mytictactoe;

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


public class TicTacToeV1 implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button1 = new JButton("");
private JButton button2 = new JButton("");
private JButton button3 = new JButton("");
private JButton button4 = new JButton("");
private JButton button5 = new JButton("");
private JButton button6 = new JButton("");
private JButton button7 = new JButton("");
private JButton button8 = new JButton("");
private JButton button9 = new JButton("");
private String letter = "";
private int count = 0;

public TicTacToeV1(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);

/*Add The Action Listener To The Buttons*/
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);

/*Make The Window Visible*/
window.setVisible(true);
}

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

/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter = "X";
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "O";
}

/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText(letter);
button1.setEnabled(false);
} else if(a.getSource() == button2){
button2.setText(letter);
button2.setEnabled(false);
} else if(a.getSource() == button3){
button3.setText(letter);
button3.setEnabled(false);
} else if(a.getSource() == button4){
button4.setText(letter);
button4.setEnabled(false);
} else if(a.getSource() == button5){
button5.setText(letter);
button5.setEnabled(false);
} else if(a.getSource() == button6){
button6.setText(letter);
button6.setEnabled(false);
} else if(a.getSource() == button7){
button7.setText(letter);
button7.setEnabled(false);
} else if(a.getSource() == button8){
button8.setText(letter);
button8.setEnabled(false);
} else if(a.getSource() == button9){
button9.setText(letter);
button9.setEnabled(false);
}

}

public static void main(String[] args){
new TicTacToeV1();
}
}
Now we need to determine the winner. I created some logical statements to determine each possible case of winning. Ecentially if button1, button2, and button3 were all the same value, that person won, but you also have to account for the fact that when the program starts each button is null causing the winner to be null. However null is not a player and in your logical statements you must account for this. I created a Boolean win instance variable set to false by default and then added these logical statements to my actionPerformed method.

if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
win = true;
}
else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
win = true;
}
else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
win = true;
}

//virticle wins
else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
win = true;
}
else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
win = true;
}
else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
win = true;
}

//diagonal wins
else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
win = true;
}
else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
win = true;
}
else {
win = false;
}
Which accounts for every possible win. Now that we have determined the winner, lets add a popup box announcing the winner. All we have to do is create a if statement and if there is a winner announce it, if there is no winner after nine clicks announce there is a tie. To create a popup box we are going to use showMessageDialog provided to us in the JOptionPane package.

if(win == true){
JOptionPane.showMessageDialog(null, letter + " WINS!");
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "Tie Game!");
}
My final code looks like this:

package mytictactoe;

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


public class TicTacToeV1 implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button1 = new JButton("");
private JButton button2 = new JButton("");
private JButton button3 = new JButton("");
private JButton button4 = new JButton("");
private JButton button5 = new JButton("");
private JButton button6 = new JButton("");
private JButton button7 = new JButton("");
private JButton button8 = new JButton("");
private JButton button9 = new JButton("");
private String letter = "";
private int count = 0;
private boolean win = false;

public TicTacToeV1(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);

/*Add The Action Listener To The Buttons*/
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);

/*Make The Window Visible*/
window.setVisible(true);
}

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

/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter = "X";
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "O";
}

/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText(letter);
button1.setEnabled(false);
} else if(a.getSource() == button2){
button2.setText(letter);
button2.setEnabled(false);
} else if(a.getSource() == button3){
button3.setText(letter);
button3.setEnabled(false);
} else if(a.getSource() == button4){
button4.setText(letter);
button4.setEnabled(false);
} else if(a.getSource() == button5){
button5.setText(letter);
button5.setEnabled(false);
} else if(a.getSource() == button6){
button6.setText(letter);
button6.setEnabled(false);
} else if(a.getSource() == button7){
button7.setText(letter);
button7.setEnabled(false);
} else if(a.getSource() == button8){
button8.setText(letter);
button8.setEnabled(false);
} else if(a.getSource() == button9){
button9.setText(letter);
button9.setEnabled(false);
}

/*Determine who won*/
//horizontal wins
if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
win = true;
}
else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
win = true;
}
else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
win = true;
}

//virticle wins
else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
win = true;
}
else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
win = true;
}
else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
win = true;
}

//diagonal wins
else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
win = true;
}
else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
win = true;
}
else {
win = false;
}

/*Show a dialog if someone wins or the game is tie*/
if(win == true){
JOptionPane.showMessageDialog(null, letter + " WINS!");
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "Tie Game!");
}
}

public static void main(String[] args){
new TicTacToeV1();
}
}

 

In a future tutorial I will clean up this code as there is a lot of unneeded repetition and I may even add AI for a single user to play.

 

Looking for more Java topics on tic-tac-toe?

How to use SelectionListener in our Tic-Tac-Toe game

Java Source Code: Tic-Tac-Toe Game

TicTacToe Winning Solution

 

 

 


Edited by Roger, 26 February 2013 - 03:32 PM.

  • -1

#2 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 11 January 2007 - 12:12 PM

Now in the tutorial above there is a lot of repeated code that can be accounted for by loops an arrays. The first thing I did to make the code more efficient was add the JButtons to an array and loop the array to add the JButton objects to the frame. So lets make an array

private JButton buttons[] = new JButton[9];
Then replacing the lines that look like this
private JButton button1 = new JButton("");
and this
button1.addActionListener(this);
with this
for(int i=0; i<=8; i++){
    buttons[i] = new JButton();
    window.add(buttons[i]);
    buttons[i].addActionListener(this);
}
Now we've just replaced 27 lines of code with 5. How about making the win conditions more efficient? Well I created an array that listed all the possible win combonations:
private int[][] winCombinations = new int[][] {
    {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
    {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
    {0, 4, 8}, {2, 4, 6}             //diagonal wins
};
Rather than creating a condition for each possible win, we can use our winCombinations in conjunction with a loop like so:
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;
    }
}
At this point I wasn't satisfied with how I determined who's turn it was. Although there is only a possibility of 4 turns and the code in version 1 works fine, I felt there was a better way to calculate it, and this is what I came up with
if(count % 2 == 0){
    letter = "O";
} else {
    letter = "X";
}
The % is the remainder operator, meaning if the count value is an even number, when divided by 2 the remainder is 0 which satisfies the first condition, if the number is odd the remainder is 1 causing the condition to branch to the else condition. Finally I came up with a better way to write the X's and O's to the button which looks like this:
JButton pressedButton = (JButton)a.getSource(); 
 pressedButton.setText(letter);
 pressedButton.setEnabled(false);
So using a little lateral thinking and making full use of Java's capabilities (loops, arrays, ect...) you can change your code from 140 lines to 80 lines! Here is what my version 2 looks like

package mytictactoe;

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

public class TicTacToeV2 implements ActionListener {
    /*Instance Variables*/
    private int[][] winCombinations = new int[][] {
            {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
            {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
            {0, 4, 8}, {2, 4, 6}             //diagonal wins
        };
    private JFrame window = new JFrame("Tic-Tac-Toe");
    private JButton buttons[] = new JButton[9];
    private int count = 0;
    private String letter = "";
    private boolean win = false;

    public TicTacToeV2(){
    /*Create Window*/
    window.setSize(300,300);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new GridLayout(3,3));
    
    /*Add Buttons To The Window*/
    for(int i=0; i<=8; i++){
        buttons[i] = new JButton();
        window.add(buttons[i]);
        buttons[i].addActionListener(this);
    }
    
    /*Make The Window Visible*/
    window.setVisible(true);
    }
    
    /**
     When an object is clicked, perform an action.
     @param a action event object
     */
    public void actionPerformed(ActionEvent a) {
        count++;
        
        /*Calculate whose turn it is*/
        if(count % 2 == 0){
            letter = "O";
        } else {
            letter = "X";
        }

        /*Write the letter to the button and deactivate it*/
         JButton pressedButton = (JButton)a.getSource(); 
         pressedButton.setText(letter);
         pressedButton.setEnabled(false);
        
        /*Determine who won*/
        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;
            }
        }
        
        /*Show a dialog when game is over*/
        if(win == true){
            JOptionPane.showMessageDialog(null, letter + " wins the game!");
            System.exit(0);
        } else if(count == 9 && win == false){
            JOptionPane.showMessageDialog(null, "The game was tie!");
            System.exit(0);
        }        
    }
    
    public static void main(String[] args){
        TicTacToeV2 starter = new TicTacToeV2();
    }
}

Edited by John, 18 July 2010 - 01:38 PM.

  • 0

#3 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 11 January 2007 - 12:12 PM

I'll explain this code later:

Tic-Tac-Toe with AI

package mytictactoe;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;

public class TicTacToeV4 implements ActionListener {
    /*Instance Variables*/
    private int[][] winCombinations = new int[][] {
            {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, //horizontal wins
            {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, //virticle wins
            {1, 5, 9}, {3, 5, 7}             //diagonal wins
        };
    private JFrame window = new JFrame("Tic-Tac-Toe");
    private JButton buttons[] = new JButton[10];
    private int count = 0;
    private String letter = "";
    private boolean win = false;
    
    public TicTacToeV4(){
    /*Create Window*/
    window.setPreferredSize(new Dimension(300,300));
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new GridLayout(3,3));
    
    /*Add Buttons To The Window*/
    for(int i = 1; i<=9; i++){
        buttons[i] = new JButton();
        window.add(buttons[i]);
        buttons[i].addActionListener(this);
    }
    
    /*Make The Window Visible*/
    window.setVisible(true);
    window.pack();
    }
    
    public void actionPerformed(ActionEvent a) {


        /*Write the letter to the button and deactivate it*/
        for(int i = 1; i<= 9; i++){
            if(a.getSource() == buttons[i]){
                buttons[i].setText("X");
                buttons[i].setEnabled(false);
            }
        }
        
        count++;        
        AI();

    }
    
    public void AI(){
        count++;
        if(buttons[1].getText().equals("O") && buttons[2].getText().equals("O") && buttons[3].getText().equals("")){
            buttons[3].setText("O");
            buttons[3].setEnabled(false);
        } else if(buttons[4].getText().equals("O") && buttons[5].getText().equals("O") && buttons[6].getText().equals("")){
            buttons[6].setText("O");
            buttons[6].setEnabled(false);
        } else if(buttons[7].getText().equals("O") && buttons[8].getText().equals("O") && buttons[9].getText().equals("")){
            buttons[9].setText("O");
            buttons[9].setEnabled(false);                
        } 
        
        else if(buttons[2].getText().equals("O") && buttons[3].getText().equals("O") && buttons[1].getText().equals("")){
            buttons[1].setText("O");
            buttons[1].setEnabled(false);                
        } else if(buttons[5].getText().equals("O") && buttons[6].getText().equals("O") && buttons[4].getText().equals("")){
            buttons[4].setText("O");
            buttons[4].setEnabled(false);                
        } else if(buttons[8].getText().equals("O") && buttons[9].getText().equals("O") && buttons[7].getText().equals("")){
            buttons[7].setText("O");
            buttons[7].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("O") && buttons[3].getText().equals("O") && buttons[2].getText().equals("")){
            buttons[2].setText("O");
            buttons[2].setEnabled(false);                
        } else if(buttons[4].getText().equals("O") && buttons[6].getText().equals("O") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        } else if(buttons[7].getText().equals("O") && buttons[9].getText().equals("O") && buttons[8].getText().equals("")){
            buttons[8].setText("O");
            buttons[8].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("O") && buttons[4].getText().equals("O") && buttons[7].getText().equals("")){
            buttons[7].setText("O");
            buttons[7].setEnabled(false);                
        } else if(buttons[2].getText().equals("O") && buttons[5].getText().equals("O") && buttons[8].getText().equals("")){
            buttons[4].setText("O");
            buttons[4].setEnabled(false);                
        } else if(buttons[3].getText().equals("O") && buttons[6].getText().equals("O") && buttons[9].getText().equals("")){
            buttons[9].setText("O");
            buttons[9].setEnabled(false);                
        }
        
        else if(buttons[4].getText().equals("O") && buttons[7].getText().equals("O") && buttons[1].getText().equals("")){
            buttons[1].setText("O");
            buttons[1].setEnabled(false);                
        } else if(buttons[5].getText().equals("O") && buttons[8].getText().equals("O") && buttons[2].getText().equals("")){
            buttons[2].setText("O");
            buttons[2].setEnabled(false);                
        } else if(buttons[6].getText().equals("O") && buttons[9].getText().equals("O") && buttons[3].getText().equals("")){
            buttons[3].setText("O");
            buttons[3].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("O") && buttons[7].getText().equals("O") && buttons[4].getText().equals("")){
            buttons[4].setText("O");
            buttons[4].setEnabled(false);                
        } else if(buttons[2].getText().equals("O") && buttons[8].getText().equals("O") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        } else if(buttons[3].getText().equals("O") && buttons[9].getText().equals("O") && buttons[6].getText().equals("")){
            buttons[6].setText("O");
            buttons[6].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("O") && buttons[5].getText().equals("O") && buttons[9].getText().equals("")){
            buttons[9].setText("O");
            buttons[9].setEnabled(false);                
        } else if(buttons[5].getText().equals("O") && buttons[9].getText().equals("O") && buttons[1].getText().equals("")){
            buttons[1].setText("O");
            buttons[1].setEnabled(false);                
        } else if(buttons[1].getText().equals("O") && buttons[9].getText().equals("O") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        }
        
        else if(buttons[3].getText().equals("O") && buttons[5].getText().equals("O") && buttons[7].getText().equals("")){
            buttons[7].setText("O");
            buttons[7].setEnabled(false);                
        } else if(buttons[7].getText().equals("O") && buttons[5].getText().equals("O") && buttons[3].getText().equals("")){
            buttons[3].setText("O");
            buttons[3].setEnabled(false);                
        } else if(buttons[7].getText().equals("O") && buttons[3].getText().equals("O") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("X") && buttons[2].getText().equals("X") && buttons[3].getText().equals("")){
            buttons[3].setText("O");
            buttons[3].setEnabled(false);
        } else if(buttons[4].getText().equals("X") && buttons[5].getText().equals("X") && buttons[6].getText().equals("")){
            buttons[6].setText("O");
            buttons[6].setEnabled(false);                
        } else if(buttons[7].getText().equals("X") && buttons[8].getText().equals("X") && buttons[9].getText().equals("")){
            buttons[9].setText("O");
            buttons[9].setEnabled(false);                
        } 
        
        else if(buttons[2].getText().equals("X") && buttons[3].getText().equals("X") && buttons[1].getText().equals("")){
            buttons[1].setText("O");
            buttons[1].setEnabled(false);                
        } else if(buttons[5].getText().equals("X") && buttons[6].getText().equals("X") && buttons[4].getText().equals("")){
            buttons[4].setText("O");
            buttons[4].setEnabled(false);                
        } else if(buttons[8].getText().equals("X") && buttons[9].getText().equals("X") && buttons[7].getText().equals("")){
            buttons[7].setText("O");
            buttons[7].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("X") && buttons[3].getText().equals("X") && buttons[2].getText().equals("")){
            buttons[2].setText("O");
            buttons[2].setEnabled(false);                
        } else if(buttons[4].getText().equals("X") && buttons[6].getText().equals("X") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        } else if(buttons[7].getText().equals("X") && buttons[9].getText().equals("X") && buttons[8].getText().equals("")){
            buttons[8].setText("O");
            buttons[8].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("X") && buttons[4].getText().equals("X") && buttons[7].getText().equals("")){
            buttons[7].setText("O");
            buttons[7].setEnabled(false);                
        } else if(buttons[2].getText().equals("X") && buttons[5].getText().equals("X") && buttons[8].getText().equals("")){
            buttons[8].setText("O");
            buttons[8].setEnabled(false);                
        } else if(buttons[3].getText().equals("X") && buttons[6].getText().equals("X") && buttons[9].getText().equals("")){
            buttons[9].setText("O");
            buttons[9].setEnabled(false);                
        }
        
        else if(buttons[4].getText().equals("X") && buttons[7].getText().equals("X") && buttons[1].getText().equals("")){
            buttons[1].setText("O");
            buttons[1].setEnabled(false);                
        } else if(buttons[5].getText().equals("X") && buttons[8].getText().equals("X") && buttons[2].getText().equals("")){
            buttons[2].setText("O");
            buttons[2].setEnabled(false);                
        } else if(buttons[6].getText().equals("X") && buttons[9].getText().equals("X") && buttons[3].getText().equals("")){
            buttons[3].setText("O");
            buttons[3].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("X") && buttons[7].getText().equals("X") && buttons[4].getText().equals("")){
            buttons[4].setText("O");
            buttons[4].setEnabled(false);                
        } else if(buttons[2].getText().equals("X") && buttons[8].getText().equals("X") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        } else if(buttons[3].getText().equals("X") && buttons[9].getText().equals("X") && buttons[6].getText().equals("")){
            buttons[6].setText("O");
            buttons[6].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("X") && buttons[5].getText().equals("X") && buttons[9].getText().equals("")){
            buttons[9].setText("O");
            buttons[9].setEnabled(false);                
        } else if(buttons[5].getText().equals("X") && buttons[9].getText().equals("X") && buttons[1].getText().equals("")){
            buttons[1].setText("O");
            buttons[1].setEnabled(false);                
        } else if(buttons[1].getText().equals("X") && buttons[9].getText().equals("X") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        }
        
        else if(buttons[3].getText().equals("X") && buttons[5].getText().equals("X") && buttons[7].getText().equals("")){
            buttons[7].setText("O");
            buttons[7].setEnabled(false);                
        } else if(buttons[7].getText().equals("X") && buttons[5].getText().equals("X") && buttons[3].getText().equals("")){
            buttons[3].setText("O");
            buttons[3].setEnabled(false);                
        } else if(buttons[7].getText().equals("X") && buttons[3].getText().equals("X") && buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("X") && buttons[5].getText().equals("O") && buttons[9].getText().equals("X")) {
            buttons[6].setText("O");
            buttons[6].setEnabled(false);            
        }    
        
        else if(buttons[3].getText().equals("X") && buttons[5].getText().equals("O") && buttons[7].getText().equals("X")) {
            buttons[4].setText("O");
            buttons[4].setEnabled(false);            
        }
        
        else if(buttons[5].getText().equals("")){
            buttons[5].setText("O");
            buttons[5].setEnabled(false);                
        }
        
        else if(buttons[1].getText().equals("")){
            buttons[1].setText("O");
            buttons[1].setEnabled(false);                
        }
        else {
            if(count >= 9)
                checkWin();
            else
                RandomMove();
        }
        
        checkWin();

    }
    
    public void RandomMove(){
        Random x = new Random();
        int y = 1 + x.nextInt(9);
        if(buttons[y].getText().equals("O") || buttons[y].getText().equals("X") ){
            RandomMove();
        } else {
            buttons[y].setText("O");
            buttons[y].setEnabled(false);
        }
    }
    
    public void checkWin(){ 
            
        /*Determine who won*/
        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().equals("")) {
                win = true;
            }
        }

        if(count % 2 == 0)
            letter = "O";
        else
            letter = "X";
        
        /*Show a dialog when game is over*/
        if(win == true){
            JOptionPane.showMessageDialog(null, letter + " wins!");
            System.exit(0);
        } else if(count >= 9 && win == false){
            JOptionPane.showMessageDialog(null, "The game was tie!");
            System.exit(0);
        }
    }
    public static void main(String[] args){
        new TicTacToeV4();
    }
}

Edited by John, 18 July 2010 - 01:39 PM.

  • 4

#4 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 17,274 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 January 2007 - 12:26 PM

One reason Tic-Tac-Toe is a popular project for CS teachers is because the algorithm for optimal play is well understood. Coding it is slightly trickier :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 20 January 2007 - 10:37 AM

One reason Tic-Tac-Toe is a popular project for CS teachers is because the algorithm for optimal play is well understood. Coding it is slightly trickier :)


Yeah, I didnt have any issues with two player tic-tac-toe, but I am having some issues the AI but that will be fixed in due time.

+++++++++++
Version 2 has been updated.
+++++++++++
  • 0

#6 Guest_Epps_*

Guest_Epps_*
  • Guest

Posted 14 February 2007 - 07:48 PM

Hi, I'm currently in a computer science class and I was using your guide as a reference on how i am making my tic tac toe board but our images on the squares are required to have color....I am unsure of how to do this but i do know that the text that locks each grid is:

pressedButton.setEnabled(false);

any idea on how to get it so there is color on my X's and O's but i can't click on the same button again?
  • 0

#7 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 14 February 2007 - 09:15 PM

Java labels support HTML code, so for example in my code, I could do something like this:

if(count % 2 == 0){
	letter = "<HTML><font color=red>O</font></HTML>";
} else {
	letter = "<HTML><font color=blue>X</font></HTML>";
}

And when an O is suppose to be printed on the JButton it is red and when an X is suppose to be printed on the JButton it is blue. You can also set the background of the JButtons by calling the setBackground(Color c) method, and an example in my code would be

pressedButton.setBackground(Color.WHITE);

and when the button is pressed it sets the background color to white. This should get you started.
  • 0

#8 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 30 May 2007 - 02:56 PM

version 3 added
  • 0

#9 Guest_Bigbag101_*

Guest_Bigbag101_*
  • Guest

Posted 01 June 2007 - 05:15 PM

hi i also have a programing class and used this method, how abouts would i go adding a picture instead of text?
  • 0

#10 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 01 June 2007 - 09:35 PM

hi i also have a programing class and used this method, how abouts would i go adding a picture instead of text?


I believe JButtons have a setIcon method which should work. So rather than calling the setText method, you should be able to work in with setIcon method with relative ease.
  • 0

#11 Guest_Bigbag101_*

Guest_Bigbag101_*
  • Guest

Posted 01 June 2007 - 09:50 PM

Sidewinder | Please use [php],
and [quote] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted.[/color]

Still regarding the image instead of text, it still does not appear to be working. If you could be so kind, can you please show me how to setIcon using the code.



[code]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToeV2 implements ActionListener {
	/*Instance Variables*/
	private int[][] winCombinations = new int[][] {
			{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
			{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
			{0, 4, 8}, {2, 4, 6}			 //diagonal wins
		};
	private JFrame window = new JFrame("Tic-Tac-Toe");
	private JButton buttons[] = new JButton[9];
	private int count = 0;
	private String letter = "";
	private boolean win = false;

	public TicTacToeV2(){
	/*Create Window*/
	window.setSize(300,300);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	window.setLayout(new GridLayout(3,3));
	
	/*Add Buttons To The Window*/
	for(int i=0; i<=8; i++){
		buttons[i] = new JButton();
		window.add(buttons[i]);
		buttons[i].addActionListener(this);
	}
	
	/*Make The Window Visible*/
	window.setVisible(true);
	}
	
	/**
	 When an object is clicked, perform an action.
	 @param a action event object
	 */
	public void actionPerformed(ActionEvent a) {
		count++;
		
		/*Calculate whose turn it is*/
		if(count % 2 == 0){
			letter = "O";
		} else {
			letter = "X";
		}

		/*Write the letter to the button and deactivate it*/
		 JButton pressedButton = (JButton)a.getSource(); 
		 pressedButton.setText(letter);
		 pressedButton.setEnabled(false);
		
		/*Determine who won*/
		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;
			}
		}
		
		/*Show a dialog when game is over*/
		if(win == true){
			JOptionPane.showMessageDialog(null, letter + " wins the game!");
			System.exit(0);
		} else if(count == 9 && win == false){
			JOptionPane.showMessageDialog(null, "The game was tie!");
			System.exit(0);
		}		
	}
	
	public static void main(String[] args){
		TicTacToeV2 starter = new TicTacToeV2();
	}
}

[color=red]Sidewinder | Please use [php], [code=auto:0] and [quote] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted.

  • 0

#12 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 02 June 2007 - 08:04 AM

From the looks of it, you've made no changes to my code. Show me what you did and I will help.

Have you looked at the Javadocs?

Java Platform 1.2 Beta 4 API Specification: Class JButton
  • 0





Also tagged with one or more of these keywords: tic-tac-toe

Powered by binpress