summaryrefslogtreecommitdiff
path: root/src/Q8/BadmintonScoringWithStroke.java
blob: 64dbd7b8b48e46e8c055d28d02cf26e343e13e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Q8;

import java.util.*;

public class BadmintonScoringWithStroke extends BadmintonScoring {
  private ArrayList<Point> points;
  private static final int PLAYER1 = 0;
  private static final int PLAYER2 = 1;

  public BadmintonScoringWithStroke(ArrayList<Point> points) {
    super(new int[0][0]);
    this.points = points;
    this.scores = to2DArray(points);
  }

  public String getMostUsedStrokePlayer1() {
    return maxStrokeFor(Point.PLAYER1);
  }

  public String getMostUsedStrokePlayer2() {
    return maxStrokeFor(Point.PLAYER2);
  }

  private String maxStrokeFor(int player) {
    int[] strokes = new int[] {0, 0, 0, 0, 0};

    for (Point point : this.points) {
      if (point.getPlayer() != player) continue;

      switch (point.getStroke()) {
        case "slice":
          strokes[0] += 1;
          break;
        case "drive":
          strokes[1] += 1;
          break;
        case "smash":
          strokes[2] += 1;
          break;
        case "drop":
          strokes[3] += 1;
          break;
        case "net-shot":
          strokes[4] += 1;
          break;
      }
    }
    int maxIndex = 0;
    int maxValue = 0;

    for (int i = 0; i < strokes.length; i++) {
      if (strokes[i] > maxValue) {
        maxIndex = i;
        maxValue = strokes[i];
      }
    }

    switch (maxIndex) {
      case 0:
        return "slice";
      case 1:
        return "drive";
      case 2:
        return "smash";
      case 3:
        return "drop";
      case 4:
        return "net-shot";
      default:
        return "unknown";
    }
  }

  private int[][] to2DArray(ArrayList<Point> points) {
    int[][] scores = new int[points.size() + 1][2];
    scores[0][Point.PLAYER1] = 0;
    scores[0][Point.PLAYER2] = 0;

    for (int i = 0; i < points.size(); i++) {
      Point point = points.get(i);

      scores[i + 1][Point.PLAYER1] = i == 0 ? 0 : scores[i][Point.PLAYER1];
      scores[i + 1][Point.PLAYER2] = i == 0 ? 0 : scores[i][Point.PLAYER2];
      scores[i + 1][point.getPlayer()] = point.getScore();
    }

    return scores;
  }
}