•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

# Java:Tutorial - Tic-Tac-Toe

tic-tac-toe

60 replies to this topic

### #37 AntLaTech

AntLaTech

CC Regular

• Just Joined
• 30 posts

Posted 03 October 2010 - 07:22 PM

Hey John,

I am new to the programming thing but I know a little bit. My professor gave us a project where we make a tic tac toe board and instead of hitting x and o. When we hit the create new game button it should randomly fill the boxes. My questions is how do I create the boxes because I am lost.

Thanks,
Ant
• 0

### #38 Loew

Loew

CC Lurker

• Just Joined
• 2 posts

Posted 09 December 2010 - 09:08 AM

With the second code (the one with all the loops) what exactly is going on when a winner is being determined?
I don't quite understand why it's i<=7 in the for loop part...why isn't it 9?
And how does the finding a winner code work?

buttons[winCombinations[i][0]].getText().equals(buttons[winCombinationss[i][1]].getText()) &&
buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
buttons[winCombinations[1][0].getText() != "")

I don't quite understand what is going on there

Thanks.
• 0

### #39 John

John

CC Mentor

• Moderator
• 4450 posts
• Location:New York, NY

Posted 09 December 2010 - 04:30 PM

There are seven combinations that will determine if you are a winner:

```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
};```

The loop checks to see if each button in the winCombinations set have the same value. So

if buttons[0] == buttons[1] == buttons[2] (which accounts for either all O's or all X's on the top row), then there is a winner, and so forth for each win combination.
• 0

### #40 Loew

Loew

CC Lurker

• Just Joined
• 2 posts

Posted 09 December 2010 - 07:00 PM

Thanks a lot
• 0

### #41 M2G

M2G

CC Lurker

• New Member
• 4 posts

Posted 30 December 2010 - 06:38 AM

I have a java project to make a tic-tac-toe game with variable dimensions and variable number of players.
For example, if the grid is 9x9. Players will play normally trying to make 3 consecutive X or 0. Each player will have a score which will be incremented if he manages to make 3 consecutive X or 0.

i want that buttons grid to be modified by those radiobuttons and I don't know how to do that.
When i press the radio button 5x5 I want the buttton grid to be modified by that dimension.

Here I gave that grid variable the value 3 so i could test it.

```public Xsi0View(SingleFrameApplication app) {
super(app);
[COLOR="Red"]grid=3[/COLOR];
[COLOR="Red"]int dimensiune = (grid*grid)[/COLOR];

initComponents();

for(int i=1; i<=dimensiune; i++){
buttons[i] = new JButton();
}

}```

This is initComponents with the GUI generated by Net Beans:

```private void initComponents() {

mainPanel = new javax.swing.JPanel();
tabla = new javax.swing.JPanel();
dimensiuni = new javax.swing.JPanel();
grupbutoane = new javax.swing.ButtonGroup();

mainPanel.setName("mainPanel"); // NOI18N
mainPanel.setPreferredSize(new java.awt.Dimension(988, 873));

tabla.setAutoscrolls(true);
tabla.setName("tabla"); // NOI18N
tabla.setLayout(new java.awt.GridLayout([COLOR="Red"]grid, grid[/COLOR]
));

dimensiuni.setName("dimensiuni"); // NOI18N
dimensiuni.setLayout(new java.awt.GridLayout(3, 3));

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(xsi0.Xsi0App.class).getContext().getResourceMap(Xsi0View.class);
dimensiune3.setText(resourceMap.getString("dimensiune3.text")); // NOI18N
dimensiune3.setName("dimensiune3"); // NOI18N

dimensiune5.setText(resourceMap.getString("dimensiune5.text")); // NOI18N
dimensiune5.setName("dimensiune5"); // NOI18N

dimensiune7.setText(resourceMap.getString("dimensiune7.text")); // NOI18N
dimensiune7.setName("dimensiune7"); // NOI18N

dimensiune9.setText(resourceMap.getString("dimensiune9.text")); // NOI18N
dimensiune9.setName("dimensiune9"); // NOI18N

javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
);
mainPanelLayout.setVerticalGroup(
);

setComponent(mainPanel);
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="comment">

// Variables declaration - do not modify
private javax.swing.JPanel dimensiuni;
private javax.swing.ButtonGroup grupbutoane;
private javax.swing.JPanel mainPanel;
private javax.swing.JPanel tabla;
// End of variables declaration```

