The image rockpaperscissors.png is a functional Rock, Paper, Scissors game that allows the user to play against the CPU.
First and foremost, make sure the npiet interpreter is installed on your device.
Then, once in the directory with npiet, run the following command:
The user is prompted to input either a 'R', 'P', or an 'S' which correspond to Rock, Paper, and Scissors respectively. The game will continue until the user inputs a 'Q' to signify "quit". If, the user inputs any characters that are not one of the four accepted inputs, the program will alert the user of the bad input and will subsequently keep executing.
The program will also keep track of the score between individual games. That is, both the CPU and user will begin at 0 and each score will increment when the corresponding player wins.
Ignore the '?' that appears whenever the user is prompted for input. The program, itself, does not print the question mark character; instead, npiet automatically prints it to inform the user that input is required. For each iteration through the loop (i.e. for each game), notice there are two '?' symbols. This is because the user inputs their choice as well as a '\n' when enter is pressed. The new line character is also read in by the program but is ignored.
The Piet programming language begins its execution in the top left pixel. Thus, the top line of the program prints "Let's Play Rock, Paper, Scissors!" to the screen. The execution then snakes back around to the left and begins the main loop. That is, the program prompts the user for input. This request for input is printed in the second line of codels from the top of the image. Once the program has the input from the user, it checks whether the input falls within the bounds of being less than 80 or greater than 83. All choices (P,Q,R,S) are ASCII characters with decimal values ranging from [80-83]. If any character that is not within that range is inputted, the program prints "Bad input 'x'" where x is the input character. The execution then loops back to the start of the loop and prompts the user for input once again.
If the user inputs a 'Q', the execution is redirected to the green and black codels where the program terminates.
If, however, the user provides the accepted input, the game will be played. The execution then is redirected to one of the nine lines in the middle of the image which signify each of the nine possible combinations of user input vs CPU selection. After either the user or CPU wins a game, the score is updated and then the execution is then redirected to the score function (up and to the right of the nine cases). Note, the score will not be printed if the CPU and user tie in a game. Whether the score is printed or not, however, the execution will always loop back around to the second line in the image which prompts the user for more input. This continues until a 'Q' has been inputted and the game will end.
Since there are no libraries to include in this esoteric language, nor are there any options to generate random numbers, the CPU's choices are hard-coded. Once the program begins, the CPU will choose Scissors. Then the following algorithm will be used to select the CPU's next choice:
Using this algorithm, the CPU chooses each element (R, P, or S) each three times if all nine of the combinations were to occur.
Refer to the table below for further guidance on the CPU's next choice:
|CPU's choice||User's choice||Result||CPU's Next Choice|