0% found this document useful (0 votes)
19 views5 pages

Message

Uploaded by

sdluke2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Message

Uploaded by

sdluke2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.util.

Scanner;

public class SnakesAndLadders {


//Do not modify these variables!
static Scanner scan = new Scanner(System.in);
static int endspace = -1;
static int p1Space = 0;
static int p2Space = 0;
static boolean p1Turn = true;

//Write the declarations of these variables to get a random multiplier for


the tail of the snake, bottom of the ladder, and chance square locations.
static int snakeTail = (int)(Math.random()*(5-3+1)+3);
static int ladderBottom = (int)(Math.random()*(7-4+1)+4);
static int chanceMultiplier = (int)(Math.random()*(6-2+1)+2);

//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();
}

/* Runs on every turn of the game


** On each turn, one player rolls a die. After checking if the roll is a
snake or ladder,
** the player moves accordingly.
** Finally, the player positions are displayed, and the next player is
prompted to roll.
*/
public static void doTurn() {
//System.out.println();
int dieRoll = getSpin();
if (p1Turn){
System.out.print("\nPlayer 1'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.");
}
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;
}
}

/* This is called when one player reaches the ending space


** If p1 wins, print "Player 1 wins!"
** If p2 wins, print "Player 2 wins!"
*/
public static void validateGameEnd() {
if(p1Space>=endspace){
System.out.print("Player 1 wins!");
}
else if(p2Space>=endspace){
System.out.print("Player 2 wins!");
}
}

/* 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);
}

//given the position of a chance square as startingSpot, prompt the player


for if they wish to gamble their position, and return their final position.
public static int chance(int startingSpot) {
int originalSpace = startingSpot;
int moveUp = 0;
System.out.print("You found a chance square! Would you like to toss a coin to
either move up or down by 2 spaces? (y/n): ");
String response = scan.nextLine();
while (response.equals("y")){
boolean isHeads = flipCoin();
if(isHeads){
moveUp +=2;
startingSpot +=2;
System.out.print("You got heads, so you can move up " + moveUp + " places!
Do you want to risk it to move up two more spaces? (y/n): ");
response = scan.nextLine();
if(response.equals("n")) {
if(p1Turn){
p1Space = startingSpot;
}
else{
p2Space = startingSpot;
}
break;
}
} else {
startingSpot = originalSpace - 2;
if(p1Turn){
p1Space = startingSpot;
}
else{
p2Space = startingSpot;
}
System.out.println("You got tails, so you move down two places from your
original place...");
break;
}
}

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);
}

/* returns a string containing the location of each player


** Ex:
** Player 1 is at space 12, and Player 2 is at space 16.
*/
public static String playerLocs() {
if(p1Space < 0){
p1Space = 0;
}
if (p2Space < 0){
p2Space = 0;
}
if (p1Space > endspace){
p1Space = endspace;
}
if (p2Space > endspace){
p2Space = endspace;
}
System.out.println("Player 1 is at space " + p1Space + ", and Player 2 is at
space " + p2Space + ".");
return "";
}

/* sets the size of the board


** should prompt the player to input a value between 10 and 100 inclusive
** validate user input and set the endspace for the game
*/
public static void setSizeOfBoard() {
System.out.print("Welcome to Snakes and Ladders! How large do you want your
board to be (between 10 and 100 squares)?: ");
endspace = scan.nextInt();
if(endspace > 100 || endspace < 10){
String s = endspace + " is invalid; please provide at least 10 squares and at
most 100 squares.";
System.out.println(s);
for (int i = 0; i < s.length(); i++) System.out.print("-");
System.out.println();
endspace = -1;
}

}
}

You might also like