/** * Assignment 2, COMP268 Class: BadmintonScoring.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 BadmintonScoring { protected int[][] scores; private static final int PLAYER1 = 0; private static final int PLAYER2 = 1; /** * Creates an instance of the BadmintonScoring object with a 2D array of scores. * * @param scores a 2D array of scores for each player. */ public BadmintonScoring(int[][] scores) { this.scores = scores; } /** @return the longest point streak for player 1. */ public int getContinuousPointsPlayer1() { return this.longestStreakFor(PLAYER1); } /** @return the longest point streak for player 2. */ public int getContinuousPointsPlayer2() { return this.longestStreakFor(PLAYER2); } /** @return the final score for player 1. */ public int getPlayer1Points() { return this.finalScoreFor(PLAYER1); } /** @return the final score for player 2. */ 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 points = new ArrayList() { { 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())); } }