
This is a game I created whilst following along with the book. Its intent is to play a game of rock paper scissors with user all the while keeping track of the wins, losses and tries.
The output should be something like follows:
System: Windows
USERwins : 0 USERties : 0 USERlosses : 0
CPUchoice : 1
CPUchoice : Paper
Enter your choice (0: Rock, 1: Paper, 2: Scissor):
2
USERchoice : 2
USERchoice : Scissor
USERwins : 1 USERties : 0 USERlosses : 0
Would you like to continue (Type yes), press enter if not :
yes
System: Windows
USERwins : 1 USERties : 0 USERlosses : 0
CPUchoice : 2
CPUchoice : Scissor
Enter your choice (0: Rock, 1: Paper, 2: Scissor):
1
USERchoice : 1
USERchoice : Paper
USERwins : 1 USERties : 0 USERlosses : 1
Would you like to continue (Type yes), press enter if not :
Process finished with exit code 1
Now I will walk through the code of the program, first, let’s discuss the imports. I have imported the os, sys, random, platform and nameof (from the varname library).
import os, sys, random, platform
from varname import nameof
I have imported the platform package to check the Operating System the code is being run on. And further the os package to run command-line instructions through the python program. In this instance I have defined a function clear() to clear the screen each time the program starts to run through the main loop.
def clear():
if platform.system() == "Windows":
print("System:",platform.system(), sep="\t")
os.system("cls")
if platform.system() == "Linux":
print("System:", platform.system(), sep="\t")
os.system("clear")
The sys package is used in the end of the program to exit with exit code 1 on successful completion of the program.
sys.exit(1)
The random package is used for generating random moves of the computer in this game.
CPUchoice = random.randint(0,2)
The varname package is an interesting one, I found this package by googling a little. I primarily use the nameof() method to output the test variable values, for specifically debugging purposes.
print(nameof(USERwins),":\t",USERwins," ",nameof(USERties),":\t",USERties," ",nameof(USERlosses),":\t",USERlosses)
Initialization
Next in the program I proceed to initialize the global (variable in the complete scope of the program) control variables.
USERwins = 0
USERties = 0
USERlosses = 0
gamelabels = ["Rock","Paper","Scissor"]
These variables keep tracking the game play as you will notice through the rest of the program, for instance the number wins-losses throughout the game.
Main Program Loop
Now to keep the program looping I declare another global which keeps track of whether or not the user wants to continue playing.
optioncontinue = True
Initially it is set to True, this is to ensure a first pass through the program. Moving on we encompass the main program code in a while loop which is conditional on the optioncontinue global variable.
In the main program loop we begin by printing the current status of the game, the user win-loss scoreboard. The we use the random library import to generate a CPU choice at random. In this version of the program we are displaying the values of the variables for debugging purposes. Then, I proceed to ask the user for their choice from which the current standing in the game, the win-loss scoreboard.
Finally after the calculations of the win-loss I proceed to ask the user to continue or not.
while(optioncontinue):
clear()
print(nameof(optioncontinue),":",optioncontinue,sep="\t")
print("System:\t",platform.system())
print(nameof(USERwins),":\t",USERwins," ",nameof(USERties),":\t",USERties," ",nameof(USERlosses),":\t",USERlosses)
CPUchoice = random.randint(0,2)
print(nameof(CPUchoice),":\t",CPUchoice)
print(nameof(CPUchoice),":\t",gamelabels[CPUchoice])
print("Enter your choice (0: Rock, 1: Paper, 2: Scissor):")
USERchoice = int(input())
#print(nameof(USERchoice),":\t",gamelabels[USERchoice])
print(nameof(USERchoice),":\t",USERchoice)
print(nameof(USERchoice),":\t",gamelabels[USERchoice])
if CPUchoice == USERchoice:
USERties = USERties + 1
if CPUchoice == 0 and USERchoice == 1:
USERlosses = USERlosses + 1
if CPUchoice == 0 and USERchoice == 2:
USERlosses = USERlosses + 1
if CPUchoice == 1 and USERchoice == 0:
USERwins = USERwins + 1
if CPUchoice == 1 and USERchoice == 2:
USERwins = USERwins + 1
if CPUchoice == 2 and USERchoice == 0:
USERwins = USERwins + 1
if CPUchoice == 2 and USERchoice == 1:
USERlosses = USERlosses + 1
print(nameof(USERwins), ":\t", USERwins, " ", nameof(USERties), ":\t", USERties, " ", nameof(USERlosses), ":\t",
USERlosses)
print("Would you like to continue (Type yes), press enter if not : ")
optioncontinue = bool(input())
Final thoughts
Whilst writing this program it was interesting to learn the functionality of the random library. For the future I believe I can re-write this program to use functions purely for example printscoreboard(), etc. For now I will keep this as future work and experiment further with functions.