Professional Documents
Culture Documents
● Win method works correctly. Display a pop up window when a player wins, and the
window should say which player won.
● If the game ends in a tie, display a pop up window saying it’s a tie.
● Counting the number of wins (and ties)
● Option to display the number of wins and ties
● Resetting the board when appropriate
● Some changes to the look of the game (colors, pictures, etc.)
Optional:
● Computer player
● Make the computer player semi intelligent
For TicTacToe2:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JMenu fileMenu = new JMenu("File"); //create a menu for the top of the frame
JMenuItem newGame = new JMenuItem("New Game");
JMenuItem winCount = new JMenuItem("Win Count");
newGame.addActionListener(new ActionListener(){ //add a listener to the menu
item
public void actionPerformed(ActionEvent e){
TicTacToePanel.resetBoard();
}
});
fileMenu.add(newGame);
fileMenu.add(winCount);
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
f.setJMenuBar(bar);
//f.pack();
f.setSize(600,600);
f.setVisible(true);
}
}
For TicTacToePanel:
import java.util.random.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
xTurn = true;
}
}
//method to check to see if there is a winner
public boolean win(){
//columns
if (tictac[0][0] == tictac[1][0] && tictac[1][0] == tictac[2][0] && tictac[2][0] == 1) {
return true;
}
else if (tictac[0][0] == tictac[1][0] && tictac[1][0] == tictac[2][0] && tictac[2][0] == 2)
{
return true;
}
if (tictac[0][1] == tictac[1][1] && tictac[1][1] == tictac[2][1] && tictac[2][1] == 1) {
return true;
}
else if (tictac[0][1] == tictac[1][1] && tictac[1][1] == tictac[2][1] && tictac[2][1] == 2)
{
return true;
}
if (tictac[0][2] == tictac[1][2] && tictac[1][2] == tictac[2][2] && tictac[2][2] == 1) {
return true;
}
else if (tictac[0][2] == tictac[1][2] && tictac[1][2] == tictac[2][2] && tictac[2][2] == 2)
{
return true;
}
//rows
if (tictac[0][0] == tictac[0][1] && tictac[0][1] == tictac[0][2] && tictac[0][2] == 1) {
return true;
}
else if (tictac[0][0] == tictac[0][1] && tictac[0][1] == tictac[0][2] && tictac[0][2] == 2)
{
return true;
}
if (tictac[1][0] == tictac[1][1] && tictac[1][1] == tictac[1][2] && tictac[1][2] == 1) {
return true;
}
else if (tictac[1][0] == tictac[1][1] && tictac[1][1] == tictac[1][2] && tictac[1][2] == 2)
{
return true;
}
if (tictac[2][0] == tictac[2][1] && tictac[2][1] == tictac[2][2] && tictac[2][2] == 1) {
return true;
}
else if (tictac[2][0] == tictac[2][1] && tictac[2][1] == tictac[2][2] && tictac[2][2] == 2)
{
return true;
}
//diagonals
if (tictac[0][0] == tictac[1][1] && tictac[1][1] == tictac[2][2] && tictac[2][2] == 1) {
return true;
}
else if (tictac[0][0] == tictac[1][1] && tictac[1][1] == tictac[2][2] && tictac[2][2] == 2)
{
return true;
}
//will execute when a button is clicked, as long as the listener was added to the button
public class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton current = (JButton)e.getSource(); //get the source of the event
and cast it to a JButton
if(win()) {
if(xTurn) //if xTurn is true, then O just played
JOptionPane.showMessageDialog(null, "O wins!!");
//display a pop up window when a player wins
else
JOptionPane.showMessageDialog(null, "X wins!!");
//display a pop up window when a player wins
}
if(win()) {
if (xTurn)
play1wins++;
else
play2wins++;
}
}
}