7 Replies - 669 Views - Last Post: 27 April 2012 - 08:55 AM Rate Topic: -----

#1 mladnier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-April 12

How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 07:56 AM

Hey Everyone, I am making a Zork-ish Game for my Computer Class and my code has a good amount of errors. I am trying to make it based in my school. We have to have a random Generation factor. I decided to make it so it randomly picks if you are a Teacher or a Student and depending on which you are depends on what room you start in. Everytime I do this is makes my current room = null.
[boolean choice1 = false;
    boolean choice2 = false;
    
    int teacher;
    int student;
    public Random generator = new Random();
    int chance;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        charactergen();
        createRooms();
        
        parser = new Parser();
    }
    /**
     * Create all the rooms and link their exits together.
     */

    public void charactergen(){
        chance = generator.nextInt(2);
        if(chance==1)
        {
            System.out.println("You are a teacher at Covenant Day School.");
            choice1 = true;
        }
        if(chance == 2)
        {
            System.out.println("You are a student at Covenant Day School.");
            choice2 = true;
        }    
    }
    private void printWelcome()
    {
        System.out.println();
        System.out.println("Welcome to DERPY Zork");
        System.out.println("Developed by: :Sfmort");
        System.out.println("Programmed by: :Sfmort");
        System.out.println("Design by: :Sfmort");
        System.out.println("Idea by: :Sfmort");
        System.out.println("");
        System.out.println("Type 'help' if you need help.");
        System.out.println("You are in the " + currentRoom +".");
        
    }
]

This post has been edited by modi123_1: 27 April 2012 - 08:09 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How to Change the Starting Room by using a Random Generator.

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8401
  • View blog
  • Posts: 31,265
  • Joined: 12-June 08

Re: How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 08:13 AM

Wow.. I am not sure what's going up on that code.

Where's the "createrooms" method?

Where is "currentRoom" even declared?!

What are you doing with that random number generator? If you want a boolean of choices then just use nextbool!

Have you read up on what providing an integer in the 'next int' does? A max value of two means you are going to get numbers that are *UP TO* two (but not including). So 0 and 1 are the options. Your if statements only evaluate 1 and 2.. half of those are potentially expected, right?

Some reading for you:
[quote]public int nextInt(int n)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)[quote]
http://docs.oracle.c...extInt%28int%29
Was This Post Helpful? 0
  • +
  • -

#3 mladnier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-April 12

Re: How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 08:29 AM

I do have the create rooms code in my program. I thought it would be implied that I have it in there so I did not put it in the code above.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8401
  • View blog
  • Posts: 31,265
  • Joined: 12-June 08

Re: How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 08:31 AM

Well if you are asking why this happens ("current room = null") then the code that actually affects that would be a smidge helpful! ;)
Was This Post Helpful? 0
  • +
  • -

#5 mladnier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-April 12

Re: How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 08:35 AM

