Tic Tac Toe
Tic Tac Toe
Project by:
1. NAVINKUMAR GURNANI(66)
2. KUNAL MULWANI(67)
3. APURVA SANDBHOR(68)
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.net.URL;
import java.applet.*;
public class TicTacToe extends JFrame
{
JTextField messageTextField = new JTextField();
JPanel gamePanel = new JPanel();
JTextField[] boxTextField = new JTextField[9];
JLabel[] gridLabel = new JLabel[4];
JPanel playersPanel = new JPanel();
ButtonGroup playersButtonGroup = new ButtonGroup();
JRadioButton twoPlayersRadioButton = new JRadioButton();
JRadioButton onePlayerRadioButton = new JRadioButton();
JPanel firstPanel = new JPanel();
ButtonGroup firstButtonGroup = new ButtonGroup();
JRadioButton youFirstRadioButton = new JRadioButton();
JRadioButton computerFirstRadioButton = new JRadioButton();
JPanel computerPanel = new JPanel();
ButtonGroup computerButtonGroup = new ButtonGroup();
JRadioButton randomRadioButton = new JRadioButton();
JRadioButton smartRadioButton = new JRadioButton();
JPanel buttonsPanel = new JPanel();
JButton startStopButton = new JButton();
JButton exitButton = new JButton();
boolean xTurn;
boolean canClick = false;
int numberClicks;
String[] possibleWins = new String[8];
boolean gameOver;
Random myRandom = new Random();
AudioClip drawSound;
AudioClip winSound;
public TicTacToe()
{
// frame constructor
setTitle("Tic Tac Toe");
getContentPane().setBackground(Color.WHITE);
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
exitForm(evt);
}
});
getContentPane().setLayout(new GridBagLayout());
twoPlayersRadioButton.setText("Two Players");
twoPlayersRadioButton.setBackground(Color.WHITE);
twoPlayersRadioButton.setSelected(true);
playersButtonGroup.add(twoPlayersRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.WEST;
playersPanel.add(twoPlayersRadioButton, gridConstraints);
twoPlayersRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
twoPlayersRadioButtonActionPerformed(e);
}
});
onePlayerRadioButton.setText("One Player");
onePlayerRadioButton.setBackground(Color.WHITE);
playersButtonGroup.add(onePlayerRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
playersPanel.add(onePlayerRadioButton, gridConstraints);
onePlayerRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
onePlayerRadioButtonActionPerformed(e);
}
});
youFirstRadioButton.setText("You First");
youFirstRadioButton.setBackground(Color.WHITE);
youFirstRadioButton.setSelected(true);
firstButtonGroup.add(youFirstRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.WEST;
firstPanel.add(youFirstRadioButton, gridConstraints);
computerFirstRadioButton.setText("Computer First");
computerFirstRadioButton.setBackground(Color.WHITE);
firstButtonGroup.add(computerFirstRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
firstPanel.add(computerFirstRadioButton, gridConstraints);
randomRadioButton.setText("Random Computer");
randomRadioButton.setBackground(Color.WHITE);
randomRadioButton.setSelected(true);
computerButtonGroup.add(randomRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.WEST;
computerPanel.add(randomRadioButton, gridConstraints);
smartRadioButton.setText("Smart Computer");
smartRadioButton.setBackground(Color.WHITE);
computerButtonGroup.add(smartRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
computerPanel.add(smartRadioButton, gridConstraints);
startStopButton.setText("Start Game");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
buttonsPanel.add(startStopButton, gridConstraints);
startStopButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
startStopButtonActionPerformed(e);
}
});
exitButton.setText("Exit");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.insets = new Insets(10, 0, 0, 0);
buttonsPanel.add(exitButton, gridConstraints);
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitButtonActionPerformed(e);
}
});
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height -
getHeight())), getWidth(), getHeight());
messageTextField.setText("Game Stopped");
youFirstRadioButton.setEnabled(false);
computerFirstRadioButton.setEnabled(false);
randomRadioButton.setEnabled(false);
smartRadioButton.setEnabled(false);
// possible wins
possibleWins[0] = "012";
possibleWins[1] = "345";
possibleWins[2] = "678";
possibleWins[3] = "036";
possibleWins[4] = "147";
possibleWins[5] = "258";
possibleWins[6] = "048";
possibleWins[7] = "246";
try
{
drawSound = Applet.newAudioClip(new URL("file:" + "beep.wav"));
winSound = Applet.newAudioClip(new URL("file:" + "tada.wav"));
}
catch (Exception ex)
{
System.out.println("Error loading sound files");
}
}
if (randomRadioButton.isSelected())
{
// random logic
// put mark in Nth available square
n = myRandom.nextInt(9 - numberClicks) + 1;
i = 0;
for (selectedBox = 0; selectedBox < 9; selectedBox++)
{
if (boxTextField[selectedBox].getText().equals(""))
i++;
if (i == n)
break;
}
// put mark in SelectedBox
markClickedBox(selectedBox);
}
else
{
// smart computer
// determine who has what mark
if (computerFirstRadioButton.isSelected())
{
computerMark = "X";
playerMark = "O";
}
else
{
computerMark = "O";
playerMark = "X";
}
// Step 1 (K = 1) - check for win - see if two boxes hold computer mark and one is empty
// Step 2 (K = 2) - check for block - see if two boxes hold player mark and one is empty
for (k = 1; k <= 2; k++)
{
if (k == 1)
markToFind = computerMark;
else
markToFind = playerMark;
for (i = 0; i < 8; i++)
{
n = 0;
emptyBox = 0;
for (j = 0; j < 3; j++)
{
boxNumber[j] = Integer.valueOf(String.valueOf(possibleWins[i].charAt(j))).intValue();
mark[j] = boxTextField[boxNumber[j]].getText();
if (mark[j].equals(markToFind))
n++;
else if (mark[j].equals(""))
emptyBox = boxNumber[j];
}
if (n == 2 && emptyBox != 0)
{
// mark empty box to win (K = 1) or block (K = 2)
markClickedBox(emptyBox);
return;
}
}
}
// Step 3 - find next best move
for (i = 0; i < 9; i++)
{
if (boxTextField[bestMoves[i]].getText().equals(""))
{
markClickedBox(bestMoves[i]);
return;
}
}
}
}
SCREEN SHOTS: