The source of my COPYRIGHTED tictactoe java game. It shows how to use multiple classes, methods, etc.
Tic - Tac - Toe Application
/*************************
* Copyright 2004 CRD *
* Author Ian Cinnamon *
* Version Number: 1.0 *
* Tic-Tac-Toe Game *
*
************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
/*Layout of Tic Tac Toe Board:
1 2 3 _|_|_
4 5 6 _|_|_
7 8 9 | |
*/
/*Artifical Intelligence Class
Artifical Intelligence Class
Artifical Intelligence Class
Artifical Intelligence Class
Artifical Intelligence Class*/
class TicTacToeAI extends JFrame implements ActionListener
{
public String playerX = JOptionPane.showInputDialog(
"Computer edition. Enter player X's name.");
public String playerO = "Computer";
public float player = Math.round(Math.random() * 10);
public String play = (playerX + "'s turn.");
public String one1 = " ";
public String two2 = " ";
public String three3 = " ";
public String four4 = " ";
public String five5 = " ";
public String six6 = " ";
public String seven7 = " ";
public String eight8 = " ";
public String nine9 = " ";
public boolean xTurn = true;
public double cwins = 0;
public double hwins = 0;
public double ties = 0;
JLabel lbl = new JLabel("Cinnamon's Tic-Tac-Toe");
JButton one = new JButton(one1);
JButton two = new JButton(two2);
JButton three = new JButton(three3);
JButton four = new JButton(four4);
JButton five = new JButton(five5);
JButton six = new JButton(six6);
JButton seven = new JButton(seven7);
JButton eight = new JButton(eight8);
JButton nine = new JButton(nine9);
JLabel status = new JLabel(play);
JButton redo = new JButton("Restart Game");
JButton help = new JButton("Who's who?");
JButton exit = new JButton("Exit");
JButton multi = new JButton("Multi-Player");
JButton single = new JButton("Single-Player");
JLabel wins = new JLabel("Wins: Computer (O): "+cwins+". Human (X): "+hwins+". Ties: "+ties+".");
public TicTacToeAI() {
super("Tic-Tac-Toe");
setSize(800, 725);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container contentArea = getContentPane();
GridBagLayout flowManager = new GridBagLayout();
GridBagConstraints pos = new GridBagConstraints();
contentArea.setLayout(flowManager);
getContentPane().setBackground(Color.RED);
redo.setFont(new Font("Arial", Font.PLAIN, 12));
help.setFont(new Font("Arial", Font.PLAIN, 12));
exit.setFont(new Font("Arial", Font.PLAIN, 12));
multi.setFont(new Font("Arial", Font.PLAIN, 12));
single.setFont(new Font("Arial", Font.PLAIN, 12));
one.setFont(new Font("Serif", Font.BOLD, 62));
two.setFont(new Font("Serif", Font.BOLD, 62));
three.setFont(new Font("Serif", Font.BOLD, 62));
four.setFont(new Font("Serif", Font.BOLD, 62));
five.setFont(new Font("Serif", Font.BOLD, 62));
six.setFont(new Font("Serif", Font.BOLD, 62));
seven.setFont(new Font("Serif", Font.BOLD, 62));
eight.setFont(new Font("Serif", Font.BOLD, 62));
nine.setFont(new Font("Serif", Font.BOLD, 62));
status.setFont(new Font("Times New Roman", Font.ITALIC + Font.BOLD, 24));
lbl.setFont(new Font("Cooper Black", Font.PLAIN, 45));
wins.setFont(new Font("Times New Roman", Font.PLAIN, 18));
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
redo.addActionListener(this);
help.addActionListener(this);
exit.addActionListener(this);
multi.addActionListener(this);
single.addActionListener(this);
single.setEnabled(false);
if (! (player >= 5)) {
play = "O's turn.";
xTurn = false;
}
else {
play = "X's turn.";
xTurn = true;
}
pos.gridx=1;
pos.gridy=0;
contentArea.add(lbl, pos);
pos.gridx = 0;
pos.gridy = 1;
contentArea.add(one, pos);
pos.gridx = 1;
pos.gridy = 1;
contentArea.add(two, pos);
pos.gridx = 2;
pos.gridy = 1;
contentArea.add(three, pos);
pos.gridx = 0;
pos.gridy = 2;
contentArea.add(four, pos);
pos.gridx = 1;
pos.gridy = 2;
contentArea.add(five, pos);
pos.gridx = 2;
pos.gridy = 2;
contentArea.add(six, pos);
pos.gridx = 0;
pos.gridy = 3;
contentArea.add(seven, pos);
pos.gridx = 1;
pos.gridy = 3;
contentArea.add(eight, pos);
pos.gridx = 2;
pos.gridy = 3;
contentArea.add(nine, pos);
pos.gridx = 1;
pos.gridy = 4;
contentArea.add(status, pos);
pos.gridx = 1;
pos.gridy = 5;
contentArea.add(redo, pos);
pos.gridx = 1;
pos.gridy = 6;
contentArea.add(help, pos);
pos.gridx = 1;
pos.gridy = 7;
contentArea.add(exit, pos);
pos.gridx = 1;
pos.gridy = 8;
contentArea.add(multi, pos);
pos.gridx = 1;
pos.gridy = 9;
contentArea.add(single, pos);
pos.gridx = 1;
pos.gridy = 11;
contentArea.add(wins, pos);
setContentPane(contentArea);
Checker();
}
public void Checker() {
if ( (one.getText() == "X" && two.getText() == "X" &&
three.getText() == "X") ||
(four.getText() == "X" && five.getText() == "X" && six.getText() == "X") ||
(seven.getText() == "X" && eight.getText() == "X" &&
nine.getText() == "X") ||
(one.getText() == "X" && four.getText() == "X" &&
seven.getText() == "X") ||
(two.getText() == "X" && five.getText() == "X" &&
eight.getText() == "X") ||
(three.getText() == "X" && six.getText() == "X" &&
nine.getText() == "X") ||
(one.getText() == "X" && five.getText() == "X" && nine.getText() == "X") ||
(three.getText() == "X" && five.getText() == "X" &&
seven.getText() == "X")) {
hwins+=0.5;
status.setText("X (" + playerX + ") wins!!!");
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
}
else if ( (one.getText() == "O" && two.getText() == "O" &&
three.getText() == "O") ||
(four.getText() == "O" && five.getText() == "O" &&
six.getText() == "O") ||
(seven.getText() == "O" && eight.getText() == "O" &&
nine.getText() == "O") ||
(one.getText() == "O" && four.getText() == "O" &&
seven.getText() == "O") ||
(two.getText() == "O" && five.getText() == "O" &&
eight.getText() == "O") ||
(three.getText() == "O" && six.getText() == "O" &&
nine.getText() == "O") ||
(one.getText() == "O" && five.getText() == "O" &&
nine.getText() == "O") ||
(three.getText() == "O" && five.getText() == "O" &&
seven.getText() == "O")) {
cwins+=0.5;
status.setText("O (" + playerO + ") wins!!!");
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
}
else if(one.getText()!=" " && two.getText()!=" " && three.getText()!=" " &&
four.getText()!=" " && five.getText()!=" " && six.getText()!=" " &&
seven.getText()!=" " && eight.getText()!=" " && nine.getText()!=" ")
{
ties=(ties+0.5);
status.setText("Cat's Game!");
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
}
}
public void AI() {
//AI Part of the Program
double r = Math.random() * 10;
int ran = (int) Math.ceil(r);
int rand = ran - 1;
//Offense move if statements
if((one.getText()=="O")&&(five.getText() == "O")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="O")&&(five.getText() == "O")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="O")&&(five.getText() == "O")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((nine.getText()=="O")&&(five.getText() == "O")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(nine.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="O")&&(seven.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((six.getText()=="O")&&(nine.getText() == "O")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="O")&&(six.getText() == "O")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((five.getText()=="O")&&(eight.getText() == "O")&&(two.getText()==" ")) {
two.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="O")&&(five.getText() == "O")&&(eight.getText()==" ")) {
eight.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="O")&&(seven.getText() == "O")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(four.getText() == "O")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(two.getText() == "O")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="O")&&(three.getText() == "O")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="O")&&(five.getText() == "O")&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((five.getText()=="O")&&(six.getText() == "O")&&(four.getText()==" ")) {
four.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="O")&&(eight.getText() == "O")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((eight.getText()=="O")&&(nine.getText() == "O")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(nine.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="O")&&(seven.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((six.getText()=="O")&&(nine.getText() == "O")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="O")&&(six.getText() == "O")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((five.getText()=="O")&&(eight.getText() == "O")&&(two.getText()==" ")) {
two.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="O")&&(five.getText() == "O")&&(eight.getText()==" ")) {
eight.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="O")&&(seven.getText() == "O")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(four.getText() == "O")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(two.getText() == "O")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="O")&&(three.getText() == "O")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="O")&&(five.getText() == "O")&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((five.getText()=="O")&&(six.getText() == "O")&&(four.getText()==" ")) {
four.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="O")&&(eight.getText() == "O")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((eight.getText()=="O")&&(nine.getText() == "O")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="O")&&(six.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="O")&&(nine.getText() == "O")&&(eight.getText()==" ")) {
eight.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(three.getText() == "O")&&(two.getText()==" ")) {
two.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="O")&&(seven.getText() == "O")&&(four.getText()==" ")) {
four.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="O")&&(eight.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="O")&&(nine.getText() == "O")&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="O")&&(three.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((nine.getText()=="O")&&(one.getText() == "O")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="O")&&(five.getText() == "O")&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
//Defensive move if statements
else if((one.getText()=="X")&&(nine.getText() == "X")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="X")&&(seven.getText() == "X")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((six.getText()=="X")&&(nine.getText() == "X")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="X")&&(six.getText() == "X")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((five.getText()=="X")&&(eight.getText() == "X")&&(two.getText()==" ")) {
two.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="X")&&(five.getText() == "X")&&(eight.getText()==" ")) {
eight.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="X")&&(seven.getText() == "X")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((five.getText()=="X")&&(eight.getText() == "X")&&(two.getText()==" ")) {
two.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="X")&&(four.getText() == "X")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="X")&&(two.getText() == "X")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="X")&&(three.getText() == "X")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="X")&&(five.getText() == "X")&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((five.getText()=="X")&&(six.getText() == "X")&&(four.getText()==" ")) {
four.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="X")&&(eight.getText() == "X")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((eight.getText()=="X")&&(nine.getText() == "X")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="X")&&(five.getText() == "X")&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="X")&&(five.getText() == "X")&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="X")&&(five.getText() == "X")&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((nine.getText()=="X")&&(five.getText() == "X")&&(one.getText()==" ")) {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="X")&&(six.getText() == "X")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="X")&&(nine.getText() == "X")&&(eight.getText()==" ")) {
eight.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="X")&&(three.getText() == "X")&&(two.getText()==" ")) {
two.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()=="X")&&(seven.getText() == "X")&&(four.getText()==" ")) {
four.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()=="X")&&(eight.getText() == "X")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()=="X")&&(nine.getText() == "X")&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()=="X")&&(three.getText() == "X")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((nine.getText()=="X")&&(one.getText() == "X")&&(five.getText()==" ")) {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()=="X")&&(five.getText() == "X")&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()==two.getText())&&(two.getText() == three.getText())&&(three.getText()==" ")) {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((four.getText()==five.getText())&&(five.getText() == six.getText())&&(six.getText()==" ")) {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((seven.getText()==eight.getText())&&(eight.getText() == nine.getText())&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()==four.getText())&&(four.getText() == seven.getText())&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((two.getText()==five.getText())&&(five.getText() == eight.getText())&&(eight.getText()==" ")) {
eight.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()==six.getText())&&(six.getText() == nine.getText())&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((one.getText()==five.getText())&&(five.getText() == nine.getText())&&(nine.getText()==" ")) {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else if((three.getText()==five.getText())&&(five.getText() == seven.getText())&&(seven.getText()==" ")) {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else {
if (rand == 1) {
if (one.getText()==" ") {
one.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 2) {
if (two.getText()==" ") {
two.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 3) {
if (three.getText()==" ") {
three.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 4) {
if (four.getText()==" ") {
four.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 5) {
if (five.getText()==" ") {
five.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 6) {
if (six.getText()==" ") {
six.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 7) {
if (seven.getText()==" ") {
seven.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 8) {
if (eight.getText()==" ") {
eight.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else if (rand == 9) {
if (nine.getText()==" ") {
nine.setText("O");
status.setText(playerX + "'s turn.");
Checker();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
else
AI();
}
else AI();
}
}
public void actionPerformed(ActionEvent event) {
Checker();
if (event.getSource() == help) {
playerX = JOptionPane.showInputDialog("Player X is " + playerX +
" and Player O is "
+ playerO +
". To change, enter player X's name.");
}
if (event.getSource() == redo) {
wins.setText("Wins: Computer (O): "+cwins+". Human (X): "+hwins+". Ties: "+ties+".");
one.setText(" ");
two.setText(" ");
three.setText(" ");
four.setText(" ");
five.setText(" ");
six.setText(" ");
seven.setText(" ");
eight.setText(" ");
nine.setText(" ");
one.setEnabled(true);
two.setEnabled(true);
three.setEnabled(true);
four.setEnabled(true);
five.setEnabled(true);
six.setEnabled(true);
seven.setEnabled(true);
eight.setEnabled(true);
nine.setEnabled(true);
xTurn=true;
play = (playerX + "'s turn.");
}
if(event.getSource()==multi) {
TicTacToeMulti cool = new TicTacToeMulti();
}
if(event.getSource() == exit) System.exit(0);
if (xTurn && event.getSource() == one) {
one.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == two) {
two.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == three) {
three.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == four) {
four.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == five) {
five.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == six) {
six.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == seven) {
seven.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == eight) {
eight.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
if (xTurn && event.getSource() == nine) {
nine.setText("X");
status.setText(playerO + "'s turn.");
Checker();
AI();
if (xTurn)
xTurn = false;
else if (! (xTurn))
xTurn = true;
}
}
public static void main(String[] args) {
TicTacToeAI eg = new TicTacToeAI();
}
}
/*Layout of Tic Tac Toe Board:
1 2 3 _|_|_
4 5 6 _|_|_
7 8 9 | |
*/
class TicTacToeMulti extends JFrame implements ActionListener
{
public String playerX = JOptionPane.showInputDialog("Enter player X's name.");
public String playerO = JOptionPane.showInputDialog("Enter player O's name.");
public float player = Math.round(Math.random()*10);
public String one1 = " ";
public String two2 = " ";
public String three3 = " ";
public String four4 = " ";
public String five5 = " ";
public String six6 = " ";
public String seven7 = " ";
public String eight8 = " ";
public String nine9 = " ";
public boolean xTurn = true;
public String play = (playerX+"'s turn.");
public double cwins = 0;
public double hwins = 0;
public double ties = 0;
JButton one = new JButton(one1);
JButton two = new JButton(two2);
JButton three = new JButton(three3);
JButton four = new JButton(four4);
JButton five = new JButton(five5);
JButton six = new JButton(six6);
JButton seven = new JButton(seven7);
JButton eight = new JButton(eight8);
JButton nine = new JButton(nine9);
JLabel status = new JLabel(play);
JButton redo = new JButton("Restart Game");
JButton help = new JButton("Who's who?");
JButton exit = new JButton("Exit");
JButton multi = new JButton("Multi-Player");
JButton single = new JButton("Single-Player");
JLabel lbl = new JLabel("Cinnamon's Tic-Tac-Toe");
JLabel wins = new JLabel("Wins: O: "+cwins+". X: "+hwins+". Ties: "+ties+".");
public TicTacToeMulti()
{
super("Tic-Tac-Toe");
setSize(800,725);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
redo.setFont(new Font("Arial", Font.PLAIN, 12));
help.setFont(new Font("Arial", Font.PLAIN, 12));
exit.setFont(new Font("Arial", Font.PLAIN, 12));
multi.setFont(new Font("Arial", Font.PLAIN, 12));
single.setFont(new Font("Arial", Font.PLAIN, 12));
one.setFont(new Font("Serif", Font.BOLD, 62));
two.setFont(new Font("Serif", Font.BOLD, 62));
three.setFont(new Font("Serif", Font.BOLD, 62));
four.setFont(new Font("Serif", Font.BOLD, 62));
five.setFont(new Font("Serif", Font.BOLD, 62));
six.setFont(new Font("Serif", Font.BOLD, 62));
seven.setFont(new Font("Serif", Font.BOLD, 62));
eight.setFont(new Font("Serif", Font.BOLD, 62));
nine.setFont(new Font("Serif", Font.BOLD, 62));
status.setFont(new Font("Times New Roman", Font.ITALIC+Font.BOLD, 24));
lbl.setFont(new Font("Cooper Black", Font.PLAIN, 45));
wins.setFont(new Font("Times New Roman", Font.PLAIN, 18));
Container contentArea = getContentPane();
GridBagLayout flowManager = new GridBagLayout();
GridBagConstraints pos = new GridBagConstraints();
contentArea.setLayout(flowManager);
getContentPane().setBackground(Color.RED);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
redo.addActionListener(this);
help.addActionListener(this);
exit.addActionListener(this);
single.addActionListener(this);
multi.addActionListener(this);
multi.setEnabled(false);
if(!(player>=5)){
play = "O's turn.";
xTurn=false;
}
else{
play = "X's turn.";
xTurn=true;
}
pos.gridx=1; pos.gridy=1;
contentArea.add(lbl, pos);
pos.gridx=0; pos.gridy=2;
contentArea.add(one,pos);
pos.gridx=1; pos.gridy=2;
contentArea.add(two,pos);
pos.gridx=2; pos.gridy=2;
contentArea.add(three,pos);
pos.gridx=0; pos.gridy=3;
contentArea.add(four,pos);
pos.gridx=1; pos.gridy=3;
contentArea.add(five,pos);
pos.gridx=2; pos.gridy=3;
contentArea.add(six,pos);
pos.gridx=0; pos.gridy=4;
contentArea.add(seven,pos);
pos.gridx=1; pos.gridy=4;
contentArea.add(eight,pos);
pos.gridx=2; pos.gridy=4;
contentArea.add(nine,pos);
pos.gridx=1; pos.gridy=6;
contentArea.add(status,pos);
pos.gridx=1; pos.gridy=9;
contentArea.add(redo,pos);
pos.gridx=1; pos.gridy=15;
contentArea.add(wins, pos);
pos.gridx = 1;
pos.gridy = 10;
contentArea.add(help, pos);
pos.gridx = 1;
pos.gridy = 11;
contentArea.add(exit, pos);
pos.gridx = 1;
pos.gridy = 12;
contentArea.add(multi, pos);
pos.gridx = 1;
pos.gridy = 13;
contentArea.add(single, pos);
setContentPane(contentArea);
Checker();
}
public void Checker()
{
if((one.getText()=="X" && two.getText()=="X" && three.getText()=="X")||
(four.getText()=="X" && five.getText()=="X" && six.getText()=="X")||
(seven.getText()=="X" && eight.getText()=="X" && nine.getText()=="X")||
(one.getText()=="X" && four.getText()=="X" && seven.getText()=="X")||
(two.getText()=="X" && five.getText()=="X" && eight.getText()=="X")||
(three.getText()=="X" && six.getText()=="X" && nine.getText()=="X")||
(one.getText()=="X" && five.getText()=="X" && nine.getText()=="X")||
(three.getText()=="X" && five.getText()=="X" && seven.getText()=="X")){
hwins+=1;
status.setText("X ("+playerX+") wins!!!");
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
}
else if((one.getText()=="O" && two.getText()=="O" && three.getText()=="O")||
(four.getText()=="O" && five.getText()=="O" && six.getText()=="O")||
(seven.getText()=="O" && eight.getText()=="O" && nine.getText()=="O")||
(one.getText()=="O" && four.getText()=="O" && seven.getText()=="O")||
(two.getText()=="O" && five.getText()=="O" && eight.getText()=="O")||
(three.getText()=="O" && six.getText()=="O" && nine.getText()=="O")||
(one.getText()=="O" && five.getText()=="O" && nine.getText()=="O")||
(three.getText()=="O" && five.getText()=="O" && seven.getText()=="O")){
status.setText("O ("+playerO+") wins!!!");
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
cwins+=1;
}
else if(one.getText()!=" " && two.getText()!=" " && three.getText()!=" " &&
four.getText()!=" " && five.getText()!=" " && six.getText()!=" " &&
seven.getText()!=" " && eight.getText()!=" " && nine.getText()!=" ")
{
status.setText("Cat's Game!");
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
ties+=1;
}
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==single) {
TicTacToeAI coolio = new TicTacToeAI();
}
if(event.getSource()==exit){
System.exit(0);
}
if(event.getSource()==help){
playerX=JOptionPane.showInputDialog("Player X is "+playerX+" and Player O is "
+playerO+". To change, enter player X's name.");
playerO=JOptionPane.showInputDialog("Enter player O's name.");
}
if(event.getSource()==redo){
wins.setText("Wins: O: "+cwins+". X: "+hwins+". Ties: "+ties+".");
one.setText(" ");
two.setText(" ");
three.setText(" ");
four.setText(" ");
five.setText(" ");
six.setText(" ");
seven.setText(" ");
eight.setText(" ");
nine.setText(" ");
one.setEnabled(true);
two.setEnabled(true);
three.setEnabled(true);
four.setEnabled(true);
five.setEnabled(true);
six.setEnabled(true);
seven.setEnabled(true);
eight.setEnabled(true);
nine.setEnabled(true);
xTurn = true;
play = (playerX + "'s turn.");
}
if(!(xTurn) && event.getSource()==one){
one.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==one){
one.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==two){
two.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==two){
two.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==three){
three.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==three){
three.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==four){
four.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==four){
four.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==five){
five.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==five){
five.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==six){
six.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==six){
six.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==seven){
seven.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==seven){
seven.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==eight){
eight.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==eight){
eight.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
if(!(xTurn) && event.getSource()==nine){
nine.setText("O");
status.setText("\n"+playerX+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
else if(xTurn && event.getSource()==nine){
nine.setText("X");
status.setText("\n"+playerO+"'s turn.\n\n");
Checker();
if(xTurn)xTurn=false;
else if(!(xTurn))xTurn=true;
}
}
}
moussa.eltayeb 0 Newbie Poster
Cudmore 9 Junior Poster in Training
howdoyoudothat? 0 Newbie Poster
sciwizeh 62 Posting Pro in Training
masijade 1,351 Industrious Poster Team Colleague Featured Poster
momik 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.