master
  1/**
  2 * Assignment 2, COMP268 Class: BadmintonScoring.java
  3 *
  4 * @description A class used to keep track of a Badminton game between two opponents.
  5 * @author: mo khan Student ID: 3431709
  6 * @date August 3, 2019
  7 * @version 1.0
  8 */
  9package Q8;
 10
 11import java.util.*;
 12
 13public class BadmintonScoring {
 14  protected int[][] scores;
 15  private static final int PLAYER1 = 0;
 16  private static final int PLAYER2 = 1;
 17
 18  /**
 19   * Creates an instance of the BadmintonScoring object with a 2D array of scores.
 20   *
 21   * @param scores a 2D array of scores for each player.
 22   */
 23  public BadmintonScoring(int[][] scores) {
 24    this.scores = scores;
 25  }
 26
 27  /** @return the longest point streak for player 1. */
 28  public int getContinuousPointsPlayer1() {
 29    return this.longestStreakFor(PLAYER1);
 30  }
 31
 32  /** @return the longest point streak for player 2. */
 33  public int getContinuousPointsPlayer2() {
 34    return this.longestStreakFor(PLAYER2);
 35  }
 36
 37  /** @return the final score for player 1. */
 38  public int getPlayer1Points() {
 39    return this.finalScoreFor(PLAYER1);
 40  }
 41
 42  /** @return the final score for player 2. */
 43  public int getPlayer2Points() {
 44    return this.finalScoreFor(PLAYER2);
 45  }
 46
 47  private int finalScoreFor(int player) {
 48    int finalScore = 0;
 49    for (int[] items : scores) finalScore = items[player];
 50    return finalScore;
 51  }
 52
 53  private int longestStreakFor(int player) {
 54    int streak = 0;
 55    int longestStreak = 0;
 56    boolean lastWinner = false;
 57
 58    for (int i = 0; i < scores.length; i++) {
 59      int score = scores[i][player];
 60      int previousScore = i == 0 ? -1 : scores[i - 1][player];
 61      boolean winner = score > previousScore;
 62
 63      if (winner && lastWinner) {
 64        streak++;
 65        longestStreak = (streak > longestStreak) ? streak : longestStreak;
 66      } else streak = 0;
 67
 68      lastWinner = winner;
 69    }
 70
 71    return longestStreak;
 72  }
 73
 74  private int winnerOf(int round) {
 75    int player1Score = scores[round][0];
 76    int player1PreviousScore = scores[round - 1][0];
 77    return player1Score > player1PreviousScore ? PLAYER1 : PLAYER2;
 78  }
 79
 80  public static void main(String[] args) {
 81    ArrayList<Point> points =
 82        new ArrayList<Point>() {
 83          {
 84            add(new Point(Point.PLAYER1, "a", 1));
 85            add(new Point(Point.PLAYER1, "c", 2));
 86            add(new Point(Point.PLAYER2, "d", 1));
 87            add(new Point(Point.PLAYER2, "e", 2));
 88            add(new Point(Point.PLAYER2, "d", 3));
 89            add(new Point(Point.PLAYER2, "e", 4));
 90            add(new Point(Point.PLAYER2, "d", 5));
 91            add(new Point(Point.PLAYER1, "a", 3));
 92            add(new Point(Point.PLAYER1, "c", 4));
 93            add(new Point(Point.PLAYER2, "e", 6));
 94            add(new Point(Point.PLAYER2, "e", 7));
 95            add(new Point(Point.PLAYER2, "a", 8));
 96            add(new Point(Point.PLAYER2, "d", 9));
 97            add(new Point(Point.PLAYER2, "e", 10));
 98            add(new Point(Point.PLAYER2, "e", 11));
 99            add(new Point(Point.PLAYER2, "e", 12));
100            add(new Point(Point.PLAYER2, "e", 13));
101            add(new Point(Point.PLAYER2, "e", 14));
102            add(new Point(Point.PLAYER2, "e", 15));
103            add(new Point(Point.PLAYER1, "c", 5));
104            add(new Point(Point.PLAYER2, "e", 16));
105            add(new Point(Point.PLAYER2, "e", 17));
106            add(new Point(Point.PLAYER2, "e", 18));
107            add(new Point(Point.PLAYER2, "e", 19));
108            add(new Point(Point.PLAYER2, "e", 20));
109            add(new Point(Point.PLAYER2, "e", 21));
110          }
111        };
112    BadmintonScoringWithStroke scoring = new BadmintonScoringWithStroke(points);
113
114    System.out.println("=== Question 8 ===");
115    System.out.println(String.format("Player 1 points: %d", scoring.getPlayer1Points()));
116    System.out.println(String.format("Player 2 points: %d", scoring.getPlayer2Points()));
117
118    System.out.println(String.format("Player 1 streak: %d", scoring.getContinuousPointsPlayer1()));
119    System.out.println(String.format("Player 2 streak: %d", scoring.getContinuousPointsPlayer2()));
120
121    System.out.println(
122        String.format("Player 1 favourite stroke: %s", scoring.getMostUsedStrokePlayer1()));
123    System.out.println(
124        String.format("Player 2 favourite stroke: %s", scoring.getMostUsedStrokePlayer2()));
125  }
126}