forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStats.java
More file actions
154 lines (136 loc) Β· 5.44 KB
/
GameStats.java
File metadata and controls
154 lines (136 loc) Β· 5.44 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Game Statistics and Utilities for Number Guessing Game
* Author: GitHub Copilot
* Description: Helper class for game statistics and utility functions
*/
public class GameStats {
// Difficulty levels
public enum Difficulty {
EASY(1, 50, 10), // 1-50, 10 attempts
MEDIUM(1, 100, 7), // 1-100, 7 attempts
HARD(1, 200, 5), // 1-200, 5 attempts
EXPERT(1, 500, 8); // 1-500, 8 attempts
private final int minNumber;
private final int maxNumber;
private final int maxAttempts;
Difficulty(int minNumber, int maxNumber, int maxAttempts) {
this.minNumber = minNumber;
this.maxNumber = maxNumber;
this.maxAttempts = maxAttempts;
}
public int getMinNumber() { return minNumber; }
public int getMaxNumber() { return maxNumber; }
public int getMaxAttempts() { return maxAttempts; }
public int getRange() { return maxNumber - minNumber + 1; }
}
/**
* Calculates the theoretical minimum attempts needed using binary search
* @param range The range of numbers (max - min + 1)
* @return Minimum attempts needed
*/
public static int calculateMinimumAttempts(int range) {
if (range <= 0) return 0;
return (int) Math.ceil(Math.log(range) / Math.log(2));
}
/**
* Calculates a score based on attempts used and difficulty
* @param attempts Number of attempts used
* @param maxAttempts Maximum attempts allowed
* @param range Range of numbers
* @return Score from 0 to 1000
*/
public static int calculateScore(int attempts, int maxAttempts, int range) {
if (attempts > maxAttempts) return 0;
// Base score for winning
double baseScore = 500;
// Bonus for fewer attempts (max 300 points)
double efficiencyBonus = ((double)(maxAttempts - attempts) / maxAttempts) * 300;
// Bonus for difficulty (max 200 points)
double difficultyBonus = Math.min(200, range / 5.0);
return (int) Math.round(baseScore + efficiencyBonus + difficultyBonus);
}
/**
* Gets performance rating based on attempts and maximum attempts
* @param attempts Attempts used
* @param maxAttempts Maximum attempts allowed
* @return Performance rating string
*/
public static String getPerformanceRating(int attempts, int maxAttempts) {
double efficiency = (double) attempts / maxAttempts;
if (efficiency <= 0.2) {
return "LEGENDARY";
} else if (efficiency <= 0.4) {
return "EXCELLENT";
} else if (efficiency <= 0.6) {
return "GOOD";
} else if (efficiency <= 0.8) {
return "FAIR";
} else {
return "LUCKY";
}
}
/**
* Generates helpful tips for the player
* @param gamesPlayed Number of games played
* @param winRate Win rate percentage
* @return Array of tips
*/
public static String[] generateTips(int gamesPlayed, double winRate) {
if (gamesPlayed < 3) {
return new String[]{
"π‘ Use binary search strategy: start with the middle number!",
"π‘ Pay attention to 'hot' and 'cold' hints!",
"π‘ Keep track of your previous guesses mentally."
};
} else if (winRate < 50) {
return new String[]{
"π‘ Try the binary search method: always guess the middle of remaining range!",
"π‘ If the range is 1-100, start with 50, then 25 or 75 based on feedback.",
"π‘ Don't make random guesses - be systematic!"
};
} else {
return new String[]{
"π‘ You're doing great! Try to win in fewer attempts for higher scores!",
"π‘ Challenge yourself with harder difficulty levels!",
"π‘ Aim for the theoretical minimum attempts when possible."
};
}
}
/**
* Validates if a number is within the given range
* @param number The number to validate
* @param min Minimum allowed value
* @param max Maximum allowed value
* @return true if valid, false otherwise
*/
public static boolean isValidGuess(int number, int min, int max) {
return number >= min && number <= max;
}
/**
* Calculates the optimal next guess using binary search strategy
* @param low Current lower bound
* @param high Current upper bound
* @return Optimal next guess
*/
public static int getOptimalGuess(int low, int high) {
return low + (high - low) / 2;
}
/**
* Formats game statistics into a readable string
* @param gamesPlayed Total games played
* @param gamesWon Total games won
* @param totalScore Total score accumulated
* @return Formatted statistics string
*/
public static String formatStatistics(int gamesPlayed, int gamesWon, int totalScore) {
if (gamesPlayed == 0) {
return "No games played yet!";
}
double winRate = (double) gamesWon / gamesPlayed * 100;
double averageScore = (double) totalScore / gamesPlayed;
return String.format(
"Games: %d | Wins: %d | Win Rate: %.1f%% | Avg Score: %.0f",
gamesPlayed, gamesWon, winRate, averageScore
);
}
}