blob: 49844024aafb3b6be5f8a2fda938575709eb3876 (
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
|
/**
* Assignment 2, COMP268 Class: BadmintonScoringWithStroke.java
*
* @description A class used to keep track of a Badminton game between two opponents.
* @author: mo khan Student ID: 3431709
* @date August 3, 2019
* @version 1.0
*/
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;
/**
* Creates an instance of this class with an ArrayList of points for each round.
*
* @param points a list of points for each round
*/
public BadmintonScoringWithStroke(ArrayList<Point> points) {
super(new int[0][0]);
this.points = points;
this.scores = to2DArray(points);
}
/** @return the name of player 1's preferred stroke. */
public String getMostUsedStrokePlayer1() {
return maxStrokeFor(Point.PLAYER1);
}
/** @return the name of player 2's preferred stroke. */
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;
}
}
|