Conversation
Syncing Main to MetroCS Repository
Added Code Generator Code and Tests
There was a problem hiding this comment.
Pull Request Overview
This PR adds a CodeGenerator class to generate random 4-digit codes for the MasterMind game and introduces comprehensive unit tests to verify the code generation behavior.
- Adds
CodeGeneratorclass withgenerateCode()method that creates random 4-digit codes using digits 0-9 - Adds
MasterMindGameTestwith three test cases validating code length, symbol validity, and randomness distribution
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| MasterMindGame.java | Adds CodeGenerator class with static method to generate random 4-digit codes and imports java.util.* |
| MasterMindGameTest.java | New test file with three tests validating code generation properties |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| void isExactlyFourSymbols() { | ||
|
|
||
| String code = CodeGenerator.generateCode(); | ||
| assertEquals(4, code.length(), "Code should be 4 symbol long"); |
There was a problem hiding this comment.
Corrected spelling of 'symbol' to 'symbols' in the error message.
| assertEquals(4, code.length(), "Code should be 4 symbol long"); | |
| assertEquals(4, code.length(), "Code should be 4 symbols long"); |
| String code = ""; | ||
|
|
||
| for (int i=0; i < 4; i++) { | ||
| int index = random.nextInt(numbers.length()); | ||
| code = code + numbers.charAt(index); | ||
| } | ||
|
|
||
| return code; |
There was a problem hiding this comment.
String concatenation in a loop is inefficient. Use StringBuilder instead to avoid creating multiple intermediate String objects.
| 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(); |
|
|
||
| class CodeGenerator { | ||
| public static String generateCode() { | ||
| Random random = new Random(); |
There was a problem hiding this comment.
Creating a new Random instance on every method call is inefficient. Consider making it a static field or use ThreadLocalRandom.current() for better performance.
There was a problem hiding this comment.
Verify that coding conventions are followed.
Here's a report excerpt from Checkstyle (via ant checkstyle):
File ConsoleGameHub/MasterMindGame.java
Error Description :: Line
Using the '.*' form of import should be avoided - java.util.*.:: 2
Missing a Javadoc comment. :: 27
Utility classes should not have a public or default constructor. :: 27
Missing a Javadoc comment. :: 28
'=' is not preceded with whitespace. :: 33
'=' is not followed by whitespace. :: 33
'4' is a magic number. :: 33
File ConsoleGameHub/MasterMindGameTest.java
Error Description :: Line
File does not end with a newline. :: 0
Using the '.*' form of import should be avoided - org.junit.jupiter.api.Assertions.*. :: 2
Using the '.*' form of import should be avoided - java.util.*. :: 4
Using the '.*' form of import should be avoided - java.util.regex.*. :: 5
Line has trailing spaces. :: 10
Closes: #44