Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well I took the code that you provided and after significant finagling was able to make a fully functioning Tic-Tac-Toe game. You were mostly on the right track with what you were doing you just needed to first begin with a design.</p> <p>In my NoughtsCrosses class I have the following:</p> <ul> <li>class Action implements ActionListener <ul> <li>This has a JButton attribute that I pass in through a constructor</li> <li>In the actionPerformed <ul> <li>set the text</li> <li>disable the button</li> <li>increment the counter</li> <li>check if someone wins <ul> <li>If there is a winner or draw game ends set the "Play again?" text</li> <li>else call the changeTurn function</li> </ul></li> </ul></li> </ul></li> <li>class Reset implements ActionListenter <ul> <li>This has a JButton attribute that I pass in through a constructor</li> <li>In the actionPerformed <ul> <li>I call the resetGame function</li> </ul></li> </ul></li> <li>function changeTurn</li> <li>function resetGame</li> <li>function checkForWinners</li> </ul> <p>as a hint, this is my implementation of the Action class and an example of the constructor I mentioned</p> <pre><code>class Action implements ActionListener{ private JButton button; public Action(JButton button){ this.button = button; } public void actionPerformed(ActionEvent e) { button.setText(letter); button.setEnabled(false); counter++; boolean gameOver = checkForWinners(); if(!gameOver) changeTurn(); else{ newgame.setText("Play again?"); newgame.addActionListener(resetButton); } } } </code></pre> <p>a call like <code>new Action(square[i])</code> is what you need to make this work. Note: the <code>resetButton</code> is of the <code>Reset</code> class I mention above much like the <code>Action</code> class it has the same construct that I passed <code>newgame</code> into.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload