Write a Java program that determines a winner between two wrestlers (red or blue
ID: 3597626 • Letter: W
Question
Write a Java program that determines a winner between two wrestlers (red or blue) and the classification points associated with the victory. A sample input file would look like the following:
Red 0 1 1 -
Blue 1 1 0 -
Red 0 0 1 -
Blue 1 2 0 -
Red 4 1 2 1 -
Blue 1 1 2 0 -
Red 1 1 0 -
Blue 0 2 2 -
Red 1 1 2 -
Blue 1 1 2 X
Red 0 1 2 0 1
Blue 4 0 1 -
Red 0 0 0 -
Blue 0 0 0 5
where the first line will indicate the number matches in the data set (m). For each of the next m lines:
The Red player and Blue players on separate lines where the first number(s) indicate the point value(s) earned in the first period each separated by a single space, then two spaces, followed by the point value(s) earned in the second period each separated by a single space, then two spaces, the number of cautions earned in the game, then two spaces followed by a final column, where a value: 1- indicates a fall 2- indicates an injury 3- indicates a withdraw 4- indicates a forfeit 5- indicates a disqualification (contest) 6- indicates a disqualification (competition) X- scored the last technical point otherwise a – dash will be indicated.
Explanation / Answer
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() = 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() 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; } } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.