Java
5. [P5_Mastermind] You are to build a text-based version of the game Mastermind (Links to an external site.)Links to an external site. . The game is played with two-players: the code master and the code breaker.
The code master chooses four different colored pegs to form a code. The pegs can be any of six colors: Red, Orange, Yellow, Green, Blue, Purple. For example the code master might choose Orange, Orange, Blue and Purple as the four peg code. We could abbreviate this code as OOBP.
The code breaker then makes guesses to try and determine the code. For example, the code breaker could guess Blue, Yellow, Orange, Purple, which we could abbreviate as BYOP. After the guess is made, the code master provides two types of feedback on the guess:
1. a count of how many colored pegs are in the correct position (e.g., if the code is OOBP and the guess is BYOP, then the Purple peg is in the correct position so the master would say 1 peg is in the correct position)
2. a count of how many pegs are a color in the code, but in the wrong position (e.g., if the code is OOBP and the guess is BYOP, then the Blue and Orange pegs in the guess are present in the code but in the wrong positions, so the master would say 2 pegs are the correct color).
One important thing to notice: if the code is OOBP and the guess is BOOO, then the feedback should 1 peg is in the correct position (the first O in the guess) and 1 peg is the correct color but in the wrong position, not 2. The O's in the 3rd and 4th position of the guess should not both be counted because there are only two O's in the code and one of them is already covered by the O in the correct position. (Frankly, this is the most difficult part of this assignment and it is strongly recommended you work out the other portions of this program before you get bogged down in this problem.)
Your goal is to have the computer play the role of the code master and the user will be the code breaker. The code breaker has 10 turns to correctly guess they code or they lose the game. Here is an example game:
|
Welcome to MASTERMIND The possible color pegs are (R)ed, (O)range, (Y)ellow, (G)reen, (B)lue, (P)urple. The code is only 4 pegs long. Enter guess number 1: BBRR 0 correct positions 0 correct colors in wrong positions Enter guess number 2: OOPP 1 correct positions 1 correct colors in wrong positions Enter guess number 3: OOYY 0 correct positions 0 correct colors in wrong positions Enter guess number 4: PPGG 2 correct positions 2 correct colors in wrong positions Enter guess number 5: PGPG 2 correct positions 2 correct colors in wrong positions Enter guess number 6: PGGP You got it in 6 guesses
|
|
Welcome to MASTERMIND The possible color pegs are (R)ed, (O)range, (Y)ellow, (G)reen, (B)lue, (P)urple. The code is only 4 pegs long. Enter guess number 1: RROO 0 correct positions 0 correct colors in wrong positions Enter guess number 2: YYGG 1 correct positions 0 correct colors in wrong positions Enter guess number 3: YBBB 1 correct positions 0 correct colors in wrong positions Enter guess number 4: PBBB 2 correct positions 0 correct colors in wrong positions Enter guess number 5: PBRR 1 correct positions 1 correct colors in wrong positions Enter guess number 6: PRBR 2 correct positions 0 correct colors in wrong positions Enter guess number 7: PYBR 2 correct positions 0 correct colors in wrong positions Enter guess number 8: PYBG 3 correct positions 0 correct colors in wrong positions Enter guess number 9: PBGG 2 correct positions 1 correct colors in wrong positions Enter guess number 10: PBBG 3 correct positions 0 correct colors in wrong positions The code was PPBG |
Where I can do them in Eclipse: