From 73d88c7e7f0e28ab06b9eecbce0867feffcdcb2a Mon Sep 17 00:00:00 2001 From: fdai6040 Date: Tue, 7 Feb 2023 00:51:19 +0100 Subject: [PATCH] Tic Tac Toe creating method for action after every turn --- src/main/java/src/TicTacToeGame.java | 35 +++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/src/TicTacToeGame.java b/src/main/java/src/TicTacToeGame.java index 2f4d326..8a6295f 100644 --- a/src/main/java/src/TicTacToeGame.java +++ b/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++; + + } + } + } + } } }