SHARE
TWEET

Untitled

a guest Jun 16th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Main {
  2.  
  3.   public static String [] board = {"1-Home","2","3-Snake","4","5-Ladder","6","7","8","9-Ladder","10","11","12","13","14-Snake","15","16","17-Snake","18","19-Ladder","20-Snake","21","22","23-Ladder","24","25-Ladder","26","27","28","29-Snake","30","31-Ladder","32","33","34-Snake","35-Snake","36-Snake","37","38-Ladder","39","40","41","42-Ladder","43","44-Snake","45","46","47","48","49-Snake","50","51"};
  4.  
  5.   public static void main(String[] args) {
  6.     int currPos1 = 0;
  7.     int currPos2 = 0;
  8.     int home = 0;
  9.     int diceOne,diceTwo;
  10.     int currPos =0;
  11.     String playerOneName, playerTwoName;
  12.     printBoard();
  13.  
  14.  
  15.     System.out.println("Welcome to Snakes and Ladders!"); //Introductions and option for rules
  16.     System.out.println("Do you want the instructions for Snakes and Ladders(YES/NO)");
  17.     String instructions = In.getString();
  18.     while (instructions.toUpperCase().equals("YES")){
  19.     //Instructions for Snakes and Ladders choose(YES/NO)
  20.     System.out.println("This is a text based Snake and Ladders game!");
  21.     System.out.println("There is going to be two players.");
  22.     System.out.println("There is going to be an option for one or two dices.");
  23.     System.out.println("If a player lands on a snake than the player moves back a certain amount determined by the snakes amouunt and opposite for the Ladder.");
  24.     System.out.println("The first player to land at home wins, but if you exceed the amount of spaces to land on home, than you move to home and retrace the steps remaining.");
  25.     System.out.println(" ");
  26.    
  27.     System.out.println("Do you want the instructions again?(YES/NO)");
  28.     instructions = In.getString();  
  29.   }
  30.     //Get player names
  31.    
  32.     System.out.println("Enter your name First Player");
  33.     playerOneName = In.getString();
  34.     System.out.println("");
  35.     System.out.println("Enter your name Second Player");
  36.     playerTwoName = In.getString();
  37.  
  38.  
  39.     //Game Starts
  40.   do{
  41.         System.out.println("How many die's do you want to play with First Player?(One/Two)");
  42.     String dice = In.getString();
  43.     //Get the amount of dices used by each player
  44.  
  45.     if (dice.toUpperCase().equals("ONE")){
  46.       diceOne = diceRoll();
  47.         currPos1 = (int)diceOne+currPos1;
  48.         System.out.println("You rolled a "+diceOne+"and you're at position"+currPos1+".");  
  49.         }
  50.     else{
  51.       diceOne = diceRoll();
  52.       diceTwo = diceRoll();
  53.         currPos1 = (int)diceOne+ diceTwo+ currPos1;
  54.         System.out.println("You rolled a "+diceOne+ "and"+diceTwo+"and you're at position"+currPos1+".");
  55.  
  56.     }
  57.  
  58.     if(board[currPos1-1].contains("Ladder"))
  59.     {
  60.       currPos1 = ladder(currPos1);
  61.     }
  62.     else if (board[currPos1-1].contains("Snake"))
  63.     {
  64.       currPos1 = snake(currPos1);
  65.     }
  66.  
  67.     System.out.println("How many die's do you want to play with Second Player?(One/Two)");
  68.     dice = In.getString();
  69.     if (dice.toUpperCase().equals("ONE")){
  70.       diceOne = diceRoll();
  71.         currPos2 = (int)diceOne+currPos2;
  72.         System.out.println("You rolled a "+diceOne+"and you're at position"+currPos2+".");
  73.     }else{
  74.       diceOne = diceRoll();
  75.       diceTwo = diceRoll();
  76.         currPos2 = (int)diceOne+ diceTwo+ currPos2;
  77.         System.out.println(currPos2);
  78.         System.out.println("You rolled a "+diceOne+ "and"+diceTwo+"and you're at position"+currPos2+".");
  79.     }
  80.      
  81.     if(board[currPos2-1].contains("Ladder")){
  82.       currPos2 = ladder(currPos2);
  83.     }
  84.     else if (board[currPos2-1].contains("Snake"))
  85.     {
  86.       currPos2 = snake(currPos2);
  87.     }
  88.  
  89.   }while(currPos1<51 && currPos2 <51);
  90.    
  91.     if(currPos1 <=52){
  92.       ladder(currPos);
  93.       snake(currPos);
  94.     }
  95.     else{
  96.       System.out.println("You have passed the boundaries, move back the amount of steps left in your roll.");
  97.     }
  98.     if(currPos2 <=52){
  99.       ladder(currPos);
  100.       snake(currPos);
  101.     }
  102.     else{
  103.       System.out.println("You have passed the boundaries, move back the amount of steps left in your roll.");
  104.     }
  105.   }
  106.   public static void printBoard(){  //Print the conetents of the board
  107.     for(String i : board){
  108.       System.out.print(i+"\t");
  109.     }
  110.     System.out.println("");
  111.   }
  112.   public static int diceRoll(){  //Random number generator for dices
  113.     return (int) (Math.random()* 6) +1;
  114. }
  115. public static boolean checkWin(int currPos1, int currPos2){
  116.   if (currPos1 ==51 && currPos2 == 51){
  117.     return true;
  118.   }
  119.   return false;
  120. }
  121. public static int ladder(int currPos){ //if landed on a ladder change corresponding position value
  122.   int stepIncrease = 0;
  123.   if (currPos ==5){
  124.     stepIncrease = 3;
  125.     currPos = currPos+stepIncrease;
  126.    }
  127.   else if(currPos ==9){
  128.     stepIncrease =5;
  129.     currPos = currPos+stepIncrease;
  130.   }
  131.   else if(currPos ==19){
  132.     stepIncrease =8;
  133.     currPos = currPos+stepIncrease;
  134.   }
  135.   else if(currPos ==23){
  136.     stepIncrease =9;
  137.     currPos = currPos+stepIncrease;
  138.   }
  139.   else if(currPos ==25){
  140.     stepIncrease = 5;
  141.     currPos = currPos+stepIncrease;
  142.   }
  143.   else if(currPos ==31){
  144.     stepIncrease = 2;
  145.     currPos = currPos+stepIncrease;
  146.   }
  147.   else if(currPos ==38){
  148.     stepIncrease = 4;
  149.     currPos = currPos+stepIncrease;
  150.   }
  151.   else if(currPos ==42){
  152.     stepIncrease = 3;
  153.     currPos = currPos+stepIncrease;
  154.   }
  155.   System.out.println("You landed on a ladder and your positin is"+currPos+".");
  156.   return currPos;
  157.  
  158.  
  159.  
  160.  
  161. }
  162. public static int snake(int currPos){
  163.   int stepDecrease = 0;
  164.   if (currPos ==3)
  165.     stepDecrease = 2;
  166.   else if(currPos ==14)
  167.     stepDecrease = 5;
  168.   else if(currPos ==17)
  169.     stepDecrease = 4;
  170.   else if(currPos ==20)
  171.     stepDecrease = 3;
  172.   else if(currPos ==29)
  173.     stepDecrease = 1;
  174.   else if(currPos==34)
  175.     stepDecrease = 2;
  176.   else if(currPos==35)
  177.     stepDecrease=4;
  178.   else if(currPos==36)
  179.     stepDecrease = 5;
  180.   else if(currPos==44)
  181.     stepDecrease = 3;
  182.   else if(currPos==49)
  183.     stepDecrease = 6;
  184.  
  185.     currPos = currPos - stepDecrease;
  186.     return currPos;
  187.  
  188. }
  189. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top