Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions MasterMindGame.java
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
Expand All @@ -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)]);
Comment on lines +27 to +30
Copy link
Preview

Copilot AI May 10, 2025

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.

Suggested change
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)]);
StringBuilder out = new StringBuilder();
for (int i = 0; i < CODE_LENGTH; i++) {
out.append(VALID_CHARACTER[RANDOM.nextInt(10)]);

Copilot uses AI. Check for mistakes.

Copy link
Preview

Copilot AI May 10, 2025

Choose a reason for hiding this comment

The 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
out.append(VALID_CHARACTER[r.nextInt(10)]);
out.append(VALID_CHARACTER[r.nextInt(VALID_CHARACTER.length)]);

Copilot uses AI. Check for mistakes.

}
return out.toString();
}
}
82 changes: 82 additions & 0 deletions MasterMindGameTest.java
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove commented-out code.

assertEquals(count, codes.size());
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}