Browse Source

TicTacToeGame String values were compared using '==', replaced it with 'equals()' method

remotes/origin/develop
fdai6040 2 years ago
parent
commit
b52a343ace
  1. 3
      src/main/java/src/TasksFrame.java
  2. 38
      src/main/java/src/TicTacToeGame.java

3
src/main/java/src/TasksFrame.java

@ -8,9 +8,6 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.awt.Color;
import static java.lang.Double.valueOf;
public class TasksFrame extends JFrame { public class TasksFrame extends JFrame {

38
src/main/java/src/TicTacToeGame.java

@ -61,7 +61,7 @@ public class TicTacToeGame implements ActionListener {
try try
{ {
textfield.setText("Loading..."); textfield.setText("Loading...");
Thread.sleep(4000);
Thread.sleep(2000);
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {
@ -100,66 +100,66 @@ public class TicTacToeGame implements ActionListener {
} }
//creating method for checking winner //creating method for checking winner
public void chek(){ 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); oWin(6, 7, 8);
} else if (chance_flag==9) { } else if (chance_flag==9) {
@ -167,7 +167,7 @@ public class TicTacToeGame implements ActionListener {
gameOver("Game Draw"); 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); oWin(3,4,5);
} }
@ -214,7 +214,7 @@ public class TicTacToeGame implements ActionListener {
{ {
if (pl1_chance) if (pl1_chance)
{ {
if (bton[i].getText() == "")
if (Objects.equals(bton[i].getText(), ""))
{ {
bton[i].setForeground(new Color(0, 188, 255)); bton[i].setForeground(new Color(0, 188, 255));
bton[i].setText("X"); bton[i].setText("X");
@ -225,7 +225,7 @@ public class TicTacToeGame implements ActionListener {
} }
else else
{ {
if (bton[i].getText() == "")
if (Objects.equals(bton[i].getText(), ""))
{ {
bton[i].setForeground(new Color(0, 255, 9)); bton[i].setForeground(new Color(0, 255, 9));
bton[i].setText("O"); bton[i].setText("O");

Loading…
Cancel
Save