Methods: Mastermind Game: Fall 2025

04 Aug 2025 - 19:07 | Version 1 |

Learning Objective(s)

  • Improve skills with methods
  • Have some fun (but work hard)

Overview of the Program

Create a program that allows the user to play mastermind. See the web for examples. One site: https://www.mahjongfun.com/mastermind/

Specifics

  • The program is to be called MasterMind.
  • There is no file i/o for this program.
  • You need to use at least two constants: One for the number of different "color" options per peg and another for the number of pegs.
  • The player is allowed to continue to guess until they get the right answer.
  • The program should continue to allow the player to play multiple games until they respond "n" to the question asking if they wish to continue.
  • The program should use strings to store the solution and guesses.
  • The different colors are represented by single digits (at most from 0 - 9)
  • The program should have the following methods (beside the main):
    • playGame (that is sent the keyboard) which plays one instance of a complete game
    • makeSolution (which is sent the number of different possible colors and the number of pegs) and returns a random valid string representing the solution
    • getGuess (that is sent the keyboard) and returns a single guess. Because this is a complex program, error checking is not needed this time.
    • getNumPerfect (that is sent the solution and the guess) that returns the number of "perfect pegs" (same position and same color). For example: 1212 and 1231 would return 2.
    • countLetter (that is sent a letter which will typically be a digit and a string) that returns the number of times that digit occurs in the string. For example: 4 and 13445 would return 4.
    • getNumClose (that is sent the solution and the guess) that returns the number of shared colors that are not in the right position.
  • a min method (that is sent two integers and returns the smaller one) would be helpful.

Sample dialogue

Below is a sample dialogue.
What is your guess? 1224
There are 0 exactly right and 3 in the wrong spot.
What is your guess? 4112
There are 3 exactly right and 0 in the wrong spot.
What is your guess? 4132
There are 2 exactly right and 2 in the wrong spot.
What is your guess? 4312
You got it right in 4 turns.
Do you wish to play again (y or n)? n
You can also run a solution for this program if you want. See classroom instructions for more details.

Lab Hints

Submission

  • Create a software development report
    • For the system design, you need to make your own flowchart.
    • For testing report,
      • make sure your tests ensure that each statement is executed at least one.
      • your expected output CANNOT be pasted from the dialogue of a running. Only the actual output may be pasted
      • It should contain at least one different input that is an "error"
    • For the improvement section, specify how many points were lost from the last lab that you should not lose points for on this lab because you corrected the problem
    • create a pdf document from your software development report
    • create an empty zip folder
    • place only MasterMind.java and your pdf document into it
    • submit the zip folder on canvas

Optional improvements

Optional improvements should ONLY be worked on once the program is completing working. In addition, the work is to be done on your own with little if any help for lab assistants or the instructor.
  1. Only allow the user to enter a valid guess (correct number of pegs and allowed colors)
  2. Output the number of guesses it took to get the right answer. At the end of the program, output the average number of guesses over all the games played.
  3. Submission instructions
    1. Call this program MasterMind2 add it to the zip folder that contains the original solution. Be sure to include appropriate testing data in your sdr for your extra credit
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
This website is using cookies. More info. That's Fine