-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
83 lines (62 loc) · 2.47 KB
/
Copy pathgame.py
File metadata and controls
83 lines (62 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# author: Tim Eichinger
# version: 1.0
import random
# The 3 options to choose in the game
options = ["SCISSOR", "STONE", "PAPER"]
# Intro
def printintro():
print("\n********************************************")
print("** WELCOME TO SCISSORS-STONE-PAPER **")
print("********************************************\n")
# Starts the game and checks whether the user entered a valid move-number.
# If that is okay, the method checkwinner() is called.
def getusername():
while True:
username = input("* Please enter your name: ")
if username != "":
break
print(f"* Your username is: {username}")
print("\n* Let's start the game! {0:SCISSOR, 1:STONE, 2:PAPER}")
return username
# starts the game by getting the move from the player.
# And then calls the method checkwinner() for getting the result.
def startgame(username):
while True:
usermove = input(f"\n* {username}, please enter your move-number: ")
if usermove == "0" or usermove == "1" or usermove == "2":
break
aimovenum = random.randint(0, 3)
usermovenum = int(usermove)
checkwinner(usermove, usermovenum, aimovenum)
checkforplayagain(username)
# Checks whether the AI or the User has won the game.
def checkwinner(usermove, usermovenum, aimovenum):
print("\n------------------------------------------")
if str(aimovenum) == usermove:
print("* It's a draw!")
printgameresult(usermovenum, aimovenum)
elif (usermovenum == 0 and aimovenum == 1) or (usermovenum == 1 and aimovenum == 2) or (usermovenum == 2 and aimovenum == 0):
print("* AI won!")
printgameresult(usermovenum, aimovenum)
else:
print("* You won!")
printgameresult(usermovenum, aimovenum)
print("-----------------------------------------\n")
# prints the result of the game. Especially which move the players have chosen.
def printgameresult(usermovenum, aimovenum):
print(f"* You chose {options[usermovenum]} and the AI chose {options[aimovenum]}")
# checks if the user wants to play again
def checkforplayagain(username):
playagain = input("* Do you want to play again? ([Y]Yes , [N]No): ")
if playagain == "Y":
startgame(username)
elif playagain == "N":
endgame()
else:
checkforplayagain(username)
# prints a text when the game ended.
def endgame():
print("* Thanks for playing with me :)")
if __name__ == '__main__':
printintro()
startgame(getusername())