Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:35:39+00:00 2026-06-12T09:35:39+00:00

I’m coding a simple maze game in java. The program reads in a text

  • 0

I’m coding a simple maze game in java. The program reads in a text “map” from an input file for the layout of the maze. The rules are simple: navigate the maze (represented by a 2D array) through user input and avoid the cave-ins (represented by Xs), and get to the ‘P’ (player) the the spot marked ‘T’. Right now, I’ve got most of the code written, it’s just a matter of getting it to work properly. I’ve set up most of the game to run with a while loop, with the boolean “got treasure” set to false. Once this rings true, it should end the game.

However, I haven’t coded the circumstance in which the player actually gets the treasure though, so I’m wondering why my code simply spits out “Congratulations! You’ve found the treasure!” and nothing else. If anyone could shed some light on this, I’d be very grateful. My code is somewhat of a mess of loops, as our teacher has just gotten to methods, constructors, and creating our own classes. Here is the code I have so far:

import java.util.*;
import java.io.File;
public class MazeGame {

public static void main(String[] args) throws Exception {
    Scanner scan = new Scanner(new File("maze.txt"));
    Scanner user = new Scanner(System.in);
    int rows = scan.nextInt();
    int columns = scan.nextInt();
    int px = 0;
    int py = 0;
    String [][] maze = new String[rows][columns];
    String junk = scan.nextLine();

    for (int i = 0; i < rows; i++){
        String temp = scan.nextLine();
        String[] arrayPasser = temp.split("");
        for (int j = 0; j < columns; j++){
            maze[i][j] = arrayPasser[i];
        }
    }

    boolean gotTreasure = false;

    while (gotTreasure = false){
        for (int i = 0; i < rows; i++){
            for (int j = 0; j < columns; j++){
                System.out.print(maze[i][j]);
                System.out.print(" ");
        }
            System.out.print("\n");
      }


        System.out.printf("\n");
        System.out.println("You may:");
        System.out.println("1) Move up");
        System.out.println("2) Move down");
        System.out.println("3) Move left");
        System.out.println("4) Move right");
        System.out.println("0) Quit");
        int choice = user.nextInt();
        int i = 0;

        if (choice == 1 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("X") == false){
                        maze[px][py] = ".";
                        maze[k][l-1] = "P";
                        maze[px][py] = maze[k][l-1];
                    }else if (maze[px][py-1] == "X"){
                        System.out.println("Cannot move into a cave-in! Try something else.");
                    }else {
                    continue;}


                    }
                }
            }
        else if (choice == 2 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("X") == false){
                        maze[px][py] = ".";
                        maze[k][l+1] = "P";
                        maze[px][py] = maze[k][l+1];
                    }else if (maze[px][py+1] == "X"){
                        System.out.println("Cannot move into a cave-in! Try something else.");
                    }else {
                    continue;}

               }
             }
            }
        else if (choice == 3 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("X") == false){
                        maze[px][py] = ".";
                        maze[k-1][l] = "P";
                        maze[px][py] = maze[k-1][l];
                    }else if (maze[px-1][py] == "X"){
                        System.out.println("Cannot move into a cave-in! Try something else.");
                    }else {
                    continue;}
                }
            }
        }
        else if (choice == 4 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("X") == false){
                        maze[px][py] = ".";
                        maze[k+1][l] = "P";
                        maze[px][py] = maze[k+1][l];
                    }else if (maze[px+1][py] == "X"){
                        System.out.println("Cannot move into a cave-in! Try something else.");
                    }else {
                    continue;}
                }
            }
        }
        else if (choice == 0){
            System.exit(0);
        }
    }

    System.out.println("Congratulations, you found the treasure!");

    scan.close();
    user.close();
        }

    }

And here is the sample input file:

  • 5 5
  • P.XX.
  • .X…
  • …X.
  • XXT..
  • ..X..
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:35:40+00:00Added an answer on June 12, 2026 at 9:35 am

    (sigh) one equals sign instead of two. You have “while (gotTreasure = false)”, which assigns the value false to gotTreasure and does not enter the loop. Change it to “while (gotTreasure == false) and it enters the loop.

    For future questions: please attempt to figure out on your own what is happening, and let others know what you have tried and what specific questions you have about it. It is arguable I should just have let this go, since it is essentially a request to debug your code for you. Learn to debug yourself. If trace statements aren’t getting executed, it’s most likely the code at that point isn’t getting executed. If a loop isn’t getting entered, it is almost certainly because the conditions for entering the loop don’t exist.

    Learn to use a debugger – eclipse (and, I am sure, lots of other development tools) has an excellent one. Find out what a breakpoint is, how to set it and examine variables when it is hit, and figure out from there what has gone wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.