basic snakes and ladders game don't know why position goes over 10

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

35 Replies - 4110 Views - Last Post: 10 January 2013 - 02:18 PM Rate Topic: -----

#1 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 05:52 PM

import java.util.Random;
import java.util.Scanner;
public class SL{
  static int p1dice1;
  static int p1dice2;
  static int p2dice1;
  static int p2dice2;
  static int position1;
  static int position2;
  static int ladders [] = {8,15,42,66};
  static int laddersEnd[] = {31,97,81,87};//where the ladders take u
  static int snakes []    = {24,55,71,88,99};
  static int snakesEnd [] = {1,13,29,67,6};//where the snakes take u
  static String name1;
  static String name2;
  public static void main (String args[])
  {
    askForName();
    loop();
  }
  public static void askForName ()
  {
    System.out.println(name1 + name2);//delete this later
    System.out.println("***********************WELCOME TO SNAKES AND LADDERS**************************");
    Scanner input = new Scanner (System.in);
    System.out.println("Enter player 1's name");
    name1 = input.nextLine();
    System.out.println("Enter player 2's name");
    name2 = input.nextLine();
    System.out.println("Good Luck " + name1 + " and " + name2);
  }
  public static void Random ()
  {
    Random rand = new Random();
    p1dice1 = 1 + rand.nextInt(6);
    p1dice2 = 1 + rand.nextInt(6);
    p2dice1 = 1 + rand.nextInt(6);
    p2dice2 = 1 + rand.nextInt(6);
  }
  public static int position1()
  {
    Scanner input = new Scanner (System.in);
    String roll = "";
    System.out.println(roll);//delete this
    System.out.println("player 1 roll, press anything");
    roll = input.next();
    System.out.println("player1 has rolled " + p1dice1 + " and " + p1dice2);
    position1 = position1 + p1dice1 + p1dice2;
    System.out.println("player 1 is now at " + position1);
    return position1;
  }
  public static int position2 ()
  {
    Scanner input = new Scanner (System.in);
    String roll = "";
    System.out.println(roll);//delete this
    System.out.println("player 2 rolls, press anything to begin");
    roll = input.next();
    System.out.println("player2 has rolled " + p2dice1 + " and " + p2dice2);
    position2 = position2 + p2dice1 + p2dice2;
    System.out.println("player 2 is now at " + position2);
    return position2;
  }
  public static void loop ()
  {
    while (position1 < 100 || position2 < 100)
    {
      Random();
      position1();
      player1Ladders();
      player1Snakes();
      position2();
      player2Ladders();
      player2Snakes();
      winner();
    }
  }
  public static void winner ()
  {
    if (position1 >= 100){
      System.out.println("THE WINNER IS " + name1);
    }
    else if (position2 >= 100){
      System.out.println("THE WINNER IS " + name2);
    }
  }
  public static void player1Ladders()
  {
    if (position1 == ladders[0]){
      System.out.println("You hit a ladder! your new position is " + laddersEnd[0]);
      position1 = laddersEnd[0];
    }      
    else if (position1 == ladders[1]){
      System.out.println("You hit a ladder! your new position is " + laddersEnd[1]);
      position1 = laddersEnd[1];
    }
    else if (position1 == ladders[2]){
      System.out.println("You hit a ladder! your new position is " + laddersEnd[2]);
      position1 = laddersEnd[2];
    }
    else if (position1 == ladders[3]){
      
      System.out.println("You hit a ladder! your new position is " + laddersEnd[3]);
      position1 = laddersEnd[3];
    }
  }
  public static void player1Snakes()
  {
    if (position1 == snakes[0]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[0]);
      position1 = snakesEnd[0];
    }
    else if (position1 == snakes[1]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[1]);
      position1 = snakesEnd[1];
    }
    else if (position1 == snakes[2]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[2]);
      position1 = snakesEnd[2];
    }
    else if (position1 == snakes[3]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[3]);
      position1 = snakesEnd[3];
    }
    else if (position1 == snakes[4]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[4]);
      position1 = snakesEnd[4];
    }
  }
  public static void player2Ladders()
  {
    if (position2 == ladders[0]){
      System.out.println("You hit a ladder! your new position is " + laddersEnd[0]);
      position2 = laddersEnd[0];
    }
    else if (position2 == ladders[1]){
      System.out.println("You hit a ladder! your new position is " + laddersEnd[1]);
      position2 = laddersEnd[1];
    }
    else if (position2 == ladders[2]){
      System.out.println("You hit a ladder! your new position is " + laddersEnd[2]);
      position2 = laddersEnd[2];
    }
    else if (position2 == ladders[3]){
      System.out.println("You hit a ladder! your new position is " + laddersEnd[3]);
      position2 = laddersEnd[3];
    }
  }
  public static void player2Snakes()
  {
    if (position2 == snakes[0]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[0]);
      position2 = laddersEnd[0];
    }
    else if (position2 == snakes[1]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[1]);
      position2 = snakesEnd[1];
    }
    else if (position2 == snakes[2]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[2]);
      position2 = snakesEnd[2];
    }
    else if (position2 == snakes[3]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[3]);
      position2 = snakesEnd[3];
    }
    else if (position2 == snakes[4]){
      System.out.println("You hit a snake! your new position is " + snakesEnd[4]);
      position2 = snakesEnd[4];
    }
  }
}


i meant to say position over 100, i think somthing is wrong with the while loop or if statment about winner (i just started using java)

This post has been edited by macosxnerd101: 08 January 2013 - 05:53 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: basic snakes and ladders game don't know why position goes over 10

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 05:54 PM

Are you encountering a specific error? If so, please post the exact error message.
Was This Post Helpful? 0
  • +
  • -

#3 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 05:57 PM

