Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chutesandladders;
- import java.util.Random;
- import java.util.Scanner;
- public class ChutesAndLadders {
- public ChutesAndLadders() {
- runGame();
- }
- Scanner keyboard = new Scanner(System.in);
- int[] board = new int[25];
- int[] players, current;
- String[] names;
- int roll = 0, currentSpot = 0, hasWinner = 0, currentPlayer = 0, turns = 0;
- String currentName = new String();
- Random rand = new Random();
- private void runGame() {
- runSetup();
- System.out.println("Going in order of name entries.");
- runMain();
- }
- private void runSetup() {
- System.out.println("Welcome to Chutes and Ladders!");
- boolean enoughPlayers = false;
- do { // Gets the number of people playing
- System.out.println("How many players are participating?");
- try {
- players = new int[Integer.parseInt(keyboard.nextLine())];
- names = new String[players.length];
- current = new int[players.length];
- for(int i=0; i<current.length; i++) {current[i] = 0;}
- enoughPlayers = true;
- }
- catch(NumberFormatException nfe) {
- }
- } while (enoughPlayers == false);
- for(int i=0; i<players.length; i++) {
- System.out.println("Who is player " + (i+1) + "?");
- names[i] = keyboard.nextLine();
- }
- }
- private void runMain() {
- do {
- for(int i=0; i<players.length; i++) {
- currentPlayer = i + 1;
- if (i == 0) {
- turns++;
- }
- System.out.println("Rolling for player " + currentPlayer + "...");
- rollDice();
- System.out.println(names[i] + " was on spot " + current[i] + ".");
- current[i] = current[i] + roll;
- currentSpot = current[i];
- currentName = names[i];
- System.out.println(names[i] + " rolled a " + roll + ".");
- if (current[i] >= 100) {
- current[i] = 100;
- hasWinner = 1;
- System.out.println(names[i] + " landed on " + current[i] + ".");
- break;
- }
- else {
- System.out.println(names[i] + " landed on " + current[i] + ".");
- checkSpots();
- current[i] = currentSpot;
- System.out.println(names[i] + " is now at spot " + currentSpot + ".");
- System.out.println("Press enter to continue.");
- keyboard.nextLine();
- }
- }
- } while (hasWinner == 0);
- System.out.println(currentName + " has won the game in " + turns + " turns!");
- }
- public void checkSpots() {
- switch (currentSpot) {
- case 1: {
- currentSpot = 38;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 4: {
- currentSpot = 14;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 9: {
- currentSpot = 31;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 16: {
- currentSpot = 6;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- case 21: {
- currentSpot = 42;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 28: {
- currentSpot = 84;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 36: {
- currentSpot = 44;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 47: {
- currentSpot = 26;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- case 49: {
- currentSpot = 11;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- case 51: {
- currentSpot = 67;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 62: {
- currentSpot = 19;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- case 64: {
- currentSpot = 60;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- case 71: {
- currentSpot = 91;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 80: {
- currentSpot = 99;
- System.out.println(currentName + " landed on a ladder!");
- break;
- }
- case 93: {
- currentSpot = 73;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- case 95: {
- currentSpot = 75;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- case 98: {
- currentSpot = 78;
- System.out.println(currentName + " fell down a slide!");
- break;
- }
- default: {
- }
- }
- }
- public void rollDice() {
- roll = rand.nextInt(6) + 1;
- }
- public static void main(String[] args) {
- new ChutesAndLadders();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement