New to Java, Need some Help

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 1881 Views - Last Post: 25 September 2013 - 10:53 PM Rate Topic: -----

#1 Tonno22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 13

New to Java, Need some Help

Posted 23 September 2013 - 09:16 PM

import java.util.Random;
import java.util.Scanner;

public class Program1 {
    
    public static final int GOAL_NUMBER = 100;
    
    
    public static void main(String[] args) {
        int roll, space,
        
        
        Random rand = new Random();
        
        
        Scanner keyboard = new Scanner(System.in);
        
        roll = rand.nextInt(6) + 1;        
             if (roll==1);
                System.out.println("You Potted a Plant");


I'm new to java, and programming in general, my first programming assignment is to make a game to run through a game of chutes and ladders, the end result after running it should look like

Turn #1. Position = 0. Roll = 4:
Moved to space # 4
Turn #2. Position = 4. Roll = 1:
Moved to space # 5
You made cookies! How nice! Climb up to # 14!



The end result looks something like that, the you made cookies is a ladder within the game, and there are around 14 of them, 7 chutes that bring you back down spaces and 7 ladders. With my code so far, I don't know if I am going about it right, but in the end after so many turns you are supposed to end up at space # 100 for the game over! Goal Reached!

I don't understand where I go to from here, if you could help me at all that would be great!

Thanks, Tonno

Is This A Good Question/Topic? 0
  • +

Replies To: New to Java, Need some Help

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11092
  • View blog
  • Posts: 18,980
  • Joined: 19-March 11

Re: New to Java, Need some Help

Posted 23 September 2013 - 09:25 PM

It would be helpful to know what sort of material you've covered so far. There are a couple of ways you might program something of this sort, but which of them might be appropriate depends on what you're familiar with.

If you've covered arrays, I can suggest that you try to model this with two arrays.

I don't think you're going to enjoy writing this if you try to do it with a massive if statement.
Was This Post Helpful? 0
  • +
  • -

#3 Tonno22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 23 September 2013 - 10:36 PM

We have covered do while loops and if else statements, I don't really know if I understand them all that well though.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11092
  • View blog
  • Posts: 18,980
  • Joined: 19-March 11

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:00 PM

Oh, okay. For some reason I thought you wanted to have a separate message for each of the hundred squares but I see that's not the case. Okay, this isn't so bad.

So here's a few tips:

You want to do this with a loop, so you might have to review that. With while loops you need to think about how you know you're done - the same goes for do...while loops. Each time through the loop is one turn for that player. You want to keep track of where the player is, and update that state based on their die roll. If the player's location happens to be one of the chute or ladder squares, you want to update it to the square that chute or ladder takes them to. So for example if the player lands on square 5, you would print that message and update their location to square 14. If they don't get a special square, just tell them where they've moved to.

The way to write this is to work out the logic first, then think about the code. You might want to make a little mock-up of the board and move through a few turns of the game to get an idea of what you're going to have to be keeping track of.
Was This Post Helpful? 0
  • +
  • -

#5 Tonno22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:04 PM

I do have a flow chart that we were required to make to help us with the coding.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11092
  • View blog
  • Posts: 18,980
  • Joined: 19-March 11

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:10 PM

Oh, brilliant. Then use that.
Was This Post Helpful? 0
  • +
  • -

#7 djgomez23   User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 45
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:24 PM

This is not a complete code. I'll leave it up to you to edit your code and try to understand what I did here.

code removed




Since we do not want to end the program unless the user reaches(passes) the goal number or 100th position, we can put the code inside a while loop wherein the condition is that position<GOAL_NUMBER.

Next, to execute the rolling, the user needs to type the word "roll". The variable 'input' gets the user's input. Next code:

if (input.trim().equalsIgnoreCase("roll"))


"equalsIgnoreCase" means that the user can type "ROLL", "roLL", "roLl" to execute the rolling. "trim()" means that white spaces after the input will be removed. For example, if the user types: "roll ", it will be trimmed to "roll" only.

Next Code:
int roll = (int) (1 + (Math.random() * (7- 1)));

This is a code where roll has a range from 1-6, like a dice. For further understanding go to this site: http://stackoverflow...range-with-java

Next Code:
				
code removed



"position = position + roll" means that it will get the value of the variable position, add it to the value of variable roll, and store it to the variable position. In simpler words, your current position will be added to rolled value.

We now add a go back spaces wherein if the user reaches the position 4, he will go back 2 spaces. If you want to add more positions use the operator "or". For example:
if (position == 4 || position == 10). This means, if the user reaches position 4 OR 10, he will go back 2 spaces. Else, we will move to the new position and we increment the turn number.

This post has been edited by jon.kiparsky: 23 September 2013 - 11:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 Tonno22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:25 PM

So I should erase my if statement there, make a do while. What will my int's be ?
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11092
  • View blog
  • Posts: 18,980
  • Joined: 19-March 11

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:27 PM

Sorry, but let's leave the OP to do their own homework, okay?
Explanations are good. Code dumps, less so.
Was This Post Helpful? 0
  • +
  • -

#10 djgomez23   User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 45
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:34 PM

View Postjon.kiparsky, on 23 September 2013 - 11:27 PM, said:

Sorry, but let's leave the OP to do their own homework, okay?
Explanations are good. Code dumps, less so.


sorry for that :))

For OP: Try to wrap your code with while loop. Just an advice, do not write the whole code especially if you're a beginner at any programming languages. Try to add one line at a time and debug your program, see if it works as wanted. :)
Was This Post Helpful? 1
  • +
  • -

