/** * Simulates a single game of volleyball using rally scoring. * @author Dave Reed * @version 8/25/08 * */ public class VolleyballSimulator { private Die roller; // Die for simulating points private int ranking1; // power ranking of team 1 private int ranking2; // power ranking of team 2 /** * Constructs a volleyball game simulator. * @param team1Ranking the power ranking (0-100) of team 1, the team that serves first * @param team2Ranking the power ranking (0-100) of team 2, the receiving team */ public VolleyballSimulator(int team1Ranking, int team2Ranking) { this.roller = new Die(team1Ranking+team2Ranking); this.ranking1 = team1Ranking; this.ranking2 = team2Ranking; } /** * Simulates a single rally between the two teams. * @return the winner of the rally (either "team 1" or "team 2") */ public String playRally() { if (this.roller.roll() <= this.ranking1) { return "team 1"; } else { return "team 2"; } } /** * Simulates an entire game using the rally scoring system. * @param winningPoints the number of points needed to win the game (winningPoints > 0) * @return the winner of the game (either "team 1" or "team 2") */ public String playGame(int winningPoints) { int score1 = 0; int score2 = 0; String winner = ""; while ((score1 < winningPoints && score2 < winningPoints) || (Math.abs(score1 - score2) <= 1)) { winner = playRally(); if (winner.equals("team 1")) { score1++; } else { score2++; } // System.out.println(winner + " wins the point (" + score1 + "-" + score2 + ")"); } return winner; } }