0

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;
            }
        }
    }
}
Bv44
  • 1
  • 1
  • 1
    You should definitely avoid long if-statements so that the code doesn't get confusing – verity Jul 13 '20 at 22:48
  • 1
    And you should definitely make more empty lines in your code, otherwise it will just become confusing – verity Jul 13 '20 at 22:50
  • And why do you use Kotlin in your Java code? – verity Jul 13 '20 at 22:51
  • 1
    Does this answer your question? [Why doesn't my equality comparison using = (a single equals) work correctly in Java?](https://stackoverflow.com/questions/39385382/why-doesnt-my-equality-comparison-using-a-single-equals-work-correctly-in-j) – PiRocks Jul 14 '20 at 00:23
  • 1
    After fixing the `gameOver` issued pointed out by PiRocks, every time you call `usrMove.main();` inside your loop, your `board` variable is going to be recreated and set to all blanks...you need to move that declaration out to class level and make it static. – Idle_Mind Jul 14 '20 at 00:27
  • @Verity Where do you see Kotlin getting used, the code shown is perfectly fine for Java 10 or higher (`var` was introduced in Java 10). – Mark Rotteveel Jul 14 '20 at 17:34

1 Answers1

3

gameOver = false should be gameOver == false, or !gameOver.

gameOver = false is an expression which sets gameOver to false and then evaluates to false, causing your do while loop to exit.

In future you can figure this kind of thing out using a debugger. You can find instructions on how to use a debugger with your editor/IDE by googling/duck-duck-going.

PiRocks
  • 1,708
  • 2
  • 18
  • 29