Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Please help with Tic Tac Toe Game winner status

tonynsxtonynsx Posts: 1Member
Hi everybody,

I'm new to Java. I'm learning GUI now. My problem is that, the label for my tic tac toe game doesn't work. Please help.
[code]
package game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class gamePanel extends JPanel{

private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
private String mark;
private int count=0;
private JLabel label;

public gamePanel()
{
ButtonListener listener = new ButtonListener();

setPreferredSize(new Dimension(450,450));
setLayout(new GridLayout(4,3));

b1=new JButton(" ");
b1.addActionListener(listener);
add(b1);

b2=new JButton(" ");
b2.addActionListener(listener);
add(b2);

b3=new JButton(" ");
b3.addActionListener(listener);
add(b3);

b4=new JButton(" ");
b4.addActionListener(listener);
add(b4);

b5=new JButton(" ");
b5.addActionListener(listener);
add(b5);

b6=new JButton(" ");
b6.addActionListener(listener);
add(b6);

b7=new JButton(" ");
b7.addActionListener(listener);
add(b7);

b8=new JButton(" ");
b8.addActionListener(listener);
add(b8);

b9=new JButton(" ");
b9.addActionListener(listener);
add(b9);

label = new JLabel("Tic Tac Toe");
label.setFont(new Font("Helvetica", Font.BOLD, 24));
label.setForeground(Color.red);
add(label);
}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
count++;

if(count %2 !=0)
{
mark="X";
}
else
{
mark="O";
}

if(e.getSource()==b1)
{
b1.setText(mark);
b1.setFont(new Font("Helvetica", Font.BOLD, 72));
b1.setEnabled(false);
}
else if(e.getSource()==b2)
{
b2.setText(mark);
b2.setFont(new Font("Helvetica", Font.BOLD, 72));
b2.setEnabled(false);
}
else if(e.getSource()==b3)
{
b3.setText(mark);
b3.setFont(new Font("Helvetica", Font.BOLD, 72));
b3.setEnabled(false);
}
else if(e.getSource()==b4)
{
b4.setText(mark);
b4.setFont(new Font("Helvetica", Font.BOLD, 72));
b4.setEnabled(false);
}
else if(e.getSource()==b5)
{
b5.setText(mark);
b5.setFont(new Font("Helvetica", Font.BOLD, 72));
b5.setEnabled(false);
}
else if(e.getSource()==b6)
{
b6.setText(mark);
b6.setFont(new Font("Helvetica", Font.BOLD, 72));
b6.setEnabled(false);
}
else if(e.getSource()==b7)
{
b7.setText(mark);
b7.setFont(new Font("Helvetica", Font.BOLD, 72));
b7.setEnabled(false);
}
else if(e.getSource()==b8)
{
b8.setText(mark);
b8.setFont(new Font("Helvetica", Font.BOLD, 72));
b8.setEnabled(false);
}
else if (e.getSource()==b9)
{
b9.setText(mark);
b9.setFont(new Font("Helvetica", Font.BOLD, 72));
b9.setEnabled(false);
}
}
}

public void result()
{
if(b1.getText() == b2.getText() && b2.getText() == b3.getText())
label.setText("Player wins.");
else if(b4.getText() == b5.getText() && b5.getText() == b6.getText())
label.setText("Player wins.");
else if(b7.getText() == b8.getText() && b8.getText() == b9.getText())
label.setText("Player wins.");
else if(b1.getText() == b4.getText() && b4.getText() == b7.getText())
label.setText("Player wins.");
else if(b2.getText() == b5.getText() && b5.getText() == b8.getText())
label.setText("Player wins.");
else if(b3.getText() == b6.getText() && b6.getText() == b9.getText())
label.setText("Player wins.");
else if(b1.getText() == b5.getText() && b5.getText() == b9.getText())
label.setText("Player wins.");
else if(b3.getText() == b5.getText() && b5.getText() == b7.getText())
label.setText("Player wins.");
else
label.setText("Computer wins");

}

}


[/code]
Sign In or Register to comment.