master
  1/**
  2 * Assignment 2, COMP268 Class: BadmintonScoringWithStroke.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 BadmintonScoringWithStroke extends BadmintonScoring {
 14  private ArrayList<Point> points;
 15  private static final int PLAYER1 = 0;
 16  private static final int PLAYER2 = 1;
 17
 18  /**
 19   * Creates an instance of this class with an ArrayList of points for each round.
 20   *
 21   * @param points a list of points for each round
 22   */
 23  public BadmintonScoringWithStroke(ArrayList<Point> points) {
 24    super(new int[0][0]);
 25    this.points = points;
 26    this.scores = to2DArray(points);
 27  }
 28
 29  /** @return the name of player 1's preferred stroke. */
 30  public String getMostUsedStrokePlayer1() {
 31    return maxStrokeFor(Point.PLAYER1);
 32  }
 33
 34  /** @return the name of player 2's preferred stroke. */
 35  public String getMostUsedStrokePlayer2() {
 36    return maxStrokeFor(Point.PLAYER2);
 37  }
 38
 39  private String maxStrokeFor(int player) {
 40    int[] strokes = new int[] {0, 0, 0, 0, 0};
 41
 42    for (Point point : this.points) {
 43      if (point.getPlayer() != player) continue;
 44
 45      switch (point.getStroke()) {
 46        case "slice":
 47          strokes[0] += 1;
 48          break;
 49        case "drive":
 50          strokes[1] += 1;
 51          break;
 52        case "smash":
 53          strokes[2] += 1;
 54          break;
 55        case "drop":
 56          strokes[3] += 1;
 57          break;
 58        case "net-shot":
 59          strokes[4] += 1;
 60          break;
 61      }
 62    }
 63    int maxIndex = 0;
 64    int maxValue = 0;
 65
 66    for (int i = 0; i < strokes.length; i++) {
 67      if (strokes[i] > maxValue) {
 68        maxIndex = i;
 69        maxValue = strokes[i];
 70      }
 71    }
 72
 73    switch (maxIndex) {
 74      case 0:
 75        return "slice";
 76      case 1:
 77        return "drive";
 78      case 2:
 79        return "smash";
 80      case 3:
 81        return "drop";
 82      case 4:
 83        return "net-shot";
 84      default:
 85        return "unknown";
 86    }
 87  }
 88
 89  private int[][] to2DArray(ArrayList<Point> points) {
 90    int[][] scores = new int[points.size() + 1][2];
 91    scores[0][Point.PLAYER1] = 0;
 92    scores[0][Point.PLAYER2] = 0;
 93
 94    for (int i = 0; i < points.size(); i++) {
 95      Point point = points.get(i);
 96
 97      scores[i + 1][Point.PLAYER1] = i == 0 ? 0 : scores[i][Point.PLAYER1];
 98      scores[i + 1][Point.PLAYER2] = i == 0 ? 0 : scores[i][Point.PLAYER2];
 99      scores[i + 1][point.getPlayer()] = point.getScore();
100    }
101
102    return scores;
103  }
104}