// The "TicTacToe3d" class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import hsa.*;
public class TicTacToe3D extends JApplet implements ActionListener
{
/*
* Variable Dictionary:
* int count = Used for counting the terms
* String symbol = Used for setting X or O to the buttons
*/
/*
* No. of panels which are basically the boards in Tic-Tac-Toe Game
* We're going to use 3 panels to make 3 Boards and set it to final
*/
public static final int panels = 3;
private GameBoard[] boards = new GameBoard [panels];
/*
* The JButtons that are used for placing X or O
*/
private JButton[] buttons = new JButton [9];
/*
* All the posible winning combinations in the game
*/
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
}
}
;
private boolean checkWin = false;
int count = 0;
String symbol = "";
class GameButton extends JButton
{
public int row, col, plane;
public GameButton (int row, int col, int plane)
{
this.row = row;
this.col = col;
this.plane = plane;
this.setText (" ");
//this.setBackground (
}
public String toString ()
{
return "(" + row + "," + col + "," + plane + ") = " + this.getText ();
}
}
class GameBoard extends JPanel
{
/*
* GameBoard Class Variable Dictionary:
*
*/
public static final int rowsB = 3;
public static final int colsB = 3;
private GameButton[] buttons = new GameButton [rowsB * colsB];
public GameBoard (int plane, ActionListener e)
{
setLayout (new GridLayout (rowsB, colsB));
for (int row = 0 ; row < rowsB ; row++)
{
for (int col = 0 ; col < colsB ; col++)
{
GameButton b = new GameButton (row, col, plane);
b.addActionListener (e);
add (b);
buttons [colsB * row + col] = b;
}
}
}
public char getValue (int y, int x)
{
String letter = buttons [colsB * y + x].getText ();
return (letter == null || letter.length () == 0) ? ' ':
letter.charAt (0);
}
public void setValue (int y, int x, char val)
{
buttons [colsB * y + x].setText (String.valueOf (val));
}
}
public void init ()
{
//Set the applet size for better viewing
setSize (300, 700);
//Message for the players
JLabel message = new JLabel ("X will go first");
getContentPane ().setLayout (new BorderLayout ());
getContentPane ().add (message, BorderLayout.NORTH);
//Adding 3 boards
getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
//Adding 3 boards
getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
for (int i = 0 ; i < 3 ; i++)
{
GameBoard gb = new GameBoard (i, this);
boards [i] = gb;
getContentPane ().add (gb);
}
}
public void actionPerformed (ActionEvent click)
{
count++;
//If the count is an even number (divisible by 2) then it sets the symbol as O
if (count % 2 == 0)
{
symbol = "O";
}
else
{
symbol = "X";
}
//If a button is click it either puts on an X or O depending on count then disable clicking it again
GameButton b = (GameButton) click.getSource ();
b.setText (symbol);
b.setEnabled (false);
GameBoard gb = new GameBoard (count, this);
if (gb.buttons [0].getText () == gb.buttons [1].getText () && gb.buttons [0].getText () == gb.buttons [2].getText () && gb.buttons [0].getText() != " ")
{
System.out.println ("+1");
}
}
} // TicTacToe3d class
Here's the whole code so far..