Well here is the code
    public void createRooms()
    {
        Room gym,lab,media,office,coach,westout,eastout,
                flagpole,lobby,front,lobby2,freshmanhall,agleoffice,
                taproom,playground,carpool,rock,hotlunch,
                server,lobby3,stair1,stair1up,stair2,stair2up,bathroom,
                roofaccess,hallway1,hallway2,hallway3,mancave,hiddenroom,
                stair3up, roof, cross,stair3;
      
        // create the rooms
        gym = new Room("Gym of School");
        media = new Room("the Media Center");
        office = new Room("the School Office");
        coach = new Room("Coach Houseton's Office");
        westout = new Room("The West Side of the School");
        eastout = new Room("The East Side of the School");
        flagpole = new Room("The Flag Pole with the Amercian Flag");
        front = new Room("The Front of the School");
        lobby = new Room("The Lobby of School");
        lobby2 = new Room("The Lobby of School, Still");
        lobby3 = new Room("the lobby of School");
        freshmanhall = new Room("The Hall of Freshmen. \n Its decorated for Color Wars");
        agleoffice = new Room("Mrs. Agle's Office. \n Weird she's not in here..");
        taproom = new Room("Mr. Tapps Room");
        playground = new Room("the Middle School Playground");
        carpool = new Room("the Car Pool line. \n There is a Mustang in the Line");
        rock = new Room("the Rock. \n A lot of people spray paint this");
        server = new Room("the server room of School");
        stair1 = new Room("the bottom of the first stairwell of School");
        stair2 = new Room("the bottom of the second stairwell of School");
        bathroom = new Room("the Bathroom");
        hiddenroom = new Room("the Hidden Room");
        hotlunch = new Room("the Hot Lunch Line");
        //second floor
        hallway1 = new Room("the Hallway");
        hallway2 = new Room("the Hallway");
        hallway3 = new Room("the Hallway");
        stair1up = new Room("the top of the first stairwell of School");
        stair2up = new Room("the top of the second stairwell of School");
        lab = new Room("the Chemistry Lab");
        roofaccess = new Room("the Roof Access");
        mancave = new Room("Mr. Randles' so called 'Man Cave");
        stair3 = new Room("The Stairwell to the Roof");
        //third floor rooms
        stair3up = new Room("The Stairwell that leads to the roof");
        roof = new Room("the roof of School");
        cross = new Room("the back of the cross");
        // initialise room exits
        //(north, east, south, west, up, down)
        //first floor rooms
        gym.setExits(coach, eastout, lobby, westout, null, null);//
        coach.setExits(hiddenroom, null, gym, null, null, null);//
        hiddenroom.setExits(null, null, coach, null, null, null);//
        westout.setExits(null, gym, stair2, null, null, null);//
        eastout.setExits(null, null, flagpole, gym, null, null);//
        flagpole.setExits(eastout, playground, carpool, null, null, null);//
        lobby.setExits(gym, bathroom, lobby2, stair1, null, null);//
        lobby2.setExits(lobby, hotlunch, front, lobby3, null, null);//
        lobby3.setExits(media, lobby2, office, freshmanhall, null, null);//
        hotlunch.setExits(null, null, null, lobby2, null, null);//
        media.setExits(null, null, lobby3, agleoffice, null, null);//
        office.setExits(lobby3, null, null, null, null, null);//
        front.setExits(lobby2, carpool, null, rock, null, null);
        freshmanhall.setExits(null, media, null, null, null, null);
        agleoffice.setExits(null, media, null, null, null, null);
        taproom.setExits(freshmanhall, null, null, null, null, null);//
        playground.setExits(null, null, flagpole, null, null, null);
        carpool.setExits(flagpole, null, null, front, null, null);//
        rock.setExits(stair2, front, null, null, null, null);
        server.setExits(agleoffice, null, freshmanhall, null, null, null);
        bathroom.setExits(null, null, null, bathroom, null, null);
        stair1.setExits(null, lobby, null, null, stair1up, null);
        stair2.setExits(westout, freshmanhall, rock, null, stair2up, null);
        //second floor
        stair1up.setExits(null, hallway1, null, null, null, stair1);
        stair2up.setExits(null, null, null, null, null, stair2);
        lab.setExits(null, null, null, mancave, null, null);
        roofaccess.setExits(null, null, hallway1, null, null, null);
        mancave.setExits(null, lab, null, null, null, null);
        hallway1.setExits(roofaccess, null, hallway2, stair1up, null, null);
        hallway2.setExits(hallway1, null, null, hallway3, null, null);
        hallway3.setExits(lab, hallway2, null, stair2up, null, null);
        stair3.setExits(lab, hallway2, null, stair2up, null, null);
        //third floor
        //(north, east, south, west, up, down)
        stair3up.setExits(null,null,roof,null,null,stair3);
        roof.setExits(stair3up,null,cross,null,null,null);
        cross.setExits(roof,null,null,null,null,null);
        if(choice1 == true) {
            
            lab = currentRoom;
        }
        if(choice2 == true) {
            
            gym = currentRoom;
        }
        
    } 


Sorry I did the code thing wrong... again...

This post has been edited by modi123_1: 27 April 2012 - 08:37 AM
Reason for edit:: highlight the lines THEN click the code tags

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8401
  • View blog
  • Posts: 31,265
  • Joined: 12-June 08

Re: How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 08:39 AM

Okay riddle me this - where is the value for "currentRoom" even assigned in that?
Was This Post Helpful? 0
  • +
  • -

#7 mladnier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-April 12

Re: How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 08:48 AM

So then do I need to have it first be currentRoom = ""; or Should make currentRoom one of the Rooms like gym is?

Or what do you think I should do?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8401
  • View blog
  • Posts: 31,265
  • Joined: 12-June 08

Re: How to Change the Starting Room by using a Random Generator.

Posted 27 April 2012 - 08:55 AM

Quote

So then do I need to have it first be currentRoom = "";

Well, yeah. Eventually you'll need to assign something to it (or initialize it) so it contains SOME information, right?

Quote

or Should make currentRoom one of the Rooms like gym is?

What's the function of "current room"? I am not even sure what datatype it is!

Quote

Or what do you think I should do?

You are going about this the wrong way. Did it seems a bit.. tedious.. to have to declare the grip of individual variables? I thought so.. What you need is some collection to hold them all! An array.. a list(of... something like that. Create the object and then you add it to the collection! That would then mean 'currentroom' would just be an integer variable to hold the current index (relative to the collection) where the player is!

Though if you are going to continue with your current implementation you might want to flip those lines that say " lab = currentRoom;" to somethign that would be 'the current room is assigned as the lab object'.... versus what you have now which is 'the lab is assigned as the currentroom object'. The same for the bit below.

Oh and those two if statements- use an if-else on that second one. It's a good programming practice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1