-
Notifications
You must be signed in to change notification settings - Fork 27
Master mind code gen #142
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?
Master mind code gen #142
Conversation
Syncing Main to MetroCS Repository
Added Code Generator Code and Tests
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.
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"); |
Copilot
AI
Nov 1, 2025
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.
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; |
Copilot
AI
Nov 1, 2025
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.
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(); |
Copilot
AI
Nov 1, 2025
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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