Message
Message
Scanner;
//Game runner. Do not modify this! Write the methods below to make the game
playable.
public static void main(String[] args) {
while (endspace == -1) {
setSizeOfBoard();
}
scan.nextLine();
System.out.println("\n-------------\nGame Starting\n-------------");
System.out.println("First player to reach space " + endspace + "
wins!");
while (p1Space < endspace && p2Space < endspace) {
doTurn();
}
validateGameEnd();
}
System.out.println("You spun " + dieRoll + ", so you move " + dieRoll + "
spaces forward.");
}
p1Space += dieRoll;
checkSquare(p1Space);
playerLocs();
p1Turn = false;
}
else{
System.out.print("\nPlayer 2's turn. Press enter to spin: ");
scan.nextLine();
if(dieRoll == 1){
System.out.println("You spun " + dieRoll + ", so you move " + dieRoll + "
space forward.");
} else {
System.out.println("You spun " + dieRoll + ", so you move " + dieRoll + "
spaces forward.");
}
p2Space += dieRoll;
checkSquare(p2Space);
playerLocs();
p1Turn = true;
}
}
/* Calculates the result of a turn based off the square the player is on
** If the player is on a ladder, snake, or chance square,
** calculates and returns the result of that square
** If NOT a ladder, snake, or chance square, returns the same space player
is already on
** num: current space the player is on
*/
public static int checkSquare(int num) {
if (num >= endspace) return endspace;
if((num - 1) / 2 % snakeTail == 0 && num >= 3)
num = snake(num);
else if(num % ladderBottom == 0)
num = ladder(num);
else if(num % chanceMultiplier == 0)
num = chance(num);
else{
return num;
}
//given the head of the snake as startingSpot, return the tail of the snake
public static int snake(int startingSpot) {
int originalSpot = startingSpot;
startingSpot = (startingSpot - 1)/2;
int difference = originalSpot - startingSpot;
if(difference > 10){
difference = 10;
}
System.out.println("You hit a snake, so you move down " + difference + "
spaces...");
if (p1Turn){
p1Space -= difference;
}
else{
p2Space -= difference;
}
return (originalSpot + difference);
}
//given the bottom of a ladder as startingSpot, return the top of the ladder
public static int ladder(int startingSpot) {
int originalSpot = startingSpot;
startingSpot = 3 * startingSpot - 7;
int difference = startingSpot - originalSpot;
if(difference > 10){
difference = 10;
}
if (originalSpot + difference >= endspace) return startingSpot;
System.out.println("You hit a ladder, so you move up " + difference + "
spaces!");
if (p1Turn){
p1Space += difference;
}
else{
p2Space += difference;
}
return (originalSpot + difference);
}
return startingSpot;
}
//Flips a coin and returns a boolean
public static boolean flipCoin(){
int coinflip = (int)(Math.random() * 2);
if(coinflip == 1){
return true;
}
else{
return false;
}
}
/* Returns the random spin for the player after waiting for enter to be
pressed
** Between 1-6, inclusive
*/
public static int getSpin() {
return (int)(Math.random()*(6-1+1)+1);
}
}
}