package a01;

import java.util.Scanner;
import java.util.Random;

/**
 * A program to simulate playing "Chutes/Snakes and Ladders".
 * Players roll the dice and advance along a track.
 * Some spaces on the track have chutes (slides; snakes in some games)
 * that the player slides down to an earlier space.
 * Some spaces on the track have ladders that lead up to a later space,
 * which the player climbs.
 *
 * The first player to the end of the track is the winner.
 *
 * @author Mark Young (A00000000)
 */
public class ChutesAndLadders {

    // class constants (any program only needs one of each of these)
    private static final Scanner KBD = new Scanner(System.in);
    private static final Random RAND = new Random();

    // class constants (game settings)
    private static final int NUM_SPACES = 6;
    private static final int DIE_SIDES = 6;

    public static void main(String[] args) {
        // create required variables
        int[] board;
        int position;
        int winner = -1; // -1 == no winner yet
        
        // set up the game
        board = makeBoard();
        position = 0;

        // introduce yourself
        printIntroduction();
        pause();

        // keep rolling until someone wins
        while (winner == -1) {
            // roll and announce roll
            int roll = rollDie();
            System.out.println("Player rolls " + roll);

            // advance player to new position
            position = position + roll;
            System.out.println("Player reaches " + position);

            // look for chutes and/or ladders
            while (board[position] != position) {
                if (board[position] < position) {
                    // going down a chute
                    position = board[position];
                    System.out.println("Player slides down a chute to " 
                            + position);
                } else {
                    // going up a ladder
                    position = board[position];
                    System.out.println("Player climbs up a ladder to " 
                            + position);
                }
            }

            // check for winner
            if (position >= NUM_SPACES) {
                winner = 0;
            }

            // pause after each turn
            pause();
        }

        // announce winner
        System.out.println("Player " + winner + " wins!");
    }

    /**
     * Print the introduction to this program.
     */
    private static void printIntroduction() {
        System.out.println("We're going to play Chutes and ladders!");
    }

    /**
     * Prompt the user and wait for them to press the enter key.
     */
    private static void pause() {
        System.out.println();
        System.out.print("...press enter...");
        KBD.nextLine();
        System.out.println();
    }

    /**
     * Roll the die.
     *
     * @return the number rolled
     */
    private static int rollDie() {
        return RAND.nextInt(DIE_SIDES) + 1;
    }

    /**
     * Create the game board. The board is represented by an array with a space
     * for every position on the board, plus sufficient extra spaces for if the
     * player rolls a number that takes them past the end of the board.
     * <p>
     * Every element of the array indicates where a player landing on that 
     * space ends up. For a chute/snake, the number will be smaller than the
     * index; for a ladder it'll be larger. For spaces that are not the start 
     * of a chute/snake/ladder, the element will be equal to its index.
     *
     * @return an array representing the game board
     */
    private static int[] makeBoard() {
        int[] board = new int[NUM_SPACES + DIE_SIDES];

        // original board -- every space except the last has a chute/snake
        // going back down to the start position (0).
        board[NUM_SPACES] = NUM_SPACES;

        return board;
    }

}
