Reputation: 377
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.
Is there a way to split this up into two classes without totally re-writing the whole thing?
/* 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 = "<HTML><font color=blue>X</font></HTML>";
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "<HTML><font color=red>O</font></HTML>";
}
/*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() && btn2.getText() == btn3.getText() && btn1.getText() != ""){
win = true;
}
else if(btn4.getText() == btn5.getText() && btn5.getText() == btn6.getText() && btn4.getText() != ""){
win = true;
}
else if(btn7.getText() == btn8.getText() && btn8.getText() == btn9.getText() && btn7.getText() != ""){
win = true;
}
//virticle win
else if(btn1.getText() == btn4.getText() && btn4.getText() == btn7.getText() && btn1.getText() != ""){
win = true;
}
else if(btn2.getText() == btn5.getText() && btn5.getText() == btn8.getText() && btn2.getText() != ""){
win = true;
}
else if(btn3.getText() == btn6.getText() && btn6.getText() == btn9.getText() && btn3.getText() != ""){
win = true;
}
//diagonal wins
else if(btn1.getText() == btn5.getText() && btn5.getText() == btn9.getText() && btn1.getText() != ""){
win = true;
}
else if(btn3.getText() == btn5.getText() && btn5.getText() == btn7.getText() && 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 && win == false){
JOptionPane.showMessageDialog(null, "Tie Game!");
}
}
public static void main(String[] args){
new TicTacToeGame();
}
}
Upvotes: 0
Views: 2865
Reputation: 4869
I think your comments are a good hint as to logical splits. Put each section into a separate method so that your actionPerformed
method would be something like the following.
public void actionPerformed(ActionEvent a) {
count++;
letter = calculateCurrentPlayer(count);
displayLetterOnButton(a.getSource());
win = checkForWin();
displayGameOver(win, count);
}
Each of the methods called from actionPerformed
would then have a small tightly-defined unit of work to perform. These methods could pretty much be copy-and-paste from the sections of your current actionsPerformed
method.
Upvotes: 0
Reputation: 5268
Joachim gave a nice answer. I just want to add that instead of doing this:
/*Display X's or O's on the buttons*/
if(a.getSource() == btn1){
btn1.setText(letter);
btn1.setEnabled(false);
...
} else if(a.getSource() == btn9){
btn9.setText(letter);
btn9.setEnabled(false);
}
You can do this:
JButton btn = (JButton) a.getSource();
btn.setText(letter);
btn.setEnabled(false);
Upvotes: 0
Reputation: 308041
There are multiple ways to split this class, but since it's not very big, most of them would end up rewriting much of it.
The cleanest way would be to split logic and UI into (at least) two separate classes:
TicTacToeGame
would implement the logic: it has a method to query who's turn it is, some methods to query the state of the playing field, a method to enter the next move, a method to query the state of the game (playing, end), a method to query who has won and probably a way to register some listenersTicTacToeUI
just takes the information of an instance of the other class and displays it. It also calls the appropriate methods when one button is clicked.The net effect will probably be more code (since the interaction between the two classes will need some code), but the code will be much cleaner and the game class can be tested independently of the UI.
Upvotes: 3
Reputation: 59660
Simple thing you can do is Make second class that implements ActionListener and then use that class to listen to all action events in your program. This is not the way you want that is one for board and one for game but this can be one for operation and one for presentation.
Upvotes: 0