1

I have an assignment where I need to create a game that lets you move a piece around a 10x10 board. You move to the "^" space and you win. This is the code I have so far but I'm having problems with it. I put a break in the code for when the player enters 9 its suppose to quit the game. When the break is in there though, I get an unreachable code error. If I take it out, the game works fine except it says game over after every input and you cant quit the game like you're suppose too. Am I putting it in the wrong spot? Or is there a better way to have the game quit if a player enters 9 for their move?

import java.util.Scanner;
import java.util.Random;

public class MineWalker {
    public static final int BOARD_SIZE = 10;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int pX = 0;
        int pY = 0;
        Random r = new Random();
        int gX = r.nextInt(BOARD_SIZE);
        int gY = r.nextInt(BOARD_SIZE);

        Spaces[][] board = new Spaces[BOARD_SIZE][BOARD_SIZE];

        for (int y = 0; y < board.length; y++) //clears board
        {
            for (int x = 0; x < board[y].length; x++) {
                board[x][y] = Spaces.Empty;
            }
        }
        board[pX][pY] = Spaces.Player;
        board[gX][gY] = Spaces.Goal;

        System.out.println("Welcome to Mine Walker. Get the ice cream cone and avoid the mines");

        boolean gameOver = false;
        while (gameOver == false) //game loop
        {
            for (int y = 0; y < board.length; y++) //Draws board
            {
                for (int x = 0; x < board[y].length; x++) {
                    switch (board[x][y]) {
                        case Empty:
                            System.out.print("_");
                            break;
                        case Player:
                            System.out.print("X");
                            break;
                        case Goal:
                            System.out.print("^");
                            break;
                    }
                }
                System.out.println();
            }
            System.out.println("Enter either a -1, 0, or 1 in the X or 9 to quit");//moves game piece
            int dx = keyboard.nextInt();
            if (dx == 9) ;
            {
                System.out.println("Game Over");
                break;
            }
            System.out.println("Enter either a -1,0, or 1 in the Y"); // Unreachable statement here
            int dy = keyboard.nextInt();

            if (dx < -1 || dx > 1) {
                System.out.println("Invalid x");
                dx = 0;
            }
            if (dy < -1 || dy > 1) {
                System.out.println("Invalid y");
                dy = 0;
            }
            board[pX][pY] = Spaces.Empty;

            pX += dx;
            pY += dy;

            if (board[pX][pY] == Spaces.Goal) {
                System.out.println("You win!");
                gameOver = true;
            }
            board[pX][pY] = Spaces.Player;
        }
    }

    ;

    enum Spaces {Empty, Player, Goal}
}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
NickH
  • 21
  • 1
  • 4
  • 3
    Count your `;`'s. There is at least one to many. – tkausl Oct 27 '17 at 01:19
  • I ran your code through my IDE's auto-formatter, and the problem should be obvious if you look carefully. See [Semicolon at end of if statement](/q/14112515). – bcsb1001 Oct 27 '17 at 01:24
  • 2
    Possible duplicate of [Semicolon at end of 'if' statement](https://stackoverflow.com/questions/14112515/semicolon-at-end-of-if-statement) – dave Oct 27 '17 at 01:25

1 Answers1

3
if(dx == 9);

though the above is a valid statement and the code shall compile with this. But then the next block of statements :-

{
    System.out.println("Game Over");
    break;
}

is executed irrespective of any condition. Hence the loop finds a break always. And if the loop has found a break, the very next statement

System.out.println("Enter either a -1,0, or 1 in the Y");

is unreachable.

Naman
  • 27,789
  • 26
  • 218
  • 353