This is the full source code:
```/*
* Xsi0View.java
*/

package xsi0;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;

/**
* The application's main frame.
*/
public class Xsi0View extends FrameView implements ActionListener{
private JButton buttons[] = new JButton[100];
private int count = 0;
private int grid;
private String letter = "";
private boolean win = false;
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
};

public Xsi0View(SingleFrameApplication app) {
super(app);
grid=3;
int dimensiune = (grid*grid);

initComponents();

for(int i=1; i<=dimensiune; i++){
buttons[i] = new JButton();
}

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

mainPanel = new javax.swing.JPanel();
tabla = new javax.swing.JPanel();
dimensiuni = new javax.swing.JPanel();
grupbutoane = new javax.swing.ButtonGroup();

mainPanel.setName("mainPanel"); // NOI18N
mainPanel.setPreferredSize(new java.awt.Dimension(988, 873));

tabla.setAutoscrolls(true);
tabla.setName("tabla"); // NOI18N
tabla.setLayout(new java.awt.GridLayout(grid, grid
));

dimensiuni.setName("dimensiuni"); // NOI18N
dimensiuni.setLayout(new java.awt.GridLayout(3, 3));

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(xsi0.Xsi0App.class).getContext().getResourceMap(Xsi0View.class);
dimensiune3.setText(resourceMap.getString("dimensiune3.text")); // NOI18N
dimensiune3.setName("dimensiune3"); // NOI18N

dimensiune5.setText(resourceMap.getString("dimensiune5.text")); // NOI18N
dimensiune5.setName("dimensiune5"); // NOI18N

dimensiune7.setText(resourceMap.getString("dimensiune7.text")); // NOI18N
dimensiune7.setName("dimensiune7"); // NOI18N

dimensiune9.setText(resourceMap.getString("dimensiune9.text")); // NOI18N
dimensiune9.setName("dimensiune9"); // NOI18N

javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
);
mainPanelLayout.setVerticalGroup(
);

setComponent(mainPanel);
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="comment">

// Variables declaration - do not modify
private javax.swing.JPanel dimensiuni;
private javax.swing.ButtonGroup grupbutoane;
private javax.swing.JPanel mainPanel;
private javax.swing.JPanel tabla;
// End of variables declaration

public void actionPerformed(ActionEvent e) {

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)e.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() == null ? "" != null : !buttons[winCombinations[i][0]].getText().equals(""))){
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);}*/
}
}

```

How can I make this work?

//Le Solved!

Edited by M2G, 05 January 2011 - 06:03 AM.

• 0

### #42 boudiz

boudiz

CC Lurker

• Just Joined
• 1 posts

Posted 01 May 2011 - 02:32 AM

Hey I was using the code of John for the game(without []). But I had some problems:
1-When I restart the game I press a button the popup appears that someone wins
2-How can put the game in the main panel as a frame for adding labels below for winner and losers
Thank you for helping
• 0

### #43 Java_

Java_

CC Newcomer

• Member
• 13 posts

Posted 01 May 2011 - 10:37 AM

Thanks for this. An interesting read.
• 0

### #44 cooolkid

cooolkid

CC Lurker

• Just Joined
• 1 posts

Posted 23 May 2011 - 09:32 PM

