Note that there are some explanatory texts on larger screens.

plurals
  1. POSplitting Written code into Two Classes (JAVA)
    text
    copied!<p>I'm writing a tic-tac-toe game for my Java, I really suck at this so far, but got it to work with the examples he gave us in class. The problem I'm having now is that I realized wants us to have at least TWO classes for this program. I have no idea what he means by that or how I convert the code I've already put together into "Two Classes". From the instructions it looks like he wants the board in one class and the game in another class.</p> <p>Is there a way to split this up into two classes without totally re-writing the whole thing?</p> <pre><code>/* Standard applet template */ import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class TicTacToeGame implements ActionListener { /*Instance Variables*/ private JFrame window = new JFrame("Tic-Tac-Toe Game"); private JButton btn1 = new JButton(""); private JButton btn2 = new JButton(""); private JButton btn3 = new JButton(""); private JButton btn4 = new JButton(""); private JButton btn5 = new JButton(""); private JButton btn6 = new JButton(""); private JButton btn7 = new JButton(""); private JButton btn8 = new JButton(""); private JButton btn9 = new JButton(""); private JLabel lblTitle = new JLabel("Tic Tac Toe Game"); private JLabel lblBlank = new JLabel(" "); private String letter = ""; private int count = 0; private boolean win = false; public TicTacToeGame(){ /*Create Window*/ window.setSize(400,300); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLayout(new GridLayout(3,3)); /*Add Buttons To The Window*/ window.add(btn1); window.add(btn2); window.add(btn3); window.add(btn4); window.add(btn5); window.add(btn6); window.add(btn7); window.add(btn8); window.add(btn9); /*Add The Action Listener To The Buttons*/ btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); btn6.addActionListener(this); btn7.addActionListener(this); btn8.addActionListener(this); btn9.addActionListener(this); /*Make The Window Visible*/ window.setVisible(true); } public void actionPerformed(ActionEvent a) { count++; /*Calculate Who's Turn It Is*/ if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){ letter = "&lt;HTML&gt;&lt;font color=blue&gt;X&lt;/font&gt;&lt;/HTML&gt;"; } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){ letter = "&lt;HTML&gt;&lt;font color=red&gt;O&lt;/font&gt;&lt;/HTML&gt;"; } /*Display X's or O's on the buttons*/ if(a.getSource() == btn1){ btn1.setText(letter); btn1.setEnabled(false); } else if(a.getSource() == btn2){ btn2.setText(letter); btn2.setEnabled(false); } else if(a.getSource() == btn3){ btn3.setText(letter); btn3.setEnabled(false); } else if(a.getSource() == btn4){ btn4.setText(letter); btn4.setEnabled(false); } else if(a.getSource() == btn5){ btn5.setText(letter); btn5.setEnabled(false); } else if(a.getSource() == btn6){ btn6.setText(letter); btn6.setEnabled(false); } else if(a.getSource() == btn7){ btn7.setText(letter); btn7.setEnabled(false); } else if(a.getSource() == btn8){ btn8.setText(letter); btn8.setEnabled(false); } else if(a.getSource() == btn9){ btn9.setText(letter); btn9.setEnabled(false); } /*Checks to See Who Won*/ //horizontal win if( btn1.getText() == btn2.getText() &amp;&amp; btn2.getText() == btn3.getText() &amp;&amp; btn1.getText() != ""){ win = true; } else if(btn4.getText() == btn5.getText() &amp;&amp; btn5.getText() == btn6.getText() &amp;&amp; btn4.getText() != ""){ win = true; } else if(btn7.getText() == btn8.getText() &amp;&amp; btn8.getText() == btn9.getText() &amp;&amp; btn7.getText() != ""){ win = true; } //virticle win else if(btn1.getText() == btn4.getText() &amp;&amp; btn4.getText() == btn7.getText() &amp;&amp; btn1.getText() != ""){ win = true; } else if(btn2.getText() == btn5.getText() &amp;&amp; btn5.getText() == btn8.getText() &amp;&amp; btn2.getText() != ""){ win = true; } else if(btn3.getText() == btn6.getText() &amp;&amp; btn6.getText() == btn9.getText() &amp;&amp; btn3.getText() != ""){ win = true; } //diagonal wins else if(btn1.getText() == btn5.getText() &amp;&amp; btn5.getText() == btn9.getText() &amp;&amp; btn1.getText() != ""){ win = true; } else if(btn3.getText() == btn5.getText() &amp;&amp; btn5.getText() == btn7.getText() &amp;&amp; btn3.getText() != ""){ win = true; } else { win = false; } /*Show a dialog if someone wins or the game is tie*/ if(win == true){ JOptionPane.showMessageDialog(null, letter + " WINS!"); } else if(count == 9 &amp;&amp; win == false){ JOptionPane.showMessageDialog(null, "Tie Game!"); } } public static void main(String[] args){ new TicTacToeGame(); } } </code></pre>
 

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