Search code examples
javamaze

Rudimentary Maze Game


Today in my class, we discussed a rudimentary maze game that could be built in Java. Honestly, I have no idea how to go about creating something like this and the Professor was not much hep either. Nevertheless, I want to know how to make something like this, without sing arrays. As a very much noob at coding, I have a very vague idea onto where I should start. Please help me! As I would look to see the code behind this which generated the map. Note: with each command the code looped back and much (R) to a new section on the grid after a choice was stated by the user. Thanks!

Help Robot (R) get to Exit (E)

R o o o o

o o o o o

o o o o o

o o o o o

o o o o E

  1. up
  2. down
  3. left
  4. right

Move? 4


Solution

  • Since you cannot use array and cannot create any new method. You can do something like this:

    Output:

     R  .  .  .  . 
     .  .  .  .  . 
     .  .  .  .  . 
     .  .  .  .  . 
     .  .  .  .  E 
    

    Codes:

        int playerX=0, playerY=0;  //holds player's location
        int exitX = 4, exitY = 4;  //holds exit's location
    
        //Print map
        for(int x=0; x<5; x++){
            for(int y=0; y<5; y++)
                if(x == playerX && y == playerY)
                    System.out.print(" R "); //print player location
                else if(x == exitX && y == exitY)
                    System.out.print(" E ");
                else    
                    System.out.print(" . ");
            System.out.println("");     
        }
        /*
        //Update player's position on movement
        if (movement == DOWN)
            playerY = Math.min(playerY+1 , 5);
        else if (movement == UP)
            playerY = Math.max(playerY-1 , 0);
        else if (movement == LEFT)
            playerX = Math.max(playerX-1 , 0);
        else else if (movement == RIGHT)
            playerX = Math.max(playerY+1 , 5);  
        */
    

    You can enclose every thing in a while loop, and repeat the loop so long player's location is not same as exit's location.