-
Notifications
You must be signed in to change notification settings - Fork 0
Mastermind #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Mastermind #98
Changes from all commits
840ffe7
4d9e63d
21ca93d
95e244b
0183f8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||
import java.util.Optional; | ||||||
import java.util.Random; | ||||||
|
||||||
/** | ||||||
* A code-breaking game where the app selects a sequence of symbols, and | ||||||
|
@@ -11,9 +12,23 @@ | |||||
* @version 1 | ||||||
*/ | ||||||
class MasterMindGame implements Game { | ||||||
|
||||||
private static final char[] VALID_CHARACTER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; | ||||||
|
||||||
private static final int CODE_LENGTH = 4; | ||||||
|
||||||
public String getName() { return "MasterMind"; } | ||||||
public Optional<Integer> play() { | ||||||
System.out.println("[Playing MasterMind - Placeholder]"); | ||||||
return Optional.empty(); | ||||||
} | ||||||
|
||||||
public String generateCode() { | ||||||
Random r = new Random(System.nanoTime()); | ||||||
StringBuilder out = new StringBuilder(); | ||||||
for (int i = 0; i < CODE_LENGTH; i++) { | ||||||
out.append(VALID_CHARACTER[r.nextInt(10)]); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace the hard-coded literal 10 with VALID_CHARACTER.length for improved maintainability, ensuring that any changes to the valid character set are automatically reflected in generateCode().
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
return out.toString(); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.HashSet; | ||
|
||
public class MasterMindGameTest { | ||
|
||
private MasterMindGame mm; | ||
private static final char[] VALID_CHARACTER = | ||
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; | ||
|
||
|
||
@BeforeEach | ||
public void setUp() { | ||
mm = new MasterMindGame(); | ||
} | ||
|
||
@Test | ||
public void testCodeLengthCorrectness() { | ||
assertEquals(4, mm.generateCode().length()); | ||
} | ||
|
||
@Test | ||
public void testCodeContentCorrectness() { | ||
int count = 100; | ||
String[] codes = new String[count]; | ||
for (int i = 0; i < count; i++) { | ||
codes[i] = mm.generateCode(); | ||
} | ||
|
||
assertEquals(0, checkIncorrectCharacters(codes)); | ||
} | ||
|
||
private static int checkIncorrectCharacters(final String[] in) { | ||
int out = 0; | ||
for (int i = 0; i < in.length; i++) { | ||
for (int j = 0; j < in[i].length(); j++) { | ||
if (!contains(VALID_CHARACTER, in[i].charAt(j))) { | ||
out++; | ||
} | ||
} | ||
} | ||
return out; | ||
} | ||
|
||
private static boolean contains(final char[] bank, final char key) { | ||
for (char b: bank) { | ||
if (b == key) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Test | ||
public void testCodeRepetition() { | ||
/* | ||
* I was curious as to the odds of an overlap, and before spending ages | ||
* rerunning "ant test" to no avail, I checked via Wolfram Alpha. | ||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If written in first person, a comment needs to also provide authorship. |
||
* It appears that 10,000 choose 100 (100 codes being checked, and | ||
* values from 0000 to 9999) is on the order of 6 * 10^241. Code | ||
* collisions are very unlikely given that many possible unique | ||
* combinations. | ||
*/ | ||
int count = 100; | ||
HashSet<String> codes = new HashSet<>(count); | ||
for (int i = 0; i < count; i++) { | ||
codes.add(mm.generateCode()); | ||
} | ||
|
||
// Commented below after ensuring code equality check is correct | ||
// printCodes(codes); | ||
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove commented-out code. |
||
assertEquals(count, codes.size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An assertion failure message here would be welcome to better explain what was being tested. |
||
} | ||
|
||
private static void printCodes(final HashSet<String> in) { | ||
for (String i: in) { | ||
System.out.println(i); | ||
} | ||
} | ||
Comment on lines
+77
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As identified by static analysis tools, this method is never used and should be deleted. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider reusing a single Random instance rather than creating a new one on every call to generateCode() to reduce overhead and improve randomness consistency.
Copilot uses AI. Check for mistakes.