Rock, Paper, Scissors, Python
Hello World.
I wanted to tackle the logic of how to create a Rock Paper Scissors (RPS) game using the knowledge that I know so far. I think this particular project has proved to me, once a gain, why I love programming. I wrote out the logic in two different ways to make it work, and then I found a videos online of Kylie Ying who also created an RPS game, but in a very different way, here is one of her videos on it.
Needless to say, you may actually have a more efficient way of making this happen.
The full code for this projects is in my GitHub Page. [ RPS Game ]
It even includes an executable for you to use without needed to compile the code if you like.
What Do I Need The Program To Do.
1. For the user to play against the computer (GameBot).
2. To count the 3 rounds.
3. To declare a winner or tie at the end of the 3 rounds
4. Ask to play again.
Instead of going through the WHOLE program, I’m just going to give you the core of it. You can of course check out the full code in my GitHub page.
I decided to use numbers [1 2 3] to represent [Rock Paper Scissors]. I found this to be easier for me. Then I of course had to sit with each section to determine what would be a winning combination, since I’ve decided that GameBot will be player one (u1) and the human is player two(u2), I worked with the logic that u1 making the random choice first, so as soon as the game starts u1 makes a choice between 1 and 3 and then u2 is asked to make a choice.
# — UI: Making the choice.
u1choice = random.randint(1,3) #Computer will randomly choose a number.
print(“Gamebot has chosen”)
u2choice = int(input(“User 2 Choose: 1, 2, or 3: “))
Now thinking about what we have chosen, literally.
There are 3 possible outcomes per choice, with only one winning choice in each. The best way for me to work it out, was to take it per choice, that seemed to make it easier to understand and to much more cleared when coding it.
The known rule is: Rock > Scissor > Paper > Rock.
Cool. So I tackled each one individually.
With the knowledge that u1 will make the first choice, I did the following.
In the “Rock” section
If u1 chooses Rock, and u2 chooses Scissor = GameBot Wins.
If u1 chooses Rock, and u2 Chooses Paper = Human Wins.
If both choose Rock = Tie, no points given
I then basically followed that for each option with the idea of u1 making that choice first in each section “Rock , Paper, & Scissors”, that’s the algorithm you see below.
Also instead of doing the “Tie” in each section, I simply added that in the end to apply to that whole section, that way I didn’t have to coded in every time. And of course after each round, I would add ‘1’ to each round, to make sure we make it to ‘3’ rounds and then stopping.
# — ROCK
if u1choice == 1 and u2choice == 3:
u1points += 1
print(“Rock beats Scissor — Point for Gamebot”)
time.sleep(2)
if u2choice == 1 and u1choice == 3:
u2points +=1
print(“Rock beats Scissor — Point for You”)
time.sleep(2)
# — PAPER
if u1choice == 2 and u2choice == 1:
u1points += 1
print(“Paper beats Rock — Point for Gamebot”)
time.sleep(2)
if u2choice == 2 and u1choice == 1:
u2points += 1
print(“Paper beats Rock — Point for You”)
time.sleep(2)
# — SCISSORS
if u1choice == 3 and u2choice == 2:
u1points += 1
print(“Scissor beats Paper — Point for Gamebot”)
time.sleep(2)
if u2choice == 3 and u1choice == 2:
u2points += 1
print(“Scissor beats Paper — Point for You”)
time.sleep(2)
# — A TIE
if u1choice == u2choice:
print(“Its a tie, No points”)
time.sleep(2)
# — The Next round/turn begins
rounds +=1
Declaring the Winner
print("\n\nThe Final Score"
"\n----------------")
if u1points > u2points:
print("Gamebot wins with [ " + str(u1points) + " ] out of 3 points.")
elif u1points == u2points:
print("No One Wins this time.")
else:
print("You win with [ " + str(u2points) + " ] out of 3 points.")
You Are Done
At this point, the virtual world is your digital oyster. I chose to display the rounds on the screen, and I, of course, added a “Would you like to play another game?” option at the end in case the user (or you) wanted to continue playing.
I hope this post has given you some ideas.
This has been ryn0f1sh, The PyNoob.
[R/F]
End Of Line.