From b52a343ace5a88be0805ca9b12f451afd7971c46 Mon Sep 17 00:00:00 2001 From: fdai6040 Date: Tue, 7 Feb 2023 14:29:24 +0100 Subject: [PATCH] TicTacToeGame String values were compared using '==', replaced it with 'equals()' method --- src/main/java/src/TasksFrame.java | 3 --- src/main/java/src/TicTacToeGame.java | 38 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/main/java/src/TasksFrame.java b/src/main/java/src/TasksFrame.java index e9f028c..0c964ae 100644 --- a/src/main/java/src/TasksFrame.java +++ b/src/main/java/src/TasksFrame.java @@ -8,9 +8,6 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.LinkedList; -import java.awt.Color; - -import static java.lang.Double.valueOf; public class TasksFrame extends JFrame { diff --git a/src/main/java/src/TicTacToeGame.java b/src/main/java/src/TicTacToeGame.java index a96ad41..ffee6d7 100644 --- a/src/main/java/src/TicTacToeGame.java +++ b/src/main/java/src/TicTacToeGame.java @@ -61,7 +61,7 @@ public class TicTacToeGame implements ActionListener { try { textfield.setText("Loading..."); - Thread.sleep(4000); + Thread.sleep(2000); } catch (InterruptedException e) { @@ -100,66 +100,66 @@ public class TicTacToeGame implements ActionListener { } //creating method for checking winner public void chek(){ - if ((bton[0].getText() == "X") && (bton[1].getText() == "X") && (bton[2].getText() == "X")) + if ((Objects.equals(bton[0].getText(), "X")) && (Objects.equals(bton[1].getText(), "X")) && (Objects.equals(bton[2].getText(), "X"))) { xWin(0, 1, 2); } - else if ((bton[0].getText() == "X") && (bton[4].getText() == "X") && (bton[8].getText() == "X")) + else if ((Objects.equals(bton[0].getText(), "X")) && (Objects.equals(bton[4].getText(), "X")) && (Objects.equals(bton[8].getText(), "X"))) { xWin(0, 4, 8); } - else if ((bton[0].getText() == "X") && (bton[3].getText() == "X") && (bton[6].getText() == "X")) + else if ((Objects.equals(bton[0].getText(), "X")) && (Objects.equals(bton[3].getText(), "X")) && (Objects.equals(bton[6].getText(), "X"))) { xWin(0, 3, 6); } - else if ((bton[1].getText() == "X") && (bton[4].getText() == "X") && (bton[7].getText() == "X")) + else if ((Objects.equals(bton[1].getText(), "X")) && (Objects.equals(bton[4].getText(), "X")) && (Objects.equals(bton[7].getText(), "X"))) { xWin(1, 4, 7); } - else if ((bton[2].getText() == "X") && (bton[4].getText() == "X") && (bton[6].getText() == "X")) + else if ((Objects.equals(bton[2].getText(), "X")) && (Objects.equals(bton[4].getText(), "X")) && (Objects.equals(bton[6].getText(), "X"))) { xWin(2, 4, 6); } - else if ((bton[2].getText() == "X") && (bton[5].getText() == "X") && (bton[8].getText() == "X")) + else if ((Objects.equals(bton[2].getText(), "X")) && (Objects.equals(bton[5].getText(), "X")) && (Objects.equals(bton[8].getText(), "X"))) { xWin(2, 5, 8); } - else if ((bton[3].getText() == "X") && (bton[4].getText() == "X") && (bton[5].getText() == "X")) + else if ((Objects.equals(bton[3].getText(), "X")) && (Objects.equals(bton[4].getText(), "X")) && (Objects.equals(bton[5].getText(), "X"))) { xWin(3, 4, 5); } - else if ((bton[6].getText() == "X") && (bton[7].getText() == "X") && (bton[8].getText() == "X")) + else if ((Objects.equals(bton[6].getText(), "X")) && (Objects.equals(bton[7].getText(), "X")) && (Objects.equals(bton[8].getText(), "X"))) { xWin(6, 7, 8); } - else if ((bton[0].getText() == "O") && (bton[1].getText() == "O") && (bton[2].getText() == "O")) + else if ((Objects.equals(bton[0].getText(), "O")) && (Objects.equals(bton[1].getText(), "O")) && (Objects.equals(bton[2].getText(), "O"))) { oWin(0, 1, 2); } - else if ((bton[0].getText() == "O") && (bton[3].getText() == "O") && (bton[6].getText() == "O")) + else if ((Objects.equals(bton[0].getText(), "O")) && (Objects.equals(bton[3].getText(), "O")) && (Objects.equals(bton[6].getText(), "O"))) { oWin(0, 3, 6); } - else if ((bton[0].getText() == "O") && (bton[4].getText() == "O") && (bton[8].getText() == "O")) + else if ((Objects.equals(bton[0].getText(), "O")) && (Objects.equals(bton[4].getText(), "O")) && (Objects.equals(bton[8].getText(), "O"))) { oWin(0, 4, 8); } - else if ((bton[1].getText() == "O") && (bton[4].getText() == "O") && (bton[7].getText() == "O")) + else if ((Objects.equals(bton[1].getText(), "O")) && (Objects.equals(bton[4].getText(), "O")) && (Objects.equals(bton[7].getText(), "O"))) { oWin(1, 4, 7); } - else if ((bton[2].getText() == "O") && (bton[4].getText() == "O") && (bton[6].getText() == "O")) + else if ((Objects.equals(bton[2].getText(), "O")) && (Objects.equals(bton[4].getText(), "O")) && (Objects.equals(bton[6].getText(), "O"))) { oWin(2, 4, 6); } - else if ((bton[2].getText() == "O") && (bton[5].getText() == "O") && (bton[8].getText() == "O")) + else if ((Objects.equals(bton[2].getText(), "O")) && (Objects.equals(bton[5].getText(), "O")) && (Objects.equals(bton[8].getText(), "O"))) { oWin(2, 5, 8); } - else if ((bton[6].getText() == "O") && (bton[7].getText() == "O") && (bton[8].getText() == "O")) + else if ((Objects.equals(bton[6].getText(), "O")) && (Objects.equals(bton[7].getText(), "O")) && (Objects.equals(bton[8].getText(), "O"))) { oWin(6, 7, 8); } else if (chance_flag==9) { @@ -167,7 +167,7 @@ public class TicTacToeGame implements ActionListener { gameOver("Game Draw"); } - else if ((bton[3].getText()== "O") && (bton[4].getText() == "O") && (bton[5].getText() == "O")) + else if ((Objects.equals(bton[3].getText(), "O")) && (Objects.equals(bton[4].getText(), "O")) && (Objects.equals(bton[5].getText(), "O"))) oWin(3,4,5); } @@ -214,7 +214,7 @@ public class TicTacToeGame implements ActionListener { { if (pl1_chance) { - if (bton[i].getText() == "") + if (Objects.equals(bton[i].getText(), "")) { bton[i].setForeground(new Color(0, 188, 255)); bton[i].setText("X"); @@ -225,7 +225,7 @@ public class TicTacToeGame implements ActionListener { } else { - if (bton[i].getText() == "") + if (Objects.equals(bton[i].getText(), "")) { bton[i].setForeground(new Color(0, 255, 9)); bton[i].setText("O");