15
0
From a challenge in a programming book long ago, PNZ is a game where the user must guess three unique digits in the correct order.
Rules:
- A random 3 digit number with no repeating digits is generated. (This is what the user is trying to guess)
- The user inputs a guess of 3 digits, which is to be evaluated by the program.
- Output a "P" for every correct digit in a correct place.
- Output a "N" for every correct digit in an incorrect place.
- Output a "Z" only if no digits are correct.
- Continue accepting inputs until all digits are correct and in the correct place, then output "PPP" followed by the number of guesses it took on a new line.
Note:
A "Correct digit" means that one of the digits in the guess is also one of the digits in the random 3 digit number.
A "Correct place" means it is a "Correct digit" AND is in the same place as the 3 digit random number.
The order of outputting should be all "P"'s first, then "N"'s, or only "Z" if nothing is correct.
If an input contains repeating digits, "P" takes priority over "N" (Example:
Number: 123
Input: 111
Output: P
)(OPTIONAL) Inputs that are not exactly 3 digits in length should not be evaluated, nor count towards the running total of guesses
Example if the generated digits were 123
> 147
P
> 152
PN
> 126
PP
> 123
PPP
4
Example if the generated digits were 047
> 123
Z
> 456
N
> 478
NN
> 947
PP
> 047
PPP
5
This is CodeGolf, so the shortest program wins!
Welcome to PPCG! This is a great first challenge, but I'm afraid we've done this before. The game is otherwise known as Mastermind. Here is the existing challenge but I can't make up my mind whether to close the old one or the new. I'm slightly leaning towards closing this, but I'll let the community decide.
– Martin Ender – 2016-01-20T16:49:42.850@MartinBüttner Ah, that's my bad. It does seem like a pretty similar problem. I'll agree with you and let the community decide. – Mr Public – 2016-01-20T16:52:01.870
@MartinBüttner What's the criterion here? To what extend should the old one have precedence? – Luis Mendo – 2016-01-20T17:26:31.770
2@MartinBüttner I think between requiring the digits to be unique and the interactive nature, this challenge is distinct enough to be worthwhile. – AdmBorkBork – 2016-01-20T17:44:39.787
@LuisMendo There is no official criterion I think, because closing old challenges is a fairly recent thing. My personal criterion is "which challenge is better and/or more barebones". – Martin Ender – 2016-01-20T17:52:46.127
@TimmyD I disagree with the interactivity thing, because I think it's negligible in comparison with figuring out the
P
s andN
s and I think that a challenge asking only for the latter part is more interesting. However, you may be right that limiting it to unique digits might allow for some different approaches. – Martin Ender – 2016-01-20T17:53:55.360@MrPublic Suppose the secret number is
123
. If the user input is composed of non-distinct digits, e.g.,111
, how should the program respond? – AdmBorkBork – 2016-01-20T20:22:42.793Number 123, gues 111: why nothing? There is a digit in the right position – edc65 – 2016-01-21T13:43:57.030
@edc65 I agree with your statement and retract my last one. By the challenge specifications, the correct output would indeed be "P". – Mr Public – 2016-01-21T13:57:23.830