Hello all, I am trying to finish this tic tac toe assignment and I realize that I have three issues. The first is that Draw game doesn't work. The second problem is that not all the checkwinner checks work. Third issue is that my program crashes if users enter something else other than 1-9. This is worth 20% of my grade so any help would be greatly appreciated.
```package tictactoe;

import java.util.Scanner;

/**
*
* @author Kenny
*/
public class Main {

public void RunGame()
{
boolean playGame = true;

while(playGame)
{

tictactoe gameboard = new tictactoe();

char player1 = 'X';
char player2 = 'O';
char player = ' ';

gameboard.initialize();
gameboard.Board();
boolean noWinner = true;
while (noWinner)
{
getNextInput(player1, gameboard);
gameboard.Board();
gameboard.checkWinner();
if (gameboard.checkWinner())
{
noWinner = false;
player = player1;
continue;
}
if (gameboard.checkCounter()>= 9)
{
noWinner = false;
}
getNextInput(player2, gameboard);
gameboard.Board();
gameboard.checkWinner();
if (gameboard.checkWinner())
{
noWinner = false;
player = player2;
continue;
}
if (gameboard.checkCounter()>= 9)
{
noWinner = false;
}
}
if (gameboard.checkWinner())
{
System.out.println("Game over Player " + player + " Wins!");
System.out.printf("Number of turns %d\n", gameboard.checkCounter());
}
else
{
System.out.println("Draw Game!");
}
System.out.println();
System.out.print("Play again? (1 = Yes, Quit = all other numbers) ");
Scanner reader = new Scanner (System.in);
if (choice == 1)
{
playGame = true;
}
else
{
playGame = false;
}
}
}

public int getNextInput(char playerToken, tictactoe pgameboard)

{
System.out.println("Please select a Number (1 - 9)");
Scanner reader = new Scanner (System.in);
int userInput;
boolean validInput = false;

while(!validInput)

{
// get ihe input from the user
// test to see if the space is available
validInput = pgameboard.checkBoard(userInput, playerToken);
}
return 0;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
main.RunGame();

}
}```
tic tac toe class
```package tictactoe;

/**
*
* @author Kenny
*/
public class tictactoe {
// setting up board
char board [][] = new char [3][3];
int counter;

public int checkCounter()
{
return counter;
}

//initializing the board
public void initialize(){
int digits = 0;
counter = 0;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board [i][j] = Character.forDigit(++digits, 10);
}
}
}

public boolean checkBoard(int boardSpot, char BoardChar)

{
int boardRow;
int boardColumn;
// changing the range to 0 - 8
boardSpot = boardSpot - 1;
/* Using integer division and module function to extract row
and column from a single number
*/
boardRow = boardSpot / 3;
boardColumn = boardSpot %3;
// test to see if space avaliable
if ((board [boardRow][boardColumn] != 'X') &&
(board [boardRow][boardColumn] != 'O') )
{
board [boardRow][boardColumn] = BoardChar;
counter ++;
return true;
}
return false;
}

public boolean checkWinner()
{

//Checking rows
char current = ' ';
for (int i = 0; i < 3; i++) {
int j = 0;
for (j = 0; j < 3; j++) {
if (!Character.isLetter(board[i][j])) {
break;
}
if (j == 0) {
current = board[i][j];
} else if (current != board[i][j]) {
break;
}
if (j == 2) {
//Found winner
return true;
}
}
}
//Checking columns
for (int i = 0; i < 3; i++) {
current = ' ';
int j = 0;
for (j = 0; j < 3; j++) {
if (!Character.isLetter(board[j][i])) {
break;
}
if (j == 0) {
current = board[j][i];
} else if (current != board[j][i]) {
break;
}
if (j == 2) {
//Found winner
return true;
}
// checking for a diaginal winner

if ((Character.isLetter(board[0][0])) && (board[1][1] == board[0][0]) &&
(board[2][2] == board[0][0])) {
return true;
}

if ((Character.isLetter(board[2][0])) && (board[1][1] == board[2][0]) &&
(board[0][2] == board[2][0])) {
return true;
}

}
return false;
}
return false;
}

public void Board()
{
System.out.println("-------------");
System.out.printf("| %c | %c | %c |\n", board [0][0], board [0][1],
board [0][2]);
System.out.println("-------------");
System.out.printf("| %c | %c | %c |\n", board [1][0], board [1][1],
board [1][2]);
System.out.println("-------------");
System.out.printf("| %c | %c | %c |\n", board [2][0], board [2][1],
board [2][2]);
System.out.println("-------------");

}
}```

• 0

### #45 kauw5164

kauw5164

CC Lurker

• Just Joined
• 1 posts

Posted 26 September 2011 - 08:14 AM

In this Code I modify code. But When X win can't show X win It Become O win. Because when show boxmessage after cnt=7.
I don't know to edit. Can you help me pls.

