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
16 changes: 16 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.*;

/**
* A code-breaking game where the app selects a sequence of symbols, and
Expand All @@ -22,3 +23,18 @@ public Optional<Integer> play() {
return Optional.empty();
}
}

class CodeGenerator {
public static String generateCode() {
Random random = new Random();
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

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

Creating a new Random instance on every method call is inefficient. Consider making it a static field or use ThreadLocalRandom.current() for better performance.

Copilot uses AI. Check for mistakes.
String numbers = "0123456789";
String code = "";

for (int i=0; i < 4; i++) {
int index = random.nextInt(numbers.length());
code = code + numbers.charAt(index);
}

return code;
Comment on lines +31 to +38
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

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

String concatenation in a loop is inefficient. Use StringBuilder instead to avoid creating multiple intermediate String objects.

Suggested change
String code = "";
for (int i=0; i < 4; i++) {
int index = random.nextInt(numbers.length());
code = code + numbers.charAt(index);
}
return code;
StringBuilder code = new StringBuilder();
for (int i=0; i < 4; i++) {
int index = random.nextInt(numbers.length());
code.append(numbers.charAt(index));
}
return code.toString();

Copilot uses AI. Check for mistakes.
}
}
45 changes: 45 additions & 0 deletions MasterMindGameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import java.util.*;
import java.util.regex.*;

public class MasterMindGameTest {

private static final String SYMBOLS = "0123456789";
private static final Pattern CODE_PATTERN =
Pattern.compile("[" + Pattern.quote(SYMBOLS) + "]{4}");


@Test
void isExactlyFourSymbols() {

String code = CodeGenerator.generateCode();
assertEquals(4, code.length(), "Code should be 4 symbol long");
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'symbol' to 'symbols' in the error message.

Suggested change
assertEquals(4, code.length(), "Code should be 4 symbol long");
assertEquals(4, code.length(), "Code should be 4 symbols long");

Copilot uses AI. Check for mistakes.

}

@Test
void areFromSymbolSet() {

String code = CodeGenerator.generateCode();
assertTrue(CODE_PATTERN.matcher(code).matches(), "Code must be from this symbol set: " + SYMBOLS);

}

@Test
void isNotMoreThanTwiceTheSize() {

Map<String, Integer> counts = new HashMap<>();
int sampleSize = 100;

for (int i = 0; i < sampleSize; i++) {
String code = CodeGenerator.generateCode();
counts.merge(code, 1, Integer::sum);
}

counts.forEach((code, count) ->
assertTrue(count <= 2, "Code '" + code + "' appeared " + count + " times"));
}

}
Loading