Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Mastermind Java help Please! I need help writing a text-based Mastermind game with Java. Mastermind is a game where the player tries to figure out

Mastermind Java help Please!

I need help writing a text-based Mastermind game with Java. Mastermind is a game where the player tries to figure out a pattern based on a series of clues. 4 different colored pegs (R for red, B for blue, G for green, W for white, P for purple, and Y for yellow) are chosen and hidden from the player. Then the user picks 4 pegs and guesses at the pattern. The computer will tell the user how many pegs are the correct color (hint, hint you may want to reuse them in the next guess). The computer will also tell the user how many of the correctly colored pegs are also in the correct position. The user has up to 10 attempts to figure out the pattern.

The program should have the following files:

1. Mastermind.java (already provided- contains main())

import.java.util.Scanner;

public class Mastermind { public static void main(String[] args)

{

Scanner input = new Scanner(System.in); MastermindPuzzle mp; int easyHard; int puzzleNum;

System.out.print("1=easy, 2=hard? "); easyHard = input.nextInt(); System.out.println();

System.out.print("Puzzle num? "); puzzleNum = input.nextInt(); System.out.println();

if (easyHard == 1) { mp = new EasyMastermindPuzzle(); }

else { mp = new HardMastermindPuzzle(); }

mp.startGame(puzzleNum);

char[] guess = new char[mp.getNumPieces()];

while (mp.stillPlaying()) { System.out.print("Guess? "); for (int i = 0; i < guess.length; i++)

{ guess[i] = (input.next()).charAt(0); }

System.out.println(); mp.makeGuess(guess);

System.out.printf("%20d %d --> ", mp.getLastCorrectColorCount(), mp.getLastCorrectPositionCount());

for (int i = 0; i < guess.length; i++) { System.out.print(guess[i]); System.out.print(' '); }

System.out.println();

}

if (mp.won()) { System.out.println("Congratulations!"); }

else

{ System.out.println("Sorry, you lost"); }

} }

2. MastermindPuzzle.java

All instance variables should be private (not public or protected)

Should be abstract

Needs to implement Winnable interface

Need at least the default constructor

getNumPieces needs to return an integer with the number of pegs in a puzzle (currently 4 use constants!)

chooseSolution should accept an integer that is the puzzle to choose and it must be abstract

startGame should accept an integer that is the puzzle to choose (1 through 3 or -1 for a random puzzle)

You must call chooseSolution in this method

I recommend that this method also reset your other variables that remember the state of the current game

makeGuess accepts a character array with the letters to check

I recommend that this method increase the number of guesses made, and set 2 instance variables that indicate the number of correctly colored pegs and the number in the correct position

This is the longest, most complicated method in the class

getLastCorrectColorCount needs to return the number of pegs that are the correct color in the last guess

getLastCorrectPositionCount needs to return the number of pegs that are in the correct position in the last guess

You may add extra methods to help yourself

3. EasyMastermindPuzzle.java

This should be a subclass of MastermindPuzzle

This class ONLY needs constructor(s) and chooseSolutionchooseSolution should do the following:

If the parameter is 1, set the puzzle to R, G, B, W

If the parameter is 2, set the puzzle to P, R, W, Y

If the parameter is 3, set the puzzle to W, B, Y, P

If the parameter is -1, set the puzzle to random colors (4 out of R, G, B, W, Y, P)

4. HardMastermindPuzzle.java

This should be a subclass of MastermindPuzzle

This class ONLY needs constructor(s) and chooseSolutionchooseSolution should do the following:

If the parameter is 1, set the puzzle to R, G, B, R

If the parameter is 2, set the puzzle to P, R, P, Y

If the parameter is 3, set the puzzle to W, W, Y, P

If the parameter is -1, set the puzzle to random colors (4 out of R, G, B, W, Y, P) with at least 1 duplicate color within in the puzzle

5. Winnable.java

This should have 3 methods each returns a boolean: won, lost, stillPlaying

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions