Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem in navigation in a txt file with specific direction

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem in navigation in a txt file with specific direction

    I have a program that I am writing for class. It takes in the rooms of a txt file, creates an array for a dungeon. then I have to be able to navigate through the dungeon using n,s,e,w. When I entire north it is supposed to r--, south r++, e c++ and w --. The problem i am running into is that when I entire any of the directions, nsew, it only goes south. Here is the program
    I would like to put the file online but dont' know how.

    /*Wendy Nurton
     * wnurton
     * 5/5/2009
     * CS 160
     */
     
    import java.io.File;
    import java.util.Scanner;
    import java.util.StringTokenizer;
     
    public class Dungeon {
     
     
        public String rooms [][] = new String[6][4];
     
        public static void main(String[] args) throws Exception {
            Dungeon dun = new Dungeon();
            String dungeon = args[0]; 
            char direction = 'p';
            int row = 0;
            int col = 0;
            dun.printWelcomeMessage();
            dun.readRoomFile(dungeon);
            dun.printRoom(row, col);
            Scanner keyboard = new Scanner (System.in);
            direction = keyboard.next().charAt(0);
     
            do {
     
            switch (direction)
     
            {
     
            case 'h':
            case 'H':
                dun.printHelp();
                break;
            case 'n':
            case 'N':    
                if (dun.checkIsExitValid(row, col, "north"))
                    row --;
                else 
                    System.out.println("This is not a valid exit.");
                dun.printRoom(row, col);
                break;
            case 's':
            case 'S':
                if (dun.checkIsExitValid(row, col, "south"))
                    row++;
                else
                    System.out.println("This is not a valid exit.");
                dun.printRoom(row, col);
                break;
            case 'w':
            case 'W':
                if (dun.checkIsExitValid(row, col, "west"))
                    col++;
                else
                    System.out.println("This is not a valid exit.");
                break;
            case 'e':
            case 'E':
                if (dun.checkIsExitValid(row, col, "east"))
                    col--;
                else
                    System.out.println("This is not a valid exit.");
                break;
            case 'l':
            case 'L':
                dun.printRoom(row, col);
                break;
     
            }//if (dun.checkIfPit(row, col))
                //System.exit(0);
            }
            while (keyboard.next().charAt(0) != 'q');
     
     
        }
                   // TODO Auto-generated method stub
     
     
        public String[] args;
        public boolean checkIsExitValid(int x, int y, String dun) {
            // TODO Auto-generated method stub
    //        System.out.println(rooms[x][y]+";"+ dun);
            if (rooms[x][y].indexOf(dun)!=-1)
            return true;
            else 
                return false;
        }
        public boolean checkIfPit(int x, int y) {
            // TODO Auto-generated method stub
            return (checkIsExitValid(x, y, "pit"));
        }
        public String getRoomDescription(int row, int col) {
            // TODO Auto-generated method stub
     
            return (rooms[row][col]);
        }
     
        public void printRoom(int row, int col) {
            // TODO Auto-generated method stub
            System.out.println(rooms[row][col]);
        }
        public void printHelp() {
            // TODO Auto-generated method stub
     
            System.out.println("");
            System.out.println("Help: to play this game, you can move north, west, south or east."); 
            System.out.println("Type 'l' to take a look around at the room, or type 'q' to quit the game.");
        }
        public void printWelcomeMessage() {
            // TODO Auto-generated method stub
            System.out.println("Welcome to my dungeon!");
            System.out.println("We have creepy crawly monsters roaming about.");
            System.out.println("Find the escape route and win!");
            System.out.println("Or drop to your death in the deep pit.");
            System.out.println("Good luck!");
            System.out.println(" ");
            System.out.println("Menu options: 'h' for help, 'n' for north, 's' for south, 'w' for west, 'e' for east, 'l' to look around, and 'q' for quit.");
     
     
        }
     
        public void readRoomFile(String dungeon) throws Exception {
            File file = new File (dungeon);
            Scanner in = new Scanner(file);
     
            int i=0;
            while( in.hasNextLine())
            {
                StringTokenizer tokenizer = new StringTokenizer(in.nextLine(), ";\n");
     
                int x = Integer.parseInt(tokenizer.nextToken());
                int y = Integer.parseInt(tokenizer.nextToken());
     
     
                rooms [x][y] = (tokenizer.nextToken());
     
            }
     
     
            // TODO Auto-generated method stub
     
        }
     
    }
    Last edited by JavaPF; May 10th, 2009 at 07:53 AM. Reason: Please use code tags...


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Moving through my array

    Hello wendybird79 and welcome to the Java Programming Forums

    I think you will find that this has already been solved here:

    http://www.javaprogrammingforums.com...e-problem.html

    Should make your life a lot easier!!

    import java.io.FileReader;
    import java.util.Scanner;
    import java.util.regex.Pattern;
     
    public class Dungeon {
        private String[][] dungeon = new String[6][4];
        public void printWelcomeMessage(){
            System.out.println("Welcome to my dungeon!");
            System.out.println("We have creepy crawly monsters roaming about.");
            System.out.println("Find the escape route and win!");
            System.out.println("Or drop to your death in the deep pit.");
            System.out.println("Good luck!");
            System.out.println();
            System.out.println("Menu options: 'h' for help, 'n' for north, 's' for south, 'w' for west, 'e' for east, 'l' to look around, and 'q' for quit.");
        }
        public void printHelp(){
            System.out.println("Help: to play this game, you can move north, west, south or east.");
            System.out.println("Type 'l' to take a look around at the room, or type 'q' to quit the game.");
        }
        public boolean readRoomFile(String file){
            Scanner fs;
            try{
                fs = new Scanner(new FileReader(file));
            }catch(Exception e){ return false; };
            for(String temp;fs.hasNextLine();){
                temp = fs.nextLine();
                dungeon[Integer.parseInt(temp.split(";")[0])][Integer.parseInt(temp.split(";")[1])] = temp.split(";")[2];
            }
            return true;
        }
        public void printRoom(int row, int col){
            System.out.println(dungeon[row][col]);
        }
        public String getRoomDescription(int row, int col){
            return dungeon[row][col];
        }
        public boolean checkIfPit(int row, int col){
            return Pattern.compile("[^a-zA-Z]pit[^a-zA-Z]").matcher(dungeon[row][col].toLowerCase()).find();
        }
        public boolean checkIsExitValid(int row, int col, char d){
            String direction = "";
            switch(d){
                case 's': direction = "south"; break;
                case 'w': direction = "west"; break;
                case 'n': direction = "north"; break;
                case 'e': direction = "east"; break;
                default: break;
            }
            if(direction != "")
                return Pattern.compile("[^a-zA-Z]"+direction+"[^a-zA-Z]").matcher(dungeon[row][col].toLowerCase()).find();
            return false;
        }
        public Dungeon(String filename){
            Scanner sin = new Scanner(System.in);
            String choice;
            Pattern goalp = Pattern.compile("[^a-zA-Z]goal[^a-zA-Z]");
            boolean goal = true;
            int roomx = 0, roomy = 0;
            do{
                if(goal){
                    while(!readRoomFile(filename)){
                        System.out.print("Please enter a valid file name: ");
                        filename = sin.next();
                    }
                    goal = false;
                    filename = "";
                    printWelcomeMessage();
                }
                System.out.print(">");
                choice = sin.next();
                if(choice.equals("h")) printHelp();
                else if(choice.equals("l")) printRoom(roomy, roomx);
                else if(choice.equals("n")||choice.equals("s")||choice.equals("e")||choice.equals("w")){
                    if(checkIsExitValid(roomy, roomx, choice.toLowerCase().charAt(0))){
                        switch(choice.charAt(0)){
                            case 'n': roomy -= 1; break;
                            case 's': roomy += 1; break;
                            case 'e': roomx += 1; break;
                            case 'w': roomx -= 1; break;
                            default: break;
                        }
                        printRoom(roomy, roomx);
                    }else{
                        System.out.print("There is no exit to the ");
                        switch(choice.charAt(0)){
                            case 'n': System.out.println("north."); break;
                            case 's': System.out.println("south."); break;
                            case 'e': System.out.println("east."); break;
                            case 'w': System.out.println("west."); break;
                        }
                    }
                }
                if(goalp.matcher(getRoomDescription(roomy, roomx).toLowerCase()).find() || checkIfPit(roomy, roomx) || choice.equals("q")){
                    goal = true;
                    System.out.println("Good Bye!");
                }
            }while(!choice.equals("q"));
        }
        public static void main(String[] args) {
            if(args.length > 0) new Dungeon(args[0]);
            else new Dungeon("");
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Moving MySql Database from one machine to another machine
    By vaishali in forum JDBC & Databases
    Replies: 5
    Last Post: July 21st, 2010, 01:21 AM