Skip to content

Commit 9624ad1

Browse files
committed
Fix Checkstyle violations and improve setScores validation
- Replace star imports with explicit imports in test file - Remove trailing whitespaces from all lines - Add proper file ending newline - Improve setScores method to handle edge cases properly - Use direct bit manipulation for power-of-2 check as suggested - Ensure error message is printed and scores remain unchanged for invalid input All Checkstyle violations resolved and tests pass successfully.
1 parent 676981a commit 9624ad1

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/main/java/com/thealgorithms/others/MiniMaxAlgorithm.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ private boolean isPowerOfTwo(int n) {
115115
}
116116

117117
public void setScores(int[] scores) {
118-
if (isPowerOfTwo(scores.length)) {
119-
this.scores = scores;
120-
height = log2(this.scores.length);
121-
} else {
118+
if (scores.length <= 0 || (scores.length & (scores.length - 1)) != 0) {
122119
System.out.println("The number of scores must be a power of 2.");
120+
return;
123121
}
122+
this.scores = scores;
123+
height = log2(this.scores.length);
124124
}
125125

126126
public int[] getScores() {

src/test/java/com/thealgorithms/others/MiniMaxAlgorithmTest.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import org.junit.jupiter.api.BeforeEach;
44
import org.junit.jupiter.api.Test;
5-
import static org.junit.jupiter.api.Assertions.*;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
69

710
import java.io.ByteArrayOutputStream;
811
import java.io.PrintStream;
@@ -28,7 +31,7 @@ void testConstructorCreatesValidScores() {
2831
// The default constructor should create scores array of length 8 (2^3)
2932
assertEquals(8, miniMax.getScores().length);
3033
assertEquals(3, miniMax.getHeight());
31-
34+
3235
// All scores should be positive (between 1 and 99)
3336
for (int score : miniMax.getScores()) {
3437
assertTrue(score >= 1 && score <= 99);
@@ -39,7 +42,7 @@ void testConstructorCreatesValidScores() {
3942
void testSetScoresWithValidPowerOfTwo() {
4043
int[] validScores = {10, 20, 30, 40};
4144
miniMax.setScores(validScores);
42-
45+
4346
assertArrayEquals(validScores, miniMax.getScores());
4447
assertEquals(2, miniMax.getHeight()); // log2(4) = 2
4548
}
@@ -48,11 +51,11 @@ void testSetScoresWithValidPowerOfTwo() {
4851
void testSetScoresWithInvalidLength() {
4952
int[] invalidScores = {10, 20, 30}; // Length 3 is not a power of 2
5053
miniMax.setScores(invalidScores);
51-
54+
5255
// Should print error message and not change the scores
5356
String output = outputStream.toString();
5457
assertTrue(output.contains("The number of scores must be a power of 2."));
55-
58+
5659
// Scores should remain unchanged (original length 8)
5760
assertEquals(8, miniMax.getScores().length);
5861
}
@@ -61,7 +64,7 @@ void testSetScoresWithInvalidLength() {
6164
void testSetScoresWithSingleElement() {
6265
int[] singleScore = {42};
6366
miniMax.setScores(singleScore);
64-
67+
6568
assertArrayEquals(singleScore, miniMax.getScores());
6669
assertEquals(0, miniMax.getHeight()); // log2(1) = 0
6770
}
@@ -71,7 +74,7 @@ void testMiniMaxWithKnownScores() {
7174
// Test with a known game tree: [3, 12, 8, 2]
7275
int[] testScores = {3, 12, 8, 2};
7376
miniMax.setScores(testScores);
74-
77+
7578
// Maximizer starts: should choose max(min(3,12), min(8,2)) = max(3, 2) = 3
7679
int result = miniMax.miniMax(0, true, 0, false);
7780
assertEquals(3, result);
@@ -82,7 +85,7 @@ void testMiniMaxWithMinimizerFirst() {
8285
// Test with minimizer starting first
8386
int[] testScores = {3, 12, 8, 2};
8487
miniMax.setScores(testScores);
85-
88+
8689
// Minimizer starts: should choose min(max(3,12), max(8,2)) = min(12, 8) = 8
8790
int result = miniMax.miniMax(0, false, 0, false);
8891
assertEquals(8, result);
@@ -93,10 +96,10 @@ void testMiniMaxWithLargerTree() {
9396
// Test with 8 elements: [5, 6, 7, 4, 5, 3, 6, 2]
9497
int[] testScores = {5, 6, 7, 4, 5, 3, 6, 2};
9598
miniMax.setScores(testScores);
96-
99+
97100
// Maximizer starts
98101
int result = miniMax.miniMax(0, true, 0, false);
99-
// Expected: max(min(max(5,6), max(7,4)), min(max(5,3), max(6,2)))
102+
// Expected: max(min(max(5,6), max(7,4)), min(max(5,3), max(6,2)))
100103
// = max(min(6, 7), min(5, 6)) = max(6, 5) = 6
101104
assertEquals(6, result);
102105
}
@@ -105,9 +108,9 @@ void testMiniMaxWithLargerTree() {
105108
void testMiniMaxVerboseOutput() {
106109
int[] testScores = {3, 12, 8, 2};
107110
miniMax.setScores(testScores);
108-
111+
109112
miniMax.miniMax(0, true, 0, true);
110-
113+
111114
String output = outputStream.toString();
112115
assertTrue(output.contains("Maximizer"));
113116
assertTrue(output.contains("Minimizer"));
@@ -118,7 +121,7 @@ void testMiniMaxVerboseOutput() {
118121
void testGetRandomScoresLength() {
119122
int[] randomScores = MiniMaxAlgorithm.getRandomScores(4, 50);
120123
assertEquals(16, randomScores.length); // 2^4 = 16
121-
124+
122125
// All scores should be between 1 and 50
123126
for (int score : randomScores) {
124127
assertTrue(score >= 1 && score <= 50);
@@ -129,7 +132,7 @@ void testGetRandomScoresLength() {
129132
void testGetRandomScoresWithDifferentParameters() {
130133
int[] randomScores = MiniMaxAlgorithm.getRandomScores(2, 10);
131134
assertEquals(4, randomScores.length); // 2^2 = 4
132-
135+
133136
// All scores should be between 1 and 10
134137
for (int score : randomScores) {
135138
assertTrue(score >= 1 && score <= 10);
@@ -140,7 +143,7 @@ void testGetRandomScoresWithDifferentParameters() {
140143
void testMainMethod() {
141144
// Test that main method runs without errors
142145
assertDoesNotThrow(() -> MiniMaxAlgorithm.main(new String[]{}));
143-
146+
144147
String output = outputStream.toString();
145148
assertTrue(output.contains("The best score for"));
146149
assertTrue(output.contains("Maximizer"));
@@ -152,7 +155,7 @@ void testHeightCalculation() {
152155
int[] scores2 = {1, 2};
153156
miniMax.setScores(scores2);
154157
assertEquals(1, miniMax.getHeight()); // log2(2) = 1
155-
158+
156159
int[] scores16 = new int[16];
157160
miniMax.setScores(scores16);
158161
assertEquals(4, miniMax.getHeight()); // log2(16) = 4
@@ -162,7 +165,7 @@ void testHeightCalculation() {
162165
void testEdgeCaseWithZeroScores() {
163166
int[] zeroScores = {0, 0, 0, 0};
164167
miniMax.setScores(zeroScores);
165-
168+
166169
int result = miniMax.miniMax(0, true, 0, false);
167170
assertEquals(0, result);
168171
}
@@ -171,7 +174,7 @@ void testEdgeCaseWithZeroScores() {
171174
void testEdgeCaseWithNegativeScores() {
172175
int[] negativeScores = {-5, -2, -8, -1};
173176
miniMax.setScores(negativeScores);
174-
177+
175178
// Tree evaluation with maximizer first:
176179
// Level 1 (minimizer): min(-5,-2) = -5, min(-8,-1) = -8
177180
// Level 0 (maximizer): max(-5, -8) = -5

0 commit comments

Comments
 (0)