summaryrefslogtreecommitdiff
path: root/src/Q8/BadmintonScoring.java
blob: 56c8e40161a43507dac1f2eb154c6bfec7b0c74d (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package Q8;

import java.util.*;

public class BadmintonScoring {
  protected int[][] scores;
  private static final int PLAYER1 = 0;
  private static final int PLAYER2 = 1;

  public BadmintonScoring(int[][] scores) {
    this.scores = scores;
  }

  public int getContinuousPointsPlayer1() {
    return this.longestStreakFor(PLAYER1);
  }

  public int getContinuousPointsPlayer2() {
    return this.longestStreakFor(PLAYER2);
  }

  public int getPlayer1Points() {
    return this.finalScoreFor(PLAYER1);
  }

  public int getPlayer2Points() {
    return this.finalScoreFor(PLAYER2);
  }

  private int finalScoreFor(int player) {
    int finalScore = 0;
    for (int[] items : scores) finalScore = items[player];
    return finalScore;
  }

  private int longestStreakFor(int player) {
    int streak = 0;
    int longestStreak = 0;
    boolean lastWinner = false;

    for (int i = 0; i < scores.length; i++) {
      int score = scores[i][player];
      int previousScore = i == 0 ? -1 : scores[i - 1][player];
      boolean winner = score > previousScore;

      if (winner && lastWinner) {
        streak++;
        longestStreak = (streak > longestStreak) ? streak : longestStreak;
      } else streak = 0;

      lastWinner = winner;
    }

    return longestStreak;
  }

  private int winnerOf(int round) {
    int player1Score = scores[round][0];
    int player1PreviousScore = scores[round - 1][0];
    return player1Score > player1PreviousScore ? PLAYER1 : PLAYER2;
  }

  public static void main(String[] args) {
    ArrayList<Point> points =
        new ArrayList<Point>() {
          {
            add(new Point(Point.PLAYER1, "a", 1));
            add(new Point(Point.PLAYER1, "c", 2));
            add(new Point(Point.PLAYER2, "d", 1));
            add(new Point(Point.PLAYER2, "e", 2));
            add(new Point(Point.PLAYER2, "d", 3));
            add(new Point(Point.PLAYER2, "e", 4));
            add(new Point(Point.PLAYER2, "d", 5));
            add(new Point(Point.PLAYER1, "a", 3));
            add(new Point(Point.PLAYER1, "c", 4));
            add(new Point(Point.PLAYER2, "e", 6));
            add(new Point(Point.PLAYER2, "e", 7));
            add(new Point(Point.PLAYER2, "a", 8));
            add(new Point(Point.PLAYER2, "d", 9));
            add(new Point(Point.PLAYER2, "e", 10));
            add(new Point(Point.PLAYER2, "e", 11));
            add(new Point(Point.PLAYER2, "e", 12));
            add(new Point(Point.PLAYER2, "e", 13));
            add(new Point(Point.PLAYER2, "e", 14));
            add(new Point(Point.PLAYER2, "e", 15));
            add(new Point(Point.PLAYER1, "c", 5));
            add(new Point(Point.PLAYER2, "e", 16));
            add(new Point(Point.PLAYER2, "e", 17));
            add(new Point(Point.PLAYER2, "e", 18));
            add(new Point(Point.PLAYER2, "e", 19));
            add(new Point(Point.PLAYER2, "e", 20));
            add(new Point(Point.PLAYER2, "e", 21));
          }
        };
    BadmintonScoringWithStroke scoring = new BadmintonScoringWithStroke(points);

    System.out.println("=== Question 8 ===");
    System.out.println(String.format("Player 1 points: %d", scoring.getPlayer1Points()));
    System.out.println(String.format("Player 2 points: %d", scoring.getPlayer2Points()));

    System.out.println(String.format("Player 1 streak: %d", scoring.getContinuousPointsPlayer1()));
    System.out.println(String.format("Player 2 streak: %d", scoring.getContinuousPointsPlayer2()));

    System.out.println(
        String.format("Player 1 favourite stroke: %s", scoring.getMostUsedStrokePlayer1()));
    System.out.println(
        String.format("Player 2 favourite stroke: %s", scoring.getMostUsedStrokePlayer2()));
  }
}