-
Notifications
You must be signed in to change notification settings - Fork 4
/
project.py
151 lines (124 loc) · 4.49 KB
/
project.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import random
import art
def main():
while True:
print(art.text2art('GAMES!'))
game = getGame()
if game == 1 :
TicTacToe()
elif game == 2 :
MagicBall()
elif game == 3 :
RockPaperScissors()
else:
continue
request = input('Do you want to carry on playing y/n? ').upper()
if request in ['NO','N']:
break
def getGame():
try :
n = int(input('Welcome to games :), If you want to exit press CTRL + C\nSelect a game :\n- 1 for TicTactoe\n- 2 for Magic 8 Ball\n- 3 for Rock Paper Scissors\nAnswer: '))
if n not in [1,2,3]:
raise Exception()
except (ValueError,Exception):
print('please Enter a Value 1 or 2')
else :
return n
def RockPaperScissors():
print('Welcome to Rock Paper scissors')
user = input("'r' for rock, 's' for scissors, 'p' for paper\n")
computer = random.choice(['r','p','s'])
if user == computer:
print('You tied!')
elif is_win(user, computer):
print('You won!')
else:
print('You lost!')
def is_win(player, player2):
if (player == 'r' and player2 == 's') or (player == 's' and player2 == 'p') or (player == 'p' and player2 == 'r'):
return True
def MagicBall():
answerNumber = random.randint(1, 9)
if answerNumber == 1:
print('Magic 8 Ball says : It is certain')
elif answerNumber == 2:
print('Magic 8 Ball says : It is decidedly so')
elif answerNumber == 3:
print('Magic 8 Ball says : Yes')
elif answerNumber == 4:
print('Magic 8 Ball says : Reply hazy try again')
elif answerNumber == 5:
print('Magic 8 Ball says : Ask again later')
elif answerNumber == 6:
print('Magic 8 Ball says : Concentrate and ask again')
elif answerNumber == 7:
print('Magic 8 Ball says : My reply is no')
elif answerNumber == 8:
print('Magic 8 Ball says : Outlook not so good')
elif answerNumber == 9:
print('Magic 8 Ball says : Very doubtful')
def TicTacToe():
print('Tic Tac Toe started!')
board = DefineBoard()
currentPlayer, nextPlayer = 'X', 'O'
while True:
try:
print(DisplayBoard(board))
position = input(f'{currentPlayer} Role to play? ')
if CheckSpot(board, position) == True :
UpdateBoard(board, currentPlayer, position)
if Winner(board, currentPlayer) == True :
print(DisplayBoard(board))
print(f"{currentPlayer} has won the game.")
break
if checkFullBoard(board) == True:
print(DisplayBoard(board))
print('Game Over!')
break
currentPlayer, nextPlayer = nextPlayer, currentPlayer
else :
continue
except (ValueError,KeyError):
print("Enter a Value between 1 and 9.")
def DefineBoard():
board = {1: ' ', 2: ' ', 3: ' ',
4: ' ', 5: ' ', 6: ' ',
7: ' ', 8: ' ', 9: ' '}
return board
def UpdateBoard(board, player, position):
board[int(position)] = player
def CheckSpot(board,position):
if board[int(position)] == ' ':
return True
else:
return False
def Winner(board,player):
if board[1] == board[2] == board[3] == player:
return True
elif board[4] == board[5] == board[6] == player:
return True
elif board[7] == board[8] == board[9] == player:
return True
elif board[1] == board[4] == board[7] == player:
return True
elif board[2] == board[5] == board[8] == player:
return True
elif board[3] == board[6] == board[9] == player:
return True
elif board[1] == board[5] == board[9] == player:
return True
elif board[3] == board[5] == board[7] == player:
return True
else :
return False
def checkFullBoard(board):
for value in board.values():
if value == ' ':
return False
else :
continue
return True
def DisplayBoard(board):
return f'{board[1]}|{board[2]}|{board[3]} 1 2 3\n-----\n{board[4]}|{board[5]}|{board[6]} 4 5 6\n-----\n{board[7]}|{board[8]}|{board[9]} 7 8 9'
if __name__ == "__main__" :
main()