```package proj_game;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class Proj_Game implements ActionListener
{
private int[][] winCombinations = new int[][]
{
{1, 2, 3}, {4, 5, 6}, {7, 8, 9},
{1, 4, 7}, {2, 5, 8}, {3, 6, 9},
{1, 5, 9}, {3, 5, 7}
};
private JFrame window = new JFrame("เกม OX คน VS บอทAIขั้นเทพ Version 1.0");
private JButton buttons[] = new JButton[10];
private int cnt = 0;
private String letter = "";
private boolean win = false;

public Proj_Game()
{
window.setPreferredSize(new Dimension(500,500));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

for(int i=1;i<=9;i++)
{
buttons[i] = new JButton();
}
window.setVisible(true);
window.pack();
}

public void actionPerformed(ActionEvent a)
{
for(int i = 1; i<= 9; i++)
{
if(a.getSource() == buttons[i])
{
buttons[i].setText("X");
buttons[i].setEnabled(false);
}
}
cnt++;
AI();
}

public void AI()
{
cnt++;
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(cnt >= 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()
{
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(cnt % 2 == 0)
letter = "O";
else
letter = "X";

if(win == true)
{
JOptionPane.showMessageDialog(null,"ฝ่าย "+ letter + " เป็นผู้ชนะ !!!");
System.exit(0);
}
else if(cnt >= 9 && win == false)
{
JOptionPane.showMessageDialog(null, "ตานี้เสมอกัน มาแข่งกันใหม่อีกซักรอบ");
System.class.getClass();
buttons[1].setText("");
buttons[1].setEnabled(true);
buttons[2].setText("");
buttons[2].setEnabled(true);
buttons[3].setText("");
buttons[3].setEnabled(true);
buttons[4].setText("");
buttons[4].setEnabled(true);
buttons[5].setText("");
buttons[5].setEnabled(true);
buttons[6].setText("");
buttons[6].setEnabled(true);
buttons[7].setText("");
buttons[7].setEnabled(true);
buttons[8].setText("");
buttons[8].setEnabled(true);
buttons[9].setText("");
buttons[9].setEnabled(true);
cnt=0;
System.runFinalization();
}

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

---------- Post added at 11:14 PM ---------- Previous post was at 11:04 PM ----------

Has a bug.
• 0

### #46 needhelp

needhelp

CC Newcomer

• Just Joined
• 12 posts

Posted 24 October 2011 - 09:18 AM

I have a question. I'm making a tic tac toe program as well. the only difference with my tic tac toe board is it is a 4X3 grid. the last row contains buttons called play again, player x's number of wins, and player o's number of wins. my question is, if someone wins the game, i would like the unclicked buttons of the actual play grid to be disabled, how would i go abouts in doing so? but i do not want my last row of buttons to be disabled.
• 0

### #47 lethalwire

lethalwire

while(false){ ... }

• Senior Member
• 766 posts
• Programming Language:C, Java, PHP, JavaScript
• Learning:PHP

Posted 25 October 2011 - 02:39 PM

I have a question. I'm making a tic tac toe program as well. the only difference with my tic tac toe board is it is a 4X3 grid. the last row contains buttons called play again, player x's number of wins, and player o's number of wins. my question is, if someone wins the game, i would like the unclicked buttons of the actual play grid to be disabled, how would i go abouts in doing so? but i do not want my last row of buttons to be disabled.

This is what you're gonna have to do unfortunately:
Also, if you want to disable unused buttons at the end of the game, you're just gonna have to manually call a method when win==true.
A method you create that is.

```void disableButtons(){
if(button1.isEnabled())
button1.setEnabled(false);
if(buttons2.isEnabled())
button2.setEnabled(false);
...
...
}
```

• 0

### #48 Ritwik I the programmer

Ritwik I the programmer

• 244 posts
• Programming Language:Java
• Learning:Java, C++, Python

Posted 28 November 2011 - 11:05 PM

There is a serious issue with the AI in TicTacToeV4: It does not check the win condition for every move.This is not required until at least one player has played 3 moves, but it is necessary after that. Also, I wonder if there is any way to make the AI undefeatable?
I've fixed the win condition problem, but can't figure out the second part. Here's the code(only the places in the code where checkWin is called have been changed):

```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));

for(int i = 1; i<=9; i++){
buttons[i] = new JButton();
}

/*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++;
checkWin();
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
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();
}
}
```

• 0

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download