-
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
Changes from all commits
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.*; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * A code-breaking game where the app selects a sequence of symbols, and | ||||||||||||||||||||||||||||||||||
|
|
@@ -22,3 +23,18 @@ public Optional<Integer> play() { | |||||||||||||||||||||||||||||||||
| return Optional.empty(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class CodeGenerator { | ||||||||||||||||||||||||||||||||||
| public static String generateCode() { | ||||||||||||||||||||||||||||||||||
| Random random = new Random(); | ||||||||||||||||||||||||||||||||||
| 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
|
||||||||||||||||||||||||||||||||||
| 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(); |
| 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"); | ||||||
|
||||||
| assertEquals(4, code.length(), "Code should be 4 symbol long"); | |
| assertEquals(4, code.length(), "Code should be 4 symbols long"); |
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
Randominstance on every method call is inefficient. Consider making it a static field or useThreadLocalRandom.current()for better performance.