I'm creating my first Swing application -> Tic Tac Toe. I've seen a lot of tutorials but I still can't understand three things:
The first is: How to create methods and use them in my code (e. g. isWinner
method in my code below)? I added it to actionPerformed
but it still won't do a thing. How should I call it to make it work real-time?
The second is: I'm not a fan of static
methods. Shall I create an object of my class and then call methods?
The third is: Is my isWinner
method ok? Or should I change the way I'm trying to check if the winner appears?
I saw a lot of content on the Internet and examples of creating games like that.
EDIT
Changed the isWinner
method. Now I call it from the actionPerformed
method. Still does nothing...
package one.more.time;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class GameWindow extends JFrame implements ActionListener{
String player = "X";
JButton but[] = new JButton[9];
JLabel status = new JLabel("Start!");
public GameWindow() {
setLayout(new BorderLayout());
for (int i = 0; i < but.length; i++){
but[i] = new JButton(Integer.toString(i+1));
but[i].addActionListener(this);
}
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < but.length; i++){
gamePanel.add(but[i]);
}
add(gamePanel, BorderLayout.CENTER);
add(status, BorderLayout.SOUTH);
setSize(new Dimension(300, 300));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GameWindow();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
JButton src = (JButton) e.getSource();
src.setActionCommand("player");
src.setText(player);
src.setEnabled(false);
if (isWinner(but, player)) {
System.exit(0);
};
}
boolean isWinner(JButton butt[], String player){
JButton but[] = butt;
for (int i = 0; i<3; i++){
if (but[i].getActionCommand().equals(but[i+3].getActionCommand()) &&
but[i+3].getActionCommand().equals(but[i+6].getActionCommand()) &&
but[i].getActionCommand().equals(but[i+6].getActionCommand()) &&
but[i].getActionCommand().equals(player))
{
return true;
}
}
for (int i = 0; i<9; i+=3)
if (but[i].getActionCommand().equals(but[i+1].getActionCommand()) &&
but[i+1].getActionCommand().equals(but[i+2].getActionCommand()) &&
but[i].getActionCommand().equals(but[i+2].getActionCommand()) &&
but[i].getActionCommand().equals(player))
{
return true;
}
if (but[0].getActionCommand().equals(but[4].getActionCommand()) &&
but[4].getActionCommand().equals(but[8].getActionCommand())&&
but[0].getActionCommand().equals(but[8].getActionCommand()) &&
but[0].getActionCommand().equals(player))
{
return true;
}
if (but[2].getActionCommand().equals(but[4].getActionCommand()) &&
but[4].getActionCommand().equals(but[6].getActionCommand()) &&
but[2].getActionCommand().equals(but[6].getActionCommand()) &&
but[2].getActionCommand().equals(player))
{
return true;
}
return false;
}
}
1-2. Dont make isWinner static. That would allow you to call it from inside your class other non static methods: such as the actionPerformed method.
but [I]==but [I+3]
- you just compare references of the different JButtons which would be false always. You need to compare the marked player behind them:
but [I].getActionCommand ().equals ( but [I+3].getActionCommand ());
Edit:
Inside the actionPerformed method don't write src.setActionCommand("player");
Replace it of course with the player's actual value: src.setActionCommand(player);
A slightly cleaner isWinner method:
boolean isWinner(JButton buttons[], String player)
{
for (int i = 0; i < 3; i++)
{
if (buttons[i].getActionCommand().equals(player) &&
buttons[i + 3].getActionCommand().equals(player) &&
buttons[i + 6].getActionCommand().equals(player))
{
return true;
}
}
for (int i = 0; i < 9; i += 3)
if (buttons[i].getActionCommand().equals(player) &&
buttons[i + 1].getActionCommand().equals(player) &&
buttons[i + 2].getActionCommand().equals(player))
{
return true;
}
if (buttons[0].getActionCommand().equals(player) &&
buttons[4].getActionCommand().equals(player) &&
buttons[8].getActionCommand().equals(player))
{
return true;
}
return buttons[2].getActionCommand().equals(player) &&
buttons[4].getActionCommand().equals(player) &&
buttons[6].getActionCommand().equals(player);
}