Classes: Final version of Mastermind

30 Mar 2023 - 13:28 | Version 1 |
At the end of this lab, you will have a complete mastermind game and have written your first completely object oriented program that uses two classes.

Learning Objective(s)

  1. Experience the development and use of multiple classes
  2. Finish the mastermind game
  3. End the semester programming on a good note.

Overview of the Program

Finish the mastermind game

Details

Part 1: Create the Row class

  1. The class should have two constants (NUMHOLES and NUMCOLORS) and one field variable which would be an integer array that will hold either the solution or a user's guess.
  2. There should be two constructors:
    1. First constructor has no arguments and generates a random guess for the array.
    2. The second constructor is sent an array with numbers (already inputted by the user) and creates a row from it. This constructor should handle two error conditions (the array being sent is too small or too large).
  3. There are no mutators because you can't change a guess or the solution, just create new ones.
  4. There is one standard accessor. It returns the color (an int) of a specific hole (an int).
  5. You should implement two standard helper methods: toString, equals
  6. Use the code from a previous lab to implement a method to return the number of colors in the guess (an argument) that are perfect
  7. Use the code from a previous lab to return an array of the count of the numbers of each of the colors in a guess (or solution)
  8. Use the code from a previous lab to return how many of the guesses are good (but not perfect).

Part 2: Create the MastermindGame class

  1. The main method should repeat playing a complete game until the user decides to quit.
  2. The method to play a game should be sent a scanner and continue to get guesses from the user until the user guesses the solution.
    1. It constructors a computer row (using random numbers)
    2. It uses another method to get an array of numbers from the user for a guess
    3. It uses that array to constructor a guess row
    4. It then determines how many where perfect and how many were good and reports that to the user
    5. It tracks the number of guesses

Sample dialogue

I have the solution.  What is your guess?
1 1 1 1 1
On guess 1:
You got 0 perfect.
You got 0 good.
What is your next guess?
2 2 2 2 2
On guess 2:
You got 2 perfect.
You got 0 good.
What is your next guess?
2 2 3 3 3
On guess 3:
You got 0 perfect.
You got 2 good.
What is your next guess?
4 4 2 2 4
On guess 4:
You got 3 perfect.
You got 0 good.
What is your next guess?
4 0 2 2 0
On guess 5:
You got 3 perfect.
You got 2 good.
What is your next guess?
0 4 2 2 0
You guessed it.
Do you want to play again (y or n)? n

Submission

  1. create an empty zip folder
  2. place your java file into it
  3. create a pdf document that contains your Software Development Report for this program and place the pdf file into the zip folder
  4. submit the zip folder on canvas
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