// The "TicTacToe3d" class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import hsa.*;
public class TicTacToe3D extends JFrame implements ActionListener
{
//Setting colors
private Color Moccasin = new Color (255, 228, 181);
private Color Cornsilk2 = new Color (238, 232, 205);
private JButton buttons[] = new JButton [27];
JPanel panelB1 = new JPanel ();
JPanel panelB2 = new JPanel ();
JPanel panelB3 = new JPanel ();
private int[][] winCombinations = new int[][]
{
//First Board Horizontal/Vertical/Diagonal win
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6},
//Second Board Horizontal/Vertical/Diagonal win
{9,10,11}, {12,13,14},{15,16,17},
{9,12,15}, {10,13,16}, {11,14,17},
{9,13,17}, {11,13,15},
//Third Board Horizontal/Vertical/Diagonal win
{18,19,20}, {21,22,23}, {24,25,26},
{18,21,24}, {19,22,25}, {20,23,26},
{18,22,26}, {20,22,24},
//Across the board winning
{0,15,21},{0,12,24},{0,10,20},{0,12,24},
{3,9,24}, {3,13,23}, {3,15,18}, {3,22,14},
{6,16,26}, {6,12,20}, {6,22,11}, {6,25,17}, {6,9,21}, {6,18,12},
{1,9,20},{1,18,11},{1,13,25},{1,16,22},
{4,10,25}, {4,12,23}, {4,16,19}, {4,14,21},{4,15,20},{4,11,24},
{7,15,26}, {7,17,24}, {7,13,19}, {7,22,10},
{2,13,24}, {2,15,22}, {2,14,26}, {2,13,17}, {2,10,8}, {2,19,9},
{5,13,21}, {5, 22, 12}, {5,11,26}, {5,17,20},
{8,13,18}, {8,9,22}, {8,15,25}, {8,11,23}, {8,20,14},
};
private boolean checkWin = false;
//Count turns starts at 0
int count = 0;
//X or O variable
String symbol = "";
//3 Sub-classes for each tic-tac-toe board
class GameBoard extends JButton
{
public GameBoard (ActionListener e)
{
setLayout (new GridLayout (3, 3));
for (int i = 0 ; i <= 8 ; i++)
{
buttons [i] = new JButton ();
buttons [i].addActionListener (e);
buttons [i].setBackground (Moccasin);
buttons [i].setPreferredSize (new Dimension (100, 100));
add (buttons [i]);
}
}
}
class GameBoard2 extends JButton
{
public GameBoard2 (ActionListener e)
{
setLayout (new GridLayout (3, 3));
for (int i = 9 ; i <= 17 ; i++)
{
buttons [i] = new JButton ();
buttons [i].addActionListener (e);
buttons [i].setBackground (Moccasin);
buttons [i].setPreferredSize (new Dimension (100, 100));
add (buttons [i]);
}
}
}
class GameBoard3 extends JButton
{
public GameBoard3 (ActionListener e)
{
setLayout (new GridLayout (3, 3));
for (int i = 18 ; i <= 26 ; i++)
{
buttons [i] = new JButton ();
buttons [i].addActionListener (e);
buttons [i].setBackground (Moccasin);
buttons [i].setPreferredSize (new Dimension (300, 150));
add (buttons [i]);
}
}
}
public TicTacToe3D ()
{
GameBoard gb = new GameBoard (this);
GameBoard2 gb2 = new GameBoard2 (this);
GameBoard3 gb3 = new GameBoard3 (this);
JFrame frame = new JFrame ("3D Tic-Tac-Toe");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().setLayout (new BorderLayout ());
// Scorer
// JLabel scoreMsgP1 = new JLabel ("Player1: ");
// JLabel scoreMsgP2 = new JLabel ("Player2: ");
// frame.getContentPane ().add (scoreMsgP2, BorderLayout.SOUTH);
panelB1.setBorder (BorderFactory.createLineBorder (Color.black));
panelB1.setPreferredSize (new Dimension (320, 320));
panelB1.setLayout (new GridLayout (3, 3));
panelB1.add (gb);
panelB2.setBorder (BorderFactory.createLineBorder (Color.black));
panelB2.setPreferredSize (new Dimension (320, 320));
panelB2.setLayout (new GridLayout (3, 3));
panelB2.add (gb2);
panelB3.setBorder (BorderFactory.createLineBorder (Color.black));
panelB3.setPreferredSize (new Dimension (320, 320));
panelB3.setLayout (new GridLayout (3, 3));
panelB3.add (gb3);
frame.getContentPane ().add (panelB1, BorderLayout.NORTH);
frame.getContentPane ().add (panelB2, BorderLayout.CENTER);
frame.getContentPane ().add (panelB3, BorderLayout.SOUTH);
frame.pack ();
frame.setVisible (true);
}
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
* It also changes the color of the button
* The fontface and size is also changed
*/
JButton pressedButton = (JButton) click.getSource ();
pressedButton.setBackground (Cornsilk2);
pressedButton.setText (symbol);
pressedButton.setFont (new Font ("Comic Sans MS", Font.BOLD, 40));
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 () != "")
{
checkWin = true;
}
}
if (checkWin == true)
{
JOptionPane.showMessageDialog (null, symbol + "Gets a Point!");
System.exit(0);
}
}
public static void main (String[] args)
{
new TicTacToe3D ();
}
} // TicTacToe3d class