ME 105 Midterm 1: Tenzi Party Tenzi is a frenetic dice game that is a fun party
ID: 3888814 • Letter: M
Question
ME 105 Midterm 1: Tenzi Party Tenzi is a frenetic dice game that is a fun party activity. The game uses sets of ten (10) 6-sided dice to play. Each player holds 10 dice in hands. When the game starts, everyone rolls at the same time. Each player looks at the first roll and decides which number to roll all 10 dice to become. For example, if the first roll shows more 3's than any other number, it's logical to go for all 3s. Put all the dice with the number aside and quickly roll the remaining dice. Keep rolling until all 10 dice show the same number. The goal of the game is to roll fast and be the first player to get all 10 of the dice to show the same number. The first player that shouts "TENZI" wins the game The following link contains the detail game description http://zenomath.org/wp-content/uploads/2015/02/Zeno-Family-Math-Game-TENZI.pdf Write a MATLAB program to simulate the Tenzi party game automatically that can Play Tenzi for multiple players simultaneously and automatically Ask the game host to input how many players will play. Use ten (10) 6-sided dice to automatically play the Tenzi game for each player. For example, 4 players will use a total of forty (40) dice. Generate ten (10) random integers between 1 and 6 to represent the face value of rolling ten dice for each player to simulate all players roll at the same time. For example, a 6-player game will generate 60 random integers. Print all the dice values on screen and clearly indicate which player (i.e. player 1, player 2, and so on...) rolls what values. Based on the first roll results, the program should automatically decide the best numbers to keep for all the players. The program should be able to automatically keep different dice values best for each player. The program must be able to make the best decisions without bias for all the players 1. 2. 3. 4. 5. 6.Explanation / Answer
Game class:
import java.util.Scanner;
public class Game {
private Player p1;
private Player p2;
private Dice dice;
private int scoreToWin;
void displayGameMenu() {
System.out.println();
System.out.println("(1) Start a new game");
System.out.println("(2) Play one round");
System.out.println("(3) Who is leading now?");
System.out.println("(4) Display game help");
System.out.println("(5) Exit game");
System.out.print("Choose an option: ");
}
void selectGameOption(int optionSelected) {
switch (optionSelected) {
case 1:
this.startNewGame();
break;
case 2:
this.playOneRound(p1);
this.playOneRound(p2);
break;
case 3:
this.whoIsLeading();
break;
case 4:
this.displayGameInstruction();
break;
default:
break;
}
}
void startNewGame() {
String p1Name;
String p2Name;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter player one name: ");
p1Name = sc.nextLine();
System.out.print("Please enter player two name: ");
p2Name = sc.nextLine();
System.out.print("Please enter the maximum score required to win: ");
scoreToWin = sc.nextInt();
p1 = new Player(p1Name);
p2 = new Player(p2Name);
dice = new Dice();
}
void playOneRound(Player p) {
int result;
int firstDiceRoll = dice.rollDice();
int secondDiceRoll = dice.rollDice();
if (firstDiceRoll == secondDiceRoll) {
result = (firstDiceRoll + secondDiceRoll) * 2;
p.setTotalScore(result);
System.out.printf("%s rolled %d and %d, "
+ "and scored %d points(BONUS DOUBLE POINTS), "
+ "for a total of %d points",
p.getName(), firstDiceRoll, secondDiceRoll,
result, p.getTotalScore()
);
} else {
result = (firstDiceRoll + secondDiceRoll);
p.setTotalScore(result);
System.out.printf("%s rolled %d and %d, "
+ "and scored %d points, "
+ "for a total of %d points",
p.getName(), firstDiceRoll, secondDiceRoll,
result, p.getTotalScore()
);
}
System.out.println();
}
void whoIsLeading() {
if (p1.getTotalScore() == p2.getTotalScore()) {
System.out.format("Its currently a draw, "
+ "%s has %d, %s has %d",
p1.getName(), p1.getTotalScore(),
p2.getName(), p2.getTotalScore()
);
} else if (p1.getTotalScore() > p2.getTotalScore()) {
System.out.printf("%s is leading, %s has %d points, "
+ "%s has %d points",
p1.getName(), p1.getName(), p1.getTotalScore(),
p2.getName(), p2.getTotalScore());
} else if (p1.getTotalScore() < p2.getTotalScore()) {
System.out.format("%s is leading, %s has %d points, "
+ "%s has %d points.",
p2.getName(), p2.getName(), p2.getTotalScore(),
p1.getName(), p1.getTotalScore()
);
}
}
void displayGameInstruction() {
System.out.println();
System.out.println("All players roll a dice twice per turn.");
System.out.println("If 2 dice rolls have the same value, the player scores 2 times the sum two dice rolls.");
System.out.println("If 2 dice rolls have different values, the player simply scores the sum of two dice rolls.");
System.out.println("For each player, result is incremented after each turn.");
System.out.println("First player to reach or exceed the maxScore wins the game");
}
boolean checkIfAnyoneHasWon() {
if (p1.getTotalScore() >= scoreToWin && p2.getTotalScore() >= scoreToWin) {
System.out.println("Its a draw! Both players have exceeded the score limit");
return true;
} else if (p1.getTotalScore() >= scoreToWin && p2.getTotalScore() < scoreToWin) {
System.out.format("%s won", p1.getName());
return true;
} else if (p1.getTotalScore() < scoreToWin && p2.getTotalScore() >= scoreToWin) {
System.out.format("%s won", p2.getName());
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println("Welcome to the Dice and Roll game!");
Game game = new Game();
int optionSelected;
while (true) {
game.displayGameMenu();
System.out.println();
Scanner sc = new Scanner(System.in);
optionSelected = sc.nextInt();
while (optionSelected > 5 || optionSelected < 0) {
System.out.print("Option entered invalid, please enter a number from 1 to 5: ");
optionSelected = sc.nextInt();
}
if (optionSelected == 5) {
System.out.println("Exiting game");
break;
}
game.selectGameOption(optionSelected);
boolean anyoneWin = game.checkIfAnyoneHasWon();
if (anyoneWin) {
System.out.println();
System.out.println("Game ended.");
break;
}
}
}
}
Player class:
public class Player {
private final String name;
private int totalScore;
Player(String name){
this.name = name;
}
String getName(){
return name;
}
int getTotalScore(){
return totalScore;
}
void setTotalScore(int score){
totalScore += score;
}
}
Dice class:
import java.util.Random;
public class Dice {
private final static int numberOfSides = 6;
int rollDice() {
int result;
Random randomNumberGenerator = new Random();
result = randomNumberGenerator.nextInt(numberOfSides) + 1;
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.