I made this java code for win detection for the first part of my tic tac toe program. It prompts you to input a move, then immediately says "player wins." What can I do to fix this?
Main class
Runs the main loop and checks if the do-while loop is working.
package com.company;
public class Main {
public static boolean gameOver = false;
public static void main(String[] args) {
do {
System.out.println("inside parent do loop");
usrMove.main();
}
while (gameOver = false) ;
System.out.println("Player wins");
}
}
usrMove class
Checks if the player has won through a looping series of if else statements.
Scans the 2, 4, 5, 6 and 8 to see if there are two adjacent x's.
package com.company;
import java.util.Scanner;
public class usrMove {
public static void main(){
System.out.println("usrMove top");
String[] board = {"", "", "", "", "", "", "", "", ""}; //sets up the board with 9 empty spaces
var scanner = new Scanner(System.in);
System.out.print("Enter your move:");
var usrMove = scanner.nextLine();
System.out.println("Your move is:" + usrMove);
for(int i = 1; i < 10; i++){
if (Integer.parseInt(usrMove) == i){
board[i] = "x"; //changes the value of the board based on user play
}
}
for(int j = 2; j != 3 && j != 7; j++){ //determines if a player has won by checking if there is a piece on either side of the one being checked
if(board[j].equals("x") && board[j - 1].equals("x") && board[j - 2].equals("x")){
System.out.println("+-1");
} else if(board[j + 1].equals("x") && board[j - 1].equals("x") && board[j - 3].equals("x")){
System.out.println("+-2");
} else if(board[j + 2].equals("x") && board[j - 1].equals("x") && board[j - 4].equals("x")){
System.out.println("+-3");
} else if(board[j + 3].equals("x") && board[j - 1].equals("x") && board[j - 5].equals("x")){
System.out.println("+-4");
} else{
Main.gameOver = false;
}
}
}
}