-
Notifications
You must be signed in to change notification settings - Fork 0
/
T3.py
executable file
·149 lines (127 loc) · 3.98 KB
/
T3.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
import random #time
def userTurn(board):
done = True
while done:
row,column = 0,0
tre = (1,2,3)
while row == 0:
row = input('Top (1), middle (2), or bottom (3)? ')
row = int(row)
if row not in tre:
row = 0
while column == 0:
column = input('Left (1), center (2), or right (3)? ')
column = int(column)
if column not in tre:
column = 0
if board[row-1][column-1] == 0:
board[row-1][column-1] = 'X'
done = False
else:
print('Space is already taken. Pick again.')
def compTurn(board):
done = True
while done:
row = random.randint(0, 2)
column = random.randint(0, 2)
if board[row][column] == 0:
board[row][column] = 'O'
done = False
def checkWinU(board):
if board[0] == ['X', 'X', 'X']:
return 'User'
elif board[1] == ['X', 'X', 'X']:
return 'User'
elif board[2] == ['X', 'X', 'X']:
return 'User'
elif board[0][0] == str('X') and board[1][0] == str('X') and board[2][0] == str('X'):
return 'User'
elif board[0][1] == str('X') and board[1][1] == str('X') and board[2][1] == str('X'):
return 'User'
elif board[0][2] == str('X') and board[1][2] == str('X') and board[2][2] == str('X'):
return 'User'
elif board[0][0] == str('X') and board[1][1] == str('X') and board[2][2] == str('X'):
return 'User'
elif board[2][0] == str('X') and board[1][1] == str('X') and board[0][2] == str('X'):
return 'User'
else:
return None
def checkWinC(board):
if board[0] == ['O', 'O', 'O']:
return 'Computer'
elif board[1] == ['O', 'O', 'O']:
return 'Computer'
elif board[2] == ['O', 'O', 'O']:
return 'Computer'
elif board[0][0] == str('O') and board[1][0] == str('O') and board[2][0] == str('O'):
return 'Computer'
elif board[0][1] == str('O') and board[1][1] == str('O') and board[2][1] == str('O'):
return 'Computer'
elif board[0][2] == str('O') and board[1][2] == str('O') and board[2][2] == str('O'):
return 'Computer'
elif board[0][0] == str('O') and board[1][1] == str('O') and board[2][2] == str('O'):
return 'Computer'
elif board[2][0] == str('O') and board[1][1] == str('O') and board[0][2] == str('O'):
return 'Computer'
else:
return None
def main():
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
winner = None
turn = 0
print()
print('New Game')
print()
print(board[0])
print(board[1])
print(board[2])
print()
print('User is X, computer is O')
while winner == None and turn <10:
print("""
User's turn
""")
turn +=1
userTurn(board)
print()
print(board[0])
print(board[1])
print(board[2])
winner = checkWinU(board)
if winner != None or turn>=9:
break
#time.sleep(5)
print("""
Computer's Turn
""")
turn +=1
#time.sleep(5)
compTurn(board)
print(board[0])
print(board[1])
print(board[2])
winner = checkWinC(board)
print()
if winner == None:
print("It's a tie. Try again.")
elif winner == 'User':
print('You Win!')
#playsound('win.mp3')
else:
print('The computer won. Better luck next time.')
#playsound('lose.mp3')
play = True
while play:
ans = input('Do you want to play again? (y/n) :')
if ans == str('y'):
print("Let's play again")
return True
elif ans == str('n'):
play = False
print('Thanks for playing!')
return False
else:
print('Input error: Please type y or n for your answer.')
playing = True
while playing:
playing = main()