Use Eclipse Java Write a program to mirror a children’s board game named Chutes
ID: 3601902 • Letter: U
Question
Use Eclipse Java
Write a program to mirror a children’s board game named Chutes and Ladders®.
You will need a separate class for each object (Die, Player), as well as a separate game program (main method). To be clear, you will submit a total of three classes.
Game Algorithm:
1) Create a Die object, and 2 Player objects.
2) Ask for and store the names of the two players.
3) Loop until a player wins (Winning = a player’s current square number is equal to or greater than 100).
a) Roll, then update the first player’s current square number.
b) Roll, then update the second player’s current square number.
c) Check if either player has won.
Please have appropriate prompts and game information.
Remember, this is Chutes and Ladders, so if the current square number is divisible by 10 then “ladder” up an additional dice roll; if the current square number is divisible by 11 then “chute” down an additional dice roll.
Use Eclipse Java
Write a program to mirror a children’s board game named Chutes and Ladders®.
You will need a separate class for each object (Die, Player), as well as a separate game program (main method). To be clear, you will submit a total of three classes.
Die -value: int +getValue(): int+roll(): int Player -name:String
-currentSquareNumber:int +getCurrentSquareNumber():int
+updateCurrentSquareNumber(int n):void
+getName(): String
+setName(String n):void
Game Algorithm:
1) Create a Die object, and 2 Player objects.
2) Ask for and store the names of the two players.
3) Loop until a player wins (Winning = a player’s current square number is equal to or greater than 100).
a) Roll, then update the first player’s current square number.
b) Roll, then update the second player’s current square number.
c) Check if either player has won.
Please have appropriate prompts and game information.
Remember, this is Chutes and Ladders, so if the current square number is divisible by 10 then “ladder” up an additional dice roll; if the current square number is divisible by 11 then “chute” down an additional dice roll.
Explanation / Answer
public class Die {
private int value;
public int getValue() {
return value;
}
public int roll()
{
return (int)(6.0 * Math.random()) + 1;
}
}
public class Player {
private String name;
private int currentSquareNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCurrentSquareNumber() {
return currentSquareNumber;
}
public void updateCurrentSquareNumber(int currentSquareNumber) {
this.currentSquareNumber = this.currentSquareNumber+currentSquareNumber;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String name;
Scanner sc=new Scanner(System.in);
Die die=new Die();
Player player1=new Player();
Player player2=new Player();
System.out.println("Enter name of player 1");
name=sc.nextLine();
player1.setName(name);
System.out.println("Enter name of player 2");
name=sc.nextLine();
player2.setName(name);
player1.updateCurrentSquareNumber(0);
player2.updateCurrentSquareNumber(0);
while((player1.getCurrentSquareNumber()<100)||(player2.getCurrentSquareNumber()<100))
{
int value=die.roll();
System.out.println(player1.getName()+" rolled "+value);
player1.updateCurrentSquareNumber(value);
if(player1.getCurrentSquareNumber()>=100)
{
System.out.println(player1.getName()+" won the game");
break;
}
value=die.roll();
System.out.println(player2.getName()+" rolled "+value);
player2.updateCurrentSquareNumber(value);
if(player2.getCurrentSquareNumber()>=100)
{
System.out.println(player2.getName()+" won the game");
break;
}
}
System.out.println("Game Over");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.