Modify the tic tac toe game at the bottom of the code to make sure that it works.
Follow these steps:
1. Finish calculating if someone won. You need to add ifs for Row 2, Row 3, Column 1, Column 2, Column 3 and the 2 Diagonals.
//Calculate if someone won
String winner = "__";
if (b11.getText ().equals (b12.getText ()) && b11.getText ().equals (b13.getText ()) && !b11.getText ().equals ("__"))
{
JOptionPane.showMessageDialog (null, b11.getText () + " won!", "We have a winner!", JOptionPane.ERROR_MESSAGE);
winner = b11.getText ();
}
This checks if b11=b12=b13 and that b11 isn't a blank. That means that someone won because they have a row.
2. Make it so they can’t cheat. Fix up the other ifs at the beginning of actionPerformed so they look like this:
//Switch the button's label if (e.getActionCommand ().equals ("11") && b11.getText ().equals ("__")) b11.setText (turn);This makes it some they can only click on buttons that the other person hasn't clicked on.
3. Change the GUI so it looks better.
4. Keeping track of the score and the turn on the screen.
import javax.swing.*; import java.applet.*; import java.awt.*; import java.awt.event.*;
public class TicTac extends Applet implements ActionListener
{
String turn = "X";
JButton b11, b12, b13, b21, b22, b23, b31, b32, b33;
int xWins = 0;
int oWins = 0;
public void init ()
{
Panel p = new Panel (new GridLayout (3, 3));
b11 = new JButton ("__");
b11.setActionCommand ("11");
b11.addActionListener (this);
p.add (b11);
b12 = new JButton ("__");
b12.setActionCommand ("12");
b12.addActionListener (this);
p.add (b12);
b13 = new JButton ("__");
b13.setActionCommand ("13");
b13.addActionListener (this);
p.add (b13);
b21 = new JButton ("__");
b21.setActionCommand ("21");
b21.addActionListener (this);
p.add (b21);
b22 = new JButton ("__");
b22.setActionCommand ("22");
b22.addActionListener (this);
p.add (b22);
b23 = new JButton ("__");
b23.setActionCommand ("23");
b23.addActionListener (this);
p.add (b23);
b31 = new JButton ("__");
b31.setActionCommand ("31");
b31.addActionListener (this);
p.add (b31);
b32 = new JButton ("__");
b32.setActionCommand ("32");
b32.addActionListener (this);
p.add (b32);
b33 = new JButton ("__");
b33.setActionCommand ("33");
b33.addActionListener (this);
p.add (b33);
add (p);
}
public void actionPerformed (ActionEvent e)
{ //Switch the button's label
if (e.getActionCommand ().equals ("11") && b11.getText ().equals ("__"))
b11.setText (turn);
else if (e.getActionCommand ().equals ("12"))
b12.setText (turn);
else if (e.getActionCommand ().equals ("13"))
b13.setText (turn);
else if (e.getActionCommand ().equals ("21"))
b21.setText (turn);
else if (e.getActionCommand ().equals ("22"))
b22.setText (turn);
else if (e.getActionCommand ().equals ("23"))
b23.setText (turn);
else if (e.getActionCommand ().equals ("31"))
b31.setText (turn);
else if (e.getActionCommand ().equals ("32"))
b32.setText (turn);
else if (e.getActionCommand ().equals ("33"))
b33.setText (turn);
//Calculate if someone won
String winner = "__";
if (b11.getText ().equals (b12.getText ()) && b11.getText ().equals (b13.getText ()) && !b11.getText ().equals ("__"))
{
JOptionPane.showMessageDialog (null, b11.getText () + " won!", "We have a winner!", JOptionPane.ERROR_MESSAGE);
winner = b11.getText ();
}
//If someone won, reset and display new score
if (!winner.equals ("__"))
{
b11.setText ("__");
b12.setText ("__");
b13.setText ("__");
b21.setText ("__");
b22.setText ("__");
b23.setText ("__");
b31.setText ("__");
b32.setText ("__");
b33.setText ("__");
if (winner.equals ("X"))
xWins++;
else
oWins++;
showStatus ("X's points: " + xWins + " O's points: " + oWins);
}
//Switch whose turn it was
if (turn.equals ("X"))
turn = "O";
else
turn = "X";
}
}