Home > OS >  How do I mark every position the player has been to in a maze game (2D array)?
How do I mark every position the player has been to in a maze game (2D array)?

Time:06-08

I'm making a maze game that uses a 2D array to represent the game board. I currently have the console outputting "P" wherever the player's current position is. However, I would like to change every element in the array that the player has "visited" and display that to show their path. How would I do this? Do I need to make a second, duplicate array so I can make changes to that?

Here's my code:

import java.util.Scanner;

public class MazeGame {

private static char[][] maze = {

{'1','1','1','0','1','1','0','0','0','1','1','1','1'},

{'1','0','1','1','1','0','1','1','1','1','0','0','1'},

{'0','0','0','0','1','0','1','0','1','0','1','0','0'},

{'1','1','1','1','1','1','1','0','1','0','1','1','1'},

{'1','0','0','0','0','0','1','1','1','1','0','0','1'},

{'1','1','1','1','1','1','1','0','1','1','1','1','1'},

{'1','0','1','0','0','0','0','1','0','0','0','0','1'},

{'1','1','1','1','1','1','1','1','1','1','1','1','1'}

};

  private static int playerRow = 0, playerCol = 0;

  private static int moves = 0;

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    printMaze();
    System.out.println("Welcome to the maze! Your goal is to make it from your starting position, the top left corner of the maze, to the end position, the bottom right corner of the maze. 1s represent spaces that you can move to, 0s represent walls that you can not move to, and Ps represent places that you have already been. To begin, enter an input: \n1-- 'U' for up\n2-- 'D' for down\n3-- 'L' for left\n4-- 'R' for right\n5-- 'Q' for quit\nHave fun playing the maze game and best of luck to you :)");

    while (true) {

      System.out.print("\nEnter a move (U/D/L/R/Q): ");

      String move = input.nextLine();

      if (move.charAt(0) == 'Q' || move.charAt(0) == 'q') {

        System.out.println("Thanks for playing and have a wonderful day!");
        System.exit(0);

      }

      if (move.length() == 1) {

        if (move.charAt(0) == 'U' || move.charAt(0) == 'u') {

          if (playerRow > 0 && maze[playerRow - 1][playerCol] != '0') {

            playerRow--;
            moves  ;

          } else {

            System.out.println("Invalid move.");

          }

        } else if (move.charAt(0) == 'D' || move.charAt(0) == 'd') {

          if (playerRow < maze.length - 1 && maze[playerRow   1][playerCol] != '0') {

            playerRow  ;
            moves  ;

          } else {

            System.out.println("Invalid move. Please enter a valid move from the list below: \n1-- 'U' for up\n2-- 'D' for down\n3-- 'L' for left\n4-- 'R' for right\n5-- 'Q' for quit");

          }

        } else if (move.charAt(0) == 'L' || move.charAt(0) == 'l') {

          if (playerCol > 0 && maze[playerRow][playerCol - 1] != '0') {

            playerCol--;
            moves  ;

          } else {

            System.out.println("Invalid move.");

          }

        } else if (move.charAt(0) == 'R' || move.charAt(0) == 'r') {

          if (playerCol < maze[0].length - 1 && maze[playerRow][playerCol   1] != '0') {

            playerCol  ;
            moves  ;

          } else {

            System.out.println("Invalid move.");

          }

        } else {

          System.out.println("Invalid move.");

        }

      } else {

        System.out.println("Invalid move.");

      }

      printMaze();

      if (playerRow == maze.length - 1 && playerCol == maze[0].length - 1) {

        System.out.println("\nCongratulations, you have won the game in "   moves   " moves!");

        break;

      }

    }

  }

  private static void printMaze() {

    for (int i = 0; i < maze.length; i  ) {

      for (int j = 0; j < maze[0].length; j  ) {

        if (i == playerRow && j == playerCol) {

          System.out.print("P ");

        } else {

          System.out.print(maze[i][j]   " ");

        }

      }

      System.out.println();

    }
  }
}

CodePudding user response:

In your case, you can straight away write P on location the player is before moving him to other part. As your conditions are maze[playerRow][playerCol 1] != '0' it does not matter if there is 1 or P, it will be still accessible field.

        maze[playerRow][playerCol] = 'P'
        playerCol  ;
        moves  ;

CodePudding user response:

Yuo can add this line maze[playerRow][playerCol] = '*'; before you do the move up/down/left/right action.

In case the user enters an invalid move, it will not matter since the printMaze() method will overwrite the * with P for the current player coordinates, given by playerRow and playerCol.

if (move.length() == 1) {
        maze[playerRow][playerCol] = '*';
  • Related