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 5 of 5

Thread: Chutes and Ladders Java Program

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Chutes and Ladders Java Program

    Hey guys I have a program that runs the basics for chutes and ladders, the only problem is I need to keep track of spaces moved via die roll, chutes, and ladders. I am not too sure how to implement this is my already existing code all help is appreciated. Thanks.


    Here is my code


    package program1;

    import java.util.Random;

    public class Program1 {

    public static final double GOAL_NUMBER = 100;

    public static void main(String[] args) {
    int forwardMoves, backwardMoves, location, roll, turn, skipped;
    Random rand = new Random();
    location = 0;
    turn = 1;
    skipped = 0;

    do {
    roll = rand.nextInt(6) + 1;
    System.out.println("Turn #" + turn + " Position: " + location + " Roll: " + roll + ":");
    location += roll;
    if (location > GOAL_NUMBER) {
    location = location - roll;
    System.out.println(" Went past the end! Turn skipped");
    turn = turn + 1;
    skipped = skipped + 1;

    }
    if (location <= GOAL_NUMBER) {
    switch (location) {

    case 1:
    location = 38;
    System.out.println("You potted a plant! Climb to 38!");
    break;
    case 5:
    location = 14;
    System.out.println("You made cookies! Climb to 14!");
    break;
    case 9:
    location = 31;
    System.out.println("You mowed the yard! Climb to 31!");
    break;
    case 15:
    location = 6;
    System.out.println("You skipped your homework. Slide down to 6!");
    break;
    case 21:
    location = 42;
    System.out.println("You helped a puppy! Climb to 42!");
    break;
    case 28:
    location = 84;
    System.out.println("You rescued a cat! Climb to 84!");
    break;
    case 36:
    location = 44;
    System.out.println("You cleaned your room! Climb to 44!");
    break;
    case 47:
    location = 26;
    System.out.println("You skated on thin ice, slide down to 26!");
    break;
    case 49:
    location = 11;
    System.out.println("You ate too much candy, slide down to 11!!");
    break;
    case 51:
    location = 68;
    System.out.println("You swept the floor! Climb to 68!");
    break;
    case 56:
    location = 53;
    System.out.println("You played in mud, slide down to 83!");
    break;
    case 62:
    location = 19;
    System.out.println("You dropped the dishes, slide down to 19!;");
    break;
    case 64:
    location = 60;
    System.out.println("You rode your bike no handed, slide down to 60!");
    break;
    case 71:
    location = 91;
    System.out.println("You returned lost money! Climb to 91!");
    break;
    case 80:
    location = 100;
    System.out.println("You helped and Injured friend! Climb to 100!");
    break;
    case 87:
    location = 24;
    System.out.println("You stole from the cookie jar, slide down to 24!");
    break;
    case 93:
    location = 73;
    System.out.println("You threw dirt. slide down to 73!");
    break;
    case 95:
    location = 75;
    System.out.println("You broke the window, slide down to 75!");
    break;
    case 98:
    location = 78;
    System.out.println("You pulled a cats tail, slide down to 78!");
    break;

    }
    System.out.println(" Moved to space #" + location);
    turn = turn + 1;
    }
    } while (location < GOAL_NUMBER);
    System.out.println("Game Over! Goal Reached!");
    System.out.println("Game Stats:");
    System.out.println("Number of turns: " + turn);
    System.out.println("Number of turns skipped by passing goal: " + skipped);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Chutes and Ladders Java Program

    Please post your code in formatting/code tags.

    I'm not sure what you're asking. Can you provide further info? What do you mean "keep track of spaces?"

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chutes and Ladders Java Program

    package program1;

    import java.util.Random;

    public class Program1 {

    public static final double GOAL_NUMBER = 100;

    public static void main(String[] args) {
    int forwardMoves, backwardMoves, location, roll, turn, skipped, diceMove;
    Random rand = new Random();
    location = 0;
    turn = 1;
    skipped = 0;
    forwardMoves = 0;
    backwardMoves = 0;

    do {
    roll = rand.nextInt(6) + 1;
    System.out.println("Turn #" + turn + " Position: " + location + " Roll: " + roll + ":");
    location += roll;
    if (location > GOAL_NUMBER) {
    location = location - roll;
    System.out.println(" Went past the end! Turn skipped");
    turn = turn + 1;
    skipped = skipped + 1;

    }
    if (location <= GOAL_NUMBER) {
    switch (location) {

    case 1:
    System.out.println("You potted a plant! Climb to 38!");
    location = 38;
    forwardMoves = forwardMoves + 37;
    break;
    case 5:
    System.out.println("You made cookies! Climb to 14");
    location = 14;
    forwardMoves = forwardMoves + 9;
    break;
    case 9:
    System.out.println("You mowed the yard! Climb to 31!");
    location = 31;
    forwardMoves = forwardMoves + 22;
    break;
    case 15:
    System.out.println("You skipped your homework, Slide down to 6!");
    location = 6;
    backwardMoves = backwardMoves + 9;
    break;
    case 21:
    System.out.println("You helped a puppy! Climb to 42!");
    location = 42;
    forwardMoves = forwardMoves + 21;
    break;
    case 28:
    System.out.println("You rescued a cat! Climb to 84!");
    location = 84;
    forwardMoves = forwardMoves + 56;
    break;
    case 36:
    System.out.println("You cleaned your room! Climb to 44!");
    location = 44;
    forwardMoves = forwardMoves + 8;
    break;
    case 47:
    System.out.println("You skated on thin ice, slide down to 26!");
    location = 26;
    backwardMoves = backwardMoves + 21;
    break;
    case 49:
    System.out.println("You ate too much candy, slide down to 11!");
    location = 11;
    backwardMoves = backwardMoves + 38;
    break;
    case 51:
    System.out.println("You swept the floor! Climb to 68!");
    location = 68;
    forwardMoves = forwardMoves + 17;
    break;
    case 56:
    System.out.println("You played in mud, slide down to 83!");
    location = 53;
    backwardMoves = backwardMoves + 3;
    break;
    case 62:
    System.out.println("You dropped the dishes, slide down to 19!");
    location = 19;
    backwardMoves = backwardMoves + 43;
    break;
    case 64:
    System.out.println("You rode your bike no handed, slide down to 60!");
    location = 60;
    backwardMoves = backwardMoves + 4;
    break;
    case 71:
    System.out.println("You returned lost money! Climb to 91!");
    location = 91;
    forwardMoves = forwardMoves + 20;
    break;
    case 80:
    System.out.println("You helped an injured friend! Climb to 100!");
    location = 100;
    forwardMoves = forwardMoves + 20;
    break;
    case 87:
    System.out.println("You stole from the cookie jar, slide down to 24!");
    location = 24;
    backwardMoves = backwardMoves + 63;
    break;
    case 93:
    System.out.println("You threw dirt, slide down to 73!");
    location = 73;
    backwardMoves = backwardMoves + 20;
    break;
    case 95:
    System.out.println("You broke the window, slide down to 75!");
    location = 75;
    backwardMoves = backwardMoves + 20;
    break;
    case 98:
    System.out.println("You pulled a cats tail, slide down to 78!");
    location = 78;
    backwardMoves = backwardMoves + 20;
    break;

    }
    System.out.println(" Moved to space #" + location);
    turn = turn + 1;
    }
    } while (location < GOAL_NUMBER);
    System.out.println("Game Over! Goal Reached!");
    System.out.println(" ");
    System.out.println("Game Stats:");
    System.out.println("Number of turns: " + turn);
    System.out.println("Spaces moved backwards by chutes: " + backwardMoves);
    System.out.println("Spaces moved forwards by ladders: " + forwardMoves);
    System.out.println("Number of turns skipped by passing goal: " + skipped);
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Chutes and Ladders Java Program

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Chutes and Ladders Java Program

    Hello JHoff93 I liked what your program was doing, so I have modified it a bit and opened a new thread called 'invoking method from another class' in this section which you might like to follow

Similar Threads

  1. Replies: 0
    Last Post: March 7th, 2013, 01:50 PM
  2. snakes and ladders game's code,i need help ,m a beginner!!!
    By arxi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 01:50 PM
  3. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. Snakes and Ladders
    By JamesAubrey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 27th, 2011, 01:07 PM