Advertisement
cclloyd

Chutes and Ladders

Jun 5th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. package chutesandladders;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class ChutesAndLadders {
  7.    
  8.     public ChutesAndLadders() {
  9.         runGame();
  10.     }
  11.    
  12.     Scanner keyboard = new Scanner(System.in);
  13.     int[] board = new int[25];
  14.     int[] players, current;
  15.     String[] names;
  16.     int roll = 0, currentSpot = 0, hasWinner = 0, currentPlayer = 0, turns = 0;
  17.     String currentName = new String();
  18.     Random rand = new Random();
  19.    
  20.     private void runGame() {
  21.         runSetup();
  22.         System.out.println("Going in order of name entries.");
  23.         runMain();
  24.     }
  25.    
  26.     private void runSetup() {
  27.         System.out.println("Welcome to Chutes and Ladders!");
  28.         boolean enoughPlayers = false;
  29.         do { // Gets the number of people playing
  30.             System.out.println("How many players are participating?");
  31.             try {
  32.             players = new int[Integer.parseInt(keyboard.nextLine())];
  33.             names = new String[players.length];
  34.             current = new int[players.length];
  35.             for(int i=0; i<current.length; i++) {current[i] = 0;}
  36.             enoughPlayers = true;
  37.             }
  38.             catch(NumberFormatException nfe) {
  39.                
  40.             }
  41.         } while (enoughPlayers == false);
  42.         for(int i=0; i<players.length; i++) {
  43.             System.out.println("Who is player " + (i+1) + "?");
  44.             names[i] = keyboard.nextLine();
  45.         }
  46.     }
  47.    
  48.     private void runMain() {       
  49.         do {
  50.             for(int i=0; i<players.length; i++) {
  51.                 currentPlayer = i + 1;
  52.                 if (i == 0) {
  53.                     turns++;
  54.                 }
  55.                 System.out.println("Rolling for player " + currentPlayer + "...");
  56.                 rollDice();
  57.                 System.out.println(names[i] + " was on spot " + current[i] + ".");
  58.                 current[i] = current[i] + roll;
  59.                 currentSpot = current[i];
  60.                 currentName = names[i];
  61.                 System.out.println(names[i] + " rolled a " + roll + ".");
  62.                 if (current[i] >= 100) {
  63.                     current[i] = 100;
  64.                     hasWinner = 1;
  65.                     System.out.println(names[i] + " landed on " + current[i] + ".");
  66.                     break;
  67.                 }
  68.                 else {
  69.                     System.out.println(names[i] + " landed on " + current[i] + ".");
  70.                     checkSpots();
  71.                     current[i] = currentSpot;
  72.    
  73.                     System.out.println(names[i] + " is now at spot " + currentSpot + ".");
  74.                     System.out.println("Press enter to continue.");
  75.                     keyboard.nextLine();
  76.                 }
  77.             }
  78.         } while (hasWinner == 0);
  79.         System.out.println(currentName + " has won the game in " + turns + " turns!");
  80.        
  81.     }
  82.    
  83.     public void checkSpots() {
  84.         switch (currentSpot) {
  85.         case 1: {
  86.             currentSpot = 38;
  87.             System.out.println(currentName + " landed on a ladder!");
  88.             break;
  89.         }
  90.         case 4: {
  91.             currentSpot = 14;
  92.             System.out.println(currentName + " landed on a ladder!");
  93.             break;
  94.         }
  95.         case 9: {
  96.             currentSpot = 31;
  97.             System.out.println(currentName + " landed on a ladder!");
  98.             break;
  99.         }
  100.         case 16: {
  101.             currentSpot = 6;
  102.             System.out.println(currentName + " fell down a slide!");
  103.             break;
  104.         }
  105.         case 21: {
  106.             currentSpot = 42;
  107.             System.out.println(currentName + " landed on a ladder!");
  108.             break;
  109.         }
  110.         case 28: {
  111.             currentSpot = 84;
  112.             System.out.println(currentName + " landed on a ladder!");
  113.             break;
  114.         }
  115.         case 36: {
  116.             currentSpot = 44;
  117.             System.out.println(currentName + " landed on a ladder!");
  118.             break;
  119.         }
  120.         case 47: {
  121.             currentSpot = 26;
  122.             System.out.println(currentName + " fell down a slide!");
  123.             break;
  124.         }
  125.         case 49: {
  126.             currentSpot = 11;
  127.             System.out.println(currentName + " fell down a slide!");
  128.             break;
  129.         }
  130.         case 51: {
  131.             currentSpot = 67;
  132.             System.out.println(currentName + " landed on a ladder!");
  133.             break;
  134.         }
  135.         case 62: {
  136.             currentSpot = 19;
  137.             System.out.println(currentName + " fell down a slide!");
  138.             break;
  139.         }
  140.         case 64: {
  141.             currentSpot = 60;
  142.             System.out.println(currentName + " fell down a slide!");
  143.             break;
  144.         }
  145.         case 71: {
  146.             currentSpot = 91;
  147.             System.out.println(currentName + " landed on a ladder!");
  148.             break;
  149.         }
  150.         case 80: {
  151.             currentSpot = 99;
  152.             System.out.println(currentName + " landed on a ladder!");
  153.             break;
  154.         }
  155.         case 93: {
  156.             currentSpot = 73;
  157.             System.out.println(currentName + " fell down a slide!");
  158.             break;
  159.         }
  160.         case 95: {
  161.             currentSpot = 75;
  162.             System.out.println(currentName + " fell down a slide!");
  163.             break;
  164.         }
  165.         case 98: {
  166.             currentSpot = 78;
  167.             System.out.println(currentName + " fell down a slide!");
  168.             break;
  169.         }
  170.         default: {
  171.         }
  172.         }
  173.     }
  174.    
  175.     public void rollDice() {
  176.         roll = rand.nextInt(6) + 1;
  177.     }
  178.    
  179.     public static void main(String[] args) {
  180.         new ChutesAndLadders();
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement