-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytexe (19).py
129 lines (110 loc) · 3.9 KB
/
pytexe (19).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
import random
import os
clear = lambda: os.system('cls')
def main():
player = 'X'
computer = 'O'
print ("User has", player, "and will go first...")
while not is_draw or not has_player_won:
get_user_move(game_board, player)
display_board(game_board)
print_line()
get_computer_move(game_board, computer)
display_board(game_board)
print_line()
is_draw(game_board)
print('Thanks for playing!')
game_board = [
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
]
def display_board(game_board):
print (str(game_board[0][0]) + '|' + str(game_board[0][1]) + '|' + str(game_board[0][2]))
print ('-+-+-')
print (str(game_board[1][0]) + '|' + str(game_board[1][1]) + '|' + str(game_board[1][2]))
print ('-+-+-')
print (str(game_board[2][0]) + '|' + str(game_board[2][1]) + '|' + str(game_board[2][2]))
print ('')
def print_line():
print('=' * 20)
def get_user_move(game_board, player):
print ("It's your turn...")
row = int(input(('What row do you want to play (0-2)')))
column = int(input(('What column do you want to play (0-2)')))
w = is_legal_move(game_board, row, column)
while w ==1:
row = int(input(('What row do you want to play (0-2)')))
column = int(input(('What column do you want to play (0-2)')))
w = is_legal_move(game_board, row, column)
game_board[row][column] = player
def get_computer_move(game_board, computer):
print ("Computer's turn...")
row = random.randint(0,2)
column = random.randint(0,2)
w = is_legal_move(game_board, row, column)
while w ==1:
row = random.randint(0,2)
column = random.randint(0,2)
w = is_legal_move(game_board, row, column)
game_board[row][column] = computer
def is_legal_move(game_board, row, column):
if row >=0 and row <= 2:
print ("")
else:
print ("That is an ivalid move, Please try again.")
return 1
if column >=0 and column <= 2:
print ("")
else:
print ("That is an ivalid move, Please try again.")
return 1
if game_board[row][column] == 'X' or game_board[row][column] == 'O':
print ("That is an ivalid move, Please try again.")
return 1
def is_draw(game_board):
#Determine if a game is a draw by checking each space. Once we find at
#least one empty spot, we can return False since it's a playable spot."""
draw = True
for i in range(3):
for j in range(3):
if game_board[i][j] == ' ':
draw = False
if draw:
print ("the game is a draw")
def has_player_won(game_board, symbol):
"""Check to see if the given symbol has won the game in any of the possible
ways."""
# Remember, with sequences the multiplication
# operator repeats the value so 'X' * 3 == 'XXX'
winner_sequence = player or computer * 3
# Check for horizontal wins
for r in range(len(game_board)):
row_symbols = ''
for c in range(len(game_board[row])):
row_symbols += game_board[row][column]
if row_symbols == winner_sequence:
return True
# Check for vertical wins
for c in range(len(game_board[0])):
col_symbols = ''
for r in range(len(game_board)):
col_symbols += game_board[row][column]
if col_symbols == winner_sequence:
return True
# Check for the two diagonal wins
# Note this will only work on a square board!
diag_symbols = ''
anti_diag_symbols = ''
for rc in range(len(game_board)):
diag_symbols += game_board[rc][rc]
anti_diag_symbols += game_board[rc][len(game_board) - 1 - rc]
if winner_sequence in (diag_symbols, anti_diag_symbols):
return True
# If we got here, nobody won yet
return False
while True:
main()
if input("Play again (y/n)\n") != "y":
break
main()