im not sure how to make the game stop when the position is 100. im not sure why it goes over 100
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 06:04 PM

Your loop here. If either is less than 100 or both are less than 100, the loop will continue. Changing the condition from OR to AND fixed this. Also, if I'm at 95 and roll a 9, 95 + 9 == 104. You could take Math.min(roll, 100) and assign it to the user's position.
while (position1 < 100 || position2 < 100) {


Was This Post Helpful? 0
  • +
  • -

#5 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 06:11 PM

thanks, do you know how i can incorporate string methods (basic ones such as .length or .substring). and how can i use parameters in my methods
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 06:19 PM

I would do a fair amount of refactoring on your code. First, you only need one Scanner object. You should not be creating a Scanner object in each method. What happens is at the end of the method, the Scanner is garbage collected. Then the new method is called and a new Scanner is created. It's not a good use of resources. Just use a static Scanner for now.

Also, having position1() and position2() methods, one for each player, is not good practice. You could design a method takeTurn(boolean player) and have it update the appropriate variable based on the player. Something like:
public static void takeTurn(boolean player){
     int dieOne = rand.nextInt(6) + 1;
     int dieTwo = rand.nextInt(6) + 1;
   
     if(player){
          playerOnePosition += dieOne + dieTwo;
     }
    
     else{
          playerTwoPosition += dieOne + dieTwo;
     }

}



Or a getRoll() method would be good practice:
public static int getRoll(){
     return rand.nextInt(12) + 1; 
}



Quote

thanks, do you know how i can incorporate string methods (basic ones such as .length or .substring).

An example with length():
String s = "test";
int length = test.length();



Though I don't really see why you need Strings here.
Was This Post Helpful? 0
  • +
  • -

#7 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 06:48 PM

for the get roll method how can i return for players' dice
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 06:51 PM

If it's important to print the value of each die, then getRoll() probably wouldn't be the best idea then. Or you could have it return the value for a single die, and call it twice.
Was This Post Helpful? 0
  • +
  • -

#9 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 06:57 PM

thanks how should i use normal variables in methods instead of all static because im trying to avoid too many static variables. which ones should i keep static??
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: basic snakes and ladders game don't know why position goes over 10

Posted 08 January 2013 - 07:42 PM

Really what you need to look more into is OOP. Static variables are one thing for beginners working with the main() method. The level to which you have designed your methods is very brittle and needs to be improved. You could have a Die class with a roll() method which returns a value, and store a couple Die objects for your game.

I've got a more basic design that I think is a little easier to follow and a lot cleaner. Now you can manage a single Game object and separate out user input.
class Dice{

    private Random rand;

    public Dice(){
        rand = new Random();
    }

    public int roll(){ return rand.nextInt(6) + 1; }

}

class Player{

    private int position;

    public Player(){
        this.position = 0;
    }

    public boolean move(int amount){
        if(position + amount >= 100){
             position = 100;
             return true; //the player has reached the end
        }

        position += amount;
        return false; //the player has just moved
    }
}

public Game{

    private Player[] players;
    private Dice[] die;
    private int playerTurn; //index of the Player to move
 
    //initialize the Game with 2 players and 2 die
    public Game(){ this(2, 2); }


    public Game(int numPlayers, int numDie){
        this.players = new Player[numPlayers];
        this.die = new Dice[numDie];
        this.playerTurn = 0;
    }

    public boolean takeTurn(){
         //roll the die
         //move the player
         //increment playerTurn by 1, reset to 0 if it == players.length
         //return true if a player has reached the end
         //return false otherwise
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

Re: basic snakes and ladders game don't know why position goes over 10

Posted 09 January 2013 - 12:42 PM

public static boolean move1()
  {
    int amount = p1dice1;
    if(position1 + amount >= 100){
      position1 = 100;
    }
    return true; //the player has reached the end
  }
  public static boolean move2()
  {
    int amount = p2dice1;
    if(position2 + amount >= 100){
      position2 = 100;
    }
    return true; 
  }
}//i did this and im not sure if its right or how to call this method into my loop

public static boolean move1()
{
int amount = p1dice1;
if(position1 + amount >= 100){
position1 = 100;
}
return true; //the player has reached the end
}
public static boolean move2()
{
int amount = p2dice1;
if(position2 + amount >= 100){
position2 = 100;
}
return true; 
}
}


i did this and im not sure if its right or how to call this method into my loop

This post has been edited by macosxnerd101: 09 January 2013 - 01:29 PM
Reason for edit:: Removed quotes and added code tags

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: basic snakes and ladders game don't know why position goes over 10

Posted 09 January 2013 - 01:30 PM

Please use code tags: :code:.

As I stated in my last post, you should not be duplicating methods per player. A big part of programming is the axiom "write once, use many." Start working on learning OOP, as it will help make things cleaner and help you adhere to this axiom.
Was This Post Helpful? 0
  • +
  • -

#13 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

Re: basic snakes and ladders game don't know why position goes over 10

Posted 09 January 2013 - 01:35 PM

if i put both of them in 1 method i cant have 2 returns
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: basic snakes and ladders game don't know why position goes over 10

Posted 09 January 2013 - 01:37 PM

What do you mean two returns? You mean return two values? No- you cannot return two values from a single method. I highly encourage you to go through the Tutoracles on OOP. They include a couple sections on methods too.
Was This Post Helpful? 0
  • +
  • -

#15 g0dly pk3r07   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 08-January 13

Re: basic snakes and ladders game don't know why position goes over 10

Posted 09 January 2013 - 01:54 PM

ok should i keep all my methods void?
i want to use integer methods with parameters but im not really sure how to call them into main or my while loop
which part of oop should i read
Was This Post Helpful? 0
  • +
  • -

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