Python Projects : Rock Paper Scissors Game v2 of 3

Hello World,
 Today I give you Version 2 of my Rock Paper Scissors game (RPS).
In this version, I was able to add a scoring mechanism, the game is set to the best out of 3 plays, and I tell you, it took me a couple of days to work out some kinks


The First Kink: How to score it
In theory, it seemed simple to me, the game is set to 3 plays, the best 2 out of the 3 wins, cool.
How do I do that exactly?
Well, my first attempt was a disaster, and I feel was a bit too complicated, I worked on it for half a day and I had to scrap it, because it was actually causing the program to malfunction.
I'll tell you both attempts, so that we would both learn from my mistake.

The Failed Attempt
Since I had 3 separate functions for rock() , paper(), scissors().
Each one was going to produce a winner , loser, or a tie.
So I went into each of these functions and created user_point & computer_point variables, which that function would return at the end of its round.
Then the scoring function would take in those 6 variables (2 from each function), do the calculation to determine the winner, user or computer
Well, the SNAFU I ran into is this, since each function was returning 2 variables, when I run the game, it would run through the round twice..so instead of being asked 3 times to choose R/P/S, you were being asked 6 times. So basically it ran 2 rounds..yet it was calculation only the 1st round..so it was..half working. This frustrated the crap out of me, and I kept thinking to myself "there has got to be an easier and more efficient way to do this", so on a hunch I tried something else, and that ended up being my Successful Attempt.


The Successful Attempt
Due to me liking to be organized, I had also created a win() / lose() / tie() functions.
So I though, since those functions are called in all 3 rock() / paper() / scissor() functions, why not use them to help with the scoring, and it worked.
I knew I needed the win / lose functions to return a variable, and I knew I needed each to have just ONE return, so that I don't run into the issue I did during my "Failed Attempt".
So I had to shift my thinking a bit...I can't think of both (computer and user) as winner and loser, I had to focus on only one. So I decided that I'll be on Team Human.
Now, this means that the win / lose is directed for the User, so that means when win() is called, it would increments the user_point variable, and return that value
There for when the user loses it calls the lose(), that would increments the computer_point and return its value.
I then made user_point & computer_point global variables, which made life so much easier, because now I can pass them down freely as I needed.


The Second Kink: the leak...un-intended execution
I was looking up "how" to pass variables, and came across a video that showed a very simple way
so here is what I did.
win() returned the global variable value of user_point
lose() returned the global variable value of computer_point


So in the score() I assigned things as follows:
user_score = win()
computer_score = lose()



That allowed me to do the calculations in of the score using user_score & computer_score
but I was running into a problem, at the end of the game, after the 3 plays are done, it would give you the result, then it would give you "You win" / "You lose" and then do the calculation and give you the final Result.
The calculations are correct, each round plays 3 times as it should, and at the end it asks you if you want to play again. It was all correct, expect for what I called "The Leak" of "You win" / "You lose" after the final play.
It took me a little bit to realize that while assigning user_score & computer_score to win() and lose(), it was also executing them, since its the first thing that comes up when score() is called.

So I took out that assignment, and instead passed down the global variables.
So now the score function looks like this.
score(user_point, computer_point)
 And now I do the calculations using those passed down global variables, and that fixed it!
Woho.



The Status of the Game Now
As of this point, the game works as intended.
Each round is the best out of 3.
The scoring works.
And it even tells you things like "Paper over Rock" and "The Machine Wins!".
And at the end of each round, you can choose to play again if you like.


Future Status : version 3 of 3
I want to "fool proof" it a bit.
I want the user to be able to type in their name.
I also want the user to be able to choose how many plays per round they want, instead of the default 3.


This was a total learning experience for me, and I'm super excited I got my first working Python game. I hope you enjoy it. Again you can download it from my Python Projects GitHub repo.

Enjoy the rest of your day.


Remember.
"Coding is the closest thing we have to a superpower" - anonymous
Go be a Super.
-ash

Comments

Popular posts from this blog

No more controllers for playing games

Look out iPhone, the G1 is coming. sw33t.