Tic Tac Toe Java

Discussion in 'School Work Help' started by xaznxryux, Jun 17, 2008.

  1. xaznxryux

    xaznxryux Well-Known Member

    2,723
    392
    468
    ok, i study C++ at school and i happen to have a friend who studies Java in High school. Since java and C++ are similar i guess i can help her a little. The code she gave me looks kind of right, i cant seem to find the problem. Want to see who can help me. Thanks in Advance

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    public class TicTacToe {

    private final static int[][] winChances = new int[][] {

    {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
    {0, 3, 6}, {1, 4, 7}, {2, 5, 8},

    {0, 4, 8}, {2, 4, 6}
    };

    private final JFrame frame = new JFrame("Tic Tac Toe");


    private final JButton[] buttons = new JButton[9];

    private String turn = "X";


    public TicTacToe() {
    final JPanel mainPanel = new JPanel(new GridLayout(3, 3));

    final ActionListener buttonListener = new ButtonListener();
    for (int i = 0; i < 9; i++) {

    this.buttons = new JButton();
    mainPanel.add(this.buttons);
    this.buttons.addActionListener(buttonListener);
    }

    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
    this.frame.setBounds(200, 200, 300, 300);
    this.frame.setVisible(true);
    }


    private void checkEndgame() {
    for (final int[] winChance : TicTacToe.winChances) {

    if (this.checkVictory(winChance)) {
    final String winner = this.turn.equals("X") ? "O" : "X";

    JOptionPane.showMessageDialog(this.frame, winner + " won the game!",
    "Game Over", JOptionPane.INFORMATION_MESSAGE);
    this.reset();

    return;
    }
    }

    for (final JButton button : this.buttons) {

    if (button.getText().equals("")) {
    return;
    }
    }


    JOptionPane.showMessageDialog(this.frame, "This game is a draw.", "Game Over",
    JOptionPane.INFORMATION_MESSAGE);
    this.reset();

    }

    private boolean checkVictory(final int[] winChance) {

    final String firstButtonText = this.buttons[winChance[0]].getText();

    final boolean firstSecondEqual =

    firstButtonText.equals(this.buttons[winChance[1]].getText());
    final boolean firstThirdEqual =
    firstButtonText.equals(this.buttons[winChance[2]].getText());


    return (!firstButtonText.equals("")) && firstSecondEqual && firstThirdEqual;
    }

    private void reset() {

    this.turn = "X";
    for (int i = 0; i < 9; i++) {
    this.buttons.setText("");

    this.buttons.setEnabled(true);
    }
    }

    private class ButtonListener implements ActionListener {

    public void actionPerformed(final ActionEvent ae) {
    final JButton source = (JButton) ae.getSource();

    source.setText(TicTacToe.this.turn);
    source.setEnabled(false);
    if (TicTacToe.this.turn.equals("X")) {

    TicTacToe.this.turn = "O";
    } else {
    TicTacToe.this.turn = "X";

    }

    TicTacToe.this.checkEndgame();
    }
    }

    public static void main(final String[] argv) {

    new TicTacToe();
    }
    }
     
  2. ender

    ender Well-Known Member

    55
    31
    0
    What happens if you run it? Any runtime errors or is there some errors in the logic?
     
  3. reno

    reno Well-Known Member

    should post up what the error is

    use some debugger or IDE and they'll identify which line breaks down =)
     
  4. noonedoes

    noonedoes Member

    11
    26
    0
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);

    There shouldn't be a space between ON _CLOSE

    it should be
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Once you fix that error, it will compile and run fine.