/**
 * Class for simulating volleyball games using both rally and sideout scoring.
 *   @author Dave Reed
 *   @version 4/01/08
 */
public class VolleyballSim {
    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 VolleyballSim(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 1 or 2)
     */
    public int playPoint() {
        if (this.roller.roll() <= this.ranking1) {
            return 1;
        }
        else {
            return 2;
        }
    }

    /**
     * Method that simulates an entire volleyball game.
     *   @param winningPoints the number of points needed to win the game (winningPoints > 0)
     *   @return the winner of the game (either 1 or 2)
     */
     public int playGame(int winningPoints) {
        int score1 = 0;
        int score2 = 0;
      
        int winner = 0;
        while ((score1 < winningPoints && score2 < winningPoints)
                              || (Math.abs(score1 - score2) <= 1)) {           
            winner = this.playPoint();
            if (winner == 1) {
                score1++;
            }
            else {
                score2++;
            }
                    
            //System.out.println(" team " + winner + " wins point (" + score1 + "-" + score2 + ")");
        }
        return winner;
    }
}