Skip to content

Conversation

TylerAndreasen
Copy link
Collaborator

Addresses #44

The generateCode() method psuedo-randomly generates a four character code from the set of digits 0-9.
Requesting Review from two willing others in accordance with this comment on Issue #44

…indGame. Implemented code length, character correctness, and code repetition tests in MasterMind Test.
…as incorrect. Extended Code to four characters, which passes the Code Length and content tests.
…time issues and decouples content and length tests.
…ode Generation using Java's Random utility and the System Time. Verified that codes of 4 characters from a set of 10 characters are vanishingly unlikely to collide in a sample of 100.
Copy link
Contributor

@jody jody left a comment

Choose a reason for hiding this comment

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

Runtime behavior test checks out fine (no change).
Regression tests check out fine (no change).

Checkstyle reports new errors in MasterMindGame.java.
Screenshot 2025-05-07 at 4 02 41 PM
A suggestion for handling the magic number "10" is to use the length property of the VALID_CHARACTER array.

PMD identified potential weaknesses in MasterMindGameTest.java.
Screenshot 2025-05-07 at 4 14 39 PM
The first isn't likely to be appropriate to this case. The second correctly identifies code that should be deleted from the file.

Checkstyle reports a minor error to be corrected in MasterMindGameTest.java.
Screenshot 2025-05-07 at 4 34 10 PM

Comment on lines +59 to +60
* 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.
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.

Comment on lines +72 to +73
// Commented below after ensuring code equality check is correct
// printCodes(codes);
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.


// Commented below after ensuring code equality check is correct
// printCodes(codes);
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.

Comment on lines +77 to +81
private static void printCodes(final HashSet<String> in) {
for (String i: in) {
System.out.println(i);
}
}
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.

@jody jody requested a review from Copilot May 10, 2025 18:30
Copy link

@Copilot Copilot AI left a 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 addresses Issue #44 by implementing the generateCode() method to pseudo-randomly generate a four-character code composed of digits 0-9, and adds comprehensive tests in MasterMindGameTest.java.

  • Implements generateCode() in MasterMindGame.java
  • Adds unit tests ensuring code length, content, and uniqueness in MasterMindGameTest.java

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
MasterMindGameTest.java Adds tests validating code properties and randomness
MasterMindGame.java Implements code generation with pseudo-random digits

Comment on lines +27 to +30
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)]);
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.

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)]);
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.

@jody
Copy link
Contributor

jody commented May 10, 2025

Screenshot 2025-05-10 at 12 36 36 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Games Associated with Games project
Projects
Development

Successfully merging this pull request may close these issues.

2 participants