Browse Source

Tic Tac Toe creating method for action after every turn

remotes/origin/develop
fdai6040 2 years ago
parent
commit
73d88c7e7f
  1. 35
      src/main/java/src/TicTacToeGame.java

35
src/main/java/src/TicTacToeGame.java

@ -10,6 +10,7 @@ public class TicTacToeGame implements ActionListener {
JPanel bt_panel = new JPanel();
JLabel textfield = new JLabel();
JButton[] bton = new JButton[9];
int chance_flag =0;
Random random = new Random();
boolean pl1_chance;
@ -41,7 +42,7 @@ public class TicTacToeGame implements ActionListener {
{
bton[i] = new JButton();
bt_panel.add(bton[i]);
bton[i].setFont(new Font("Serif", Font.BOLD, 120));
bton[i].setFont(new Font("Serif", Font.BOLD, 30));
bton[i].setFocusable(false);
bton[i].addActionListener(this);
bton[i].setBackground(Color.cyan);
@ -84,9 +85,37 @@ public class TicTacToeGame implements ActionListener {
//Method for perfoming action after every turn
//Method for performing action after every turn
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 9; i++)
{
if (e.getSource() == bton[i])
{
if (pl1_chance)
{
if (bton[i].getText() == "")
{
bton[i].setForeground(new Color(0, 188, 255));
bton[i].setText("X");
pl1_chance = false;
textfield.setText("O turn");
chance_flag++;
}
}
else
{
if (bton[i].getText() == "")
{
bton[i].setForeground(new Color(0, 255, 9));
bton[i].setText("O");
pl1_chance = true;
textfield.setText("X turn");
chance_flag++;
}
}
}
}
}
}
Loading…
Cancel
Save