#11 Tonno22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 23 September 2013 - 11:43 PM

Alright thank you
Was This Post Helpful? 0
  • +
  • -

#12 Tonno22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 24 September 2013 - 12:51 AM

package program1;

import java.util.Scanner;

public class Program1 {
    
    public static final int GOAL_NUMBER = 100;
   
    
    public static void main(String[] args) {
        int position = 0;
        int turn = 1;
        
        
        
       
        
        Scanner keyboard = new Scanner(System.in);
               
            while (position<GOAL_NUMBER) {
                int roll = (int) (1 + (Math.random() * (7-1)));
                System.out.println("\nTurn #" + turn + ".  Position = " + position + ". Roll = " + roll + ":");
                position = position + roll;
                if (position==1 || position == 38 ){
                    System.out.println("You Potted a Plant! How Nice! Climb to #38!");
                    position = position + 37;
                }
                
                 
                if (position==5 || position == 14) {
                    System.out.println("You Made cookies!  How Nice!  Climb to #14!");
                    position = position + 9;
                }                
                if (position==9 || position == 31) {
                    System.out.println("You Mowed the yard!  How Nice!  Climb to #31!");
                    position = position + 22;
                }
                if (position==15 || position == 6) {
                    System.out.println("You Skipped homework!  Busted!  Slide to #6!");
                    position = position - 9;
                }
                if (position==21 || position == 42) {
                    System.out.println("You Helped a puppy!  How Nice!  Climb to #42!");
                    position = position + 21;
                }    
                if (position==28 || position == 84) {
                    System.out.println("You Rescued a cat!  How Nice!  Climb to #84!");
                    position = position + 56;
                }
                if (position==36 || position == 44) {
                    System.out.println("You Cleaned your room!  How Nice!  Climb to #44!");
                    position = position + 8;
                }
                if (position==47 || position == 26) {
                    System.out.println("You Skated on thin ice!  Busted!  Slide to #26!");
                    position = position - 21;
                }
                if (position==49 || position == 11) {
                    System.out.println("You ate too much!  Busted!  Slide to #11!");
                    position = position - 38;
                }
                if (position==51 || position == 68) {
                    System.out.println("You swept the floor!  How Nice!  Climb to #68!");
                    position = position + 17;
                }
                if (position==56 || position == 53) {
                    System.out.println("You played in the mud!  Busted!  Slide to #53!");
                    position = position - 3;
                }
                if (position==62 || position == 19) {
                    System.out.println("You dropped dishes!  Busted!  Slide to #19!");
                    position = position - 43;
                }
                if (position==64 || position == 60) {
                    System.out.println("You rode bike no handed!  Busted!  Slide to #60!");
                    position = position - 4;
                }
                if (position==71 || position == 91) {
                    System.out.println("You Returned lost money!  How Nice!  Climb to #91!");
                    position = position + 20;
                }
                if (position==80 || position == 100) {
                    System.out.println("You helped an injured friend!  How Nice!  Climb to #100!");
                    position = position +20;
                }
                if (position==87 || position == 24) {
                    System.out.println("You stole from your freind!  Busted!  Slide to #24!");
                    position = position - 63;
                }
                if (position==93 || position == 73) {
                    System.out.println("You threw dirt!  Busted!  Slide to #73!");
                    position = position - 20;
                }
                if (position==95 || position == 75) {
                    System.out.println("You broke a window!  Busted!  Slide to #75!");
                    position = position - 20;
                }
                if (position==98 || position == 78) {
                    System.out.println("You pulled cats tail!  Busted!  Slide to #78!");
                    position = position - 20;
                
                }
                
                else {
                    System.out.println("Moved to space #" + position);
                
                
                
                System.out.println();
                turn++;
                
                
                
                
            
                while(roll==1);
                
                    
                
            }
            
         }

      
    }


So this is what I have so far, but when I try running it it runs completely wrong, on the first run I got this.
Turn #1. Position = 0. Roll = 3:
Moved to space #3


Turn #2. Position = 3. Roll = 5:
Moved to space #8


Turn #3. Position = 8. Roll = 3:
You ate too much! Busted! Slide to #11!
Moved to space #-27


Turn #4. Position = -27. Roll = 6:
Moved to space #-21


Turn #5. Position = -21. Roll = 2:
Moved to space #-19


Turn #6. Position = -19. Roll = 3:
Moved to space #-16


Turn #7. Position = -16. Roll = 1:
Moved to space #-15

It's like it took my position from you ate too much and subtracted what it subtracts from the 11, I don't understand.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11092
  • View blog
  • Posts: 18,980
  • Joined: 19-March 11

Re: New to Java, Need some Help

Posted 24 September 2013 - 12:56 AM

	                if (position==49 || position == 11) {
	                    System.out.println("You ate too much!  Busted!  Slide to #11!");
	                    position = position - 38;


It's doing what you told it to do.
Was This Post Helpful? 0
  • +
  • -

#14 Tonno22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 13

Re: New to Java, Need some Help

Posted 24 September 2013 - 12:58 AM

So I don't want to make them equal to each other because if it lands on 11, its like it is the same as on position 49 so it will subtract 38 from 11?
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11092
  • View blog
  • Posts: 18,980
  • Joined: 19-March 11

Re: New to Java, Need some Help

Posted 24 September 2013 - 01:00 AM

Explain to me what's happening in that line. Focus on the condition you're using for the if statement.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3