/** * 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 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 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 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; } }