-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
152 lines (120 loc) · 4.08 KB
/
test.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
152
import random
player_letter = ""
computer_letter = ""
def print_welcome_msg():
print("Welcome to Tic Tac Toe!")
print("This is the first game that made by Peler and Jessie!")
print("ENJOY!")
def get_player_letter():
global player_letter
global computer_letter
letter = input("Do you want to use X or O? ").upper()
while not (letter == "X" or letter == "O"):
print("please enter X or O")
letter = input("Do you want to use X or O? ").upper()
if letter == "X":
player_letter = "X"
computer_letter = "O"
else:
player_letter = "O"
computer_letter = "X"
def print_board(board):
print(f" {board[7]} | {board[8]} | {board[9]}")
print("-----------")
print(f" {board[4]} | {board[5]} | {board[6]}")
print("-----------")
print(f" {board[1]} | {board[2]} | {board[3]}")
def choose_who_go_first():
"""
the function will decide whether to player go first or computer go first
:return: True if player go first, False if computer go first
"""
return random.choice([True, False])
#
def get_player_move(board):
"""
get player move
:return: the index of the position which player wants to move
"""
move = input("What's your next move? ")
while move not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"] and (not is_empty(board, int(move))):
print("please enter a valid move")
move = input("What's your next move? ")
return int(move)
def make_move(board, position, letter):
board[position] = letter
def is_empty(board, position):
return board[position] == " "
def is_win(board, letter):
return (board[7] == board[8] == board[9] == letter) or (board[4] == board[5] == board[6] == letter) or (
board[1] == board[2] == board[3] == letter) or (board[7] == board[4] == board[1] == letter) or (
board[9] == board[6] == board[3] == letter) or (board[8] == board[5] == board[2] == letter) or (
board[7] == board[5] == board[3] == letter) or (board[9] == board[5] == board[1] == letter)
def get_board_copy(board):
return board[:]
def get_computer_move(board):
for i in range(1, 10):
if is_empty(board, i):
copy_board = get_board_copy(board)
make_move(copy_board, i, computer_letter)
if is_win(copy_board, computer_letter):
return i
for i in range(1, 10):
if is_empty(board, i):
copy_board = get_board_copy(board)
make_move(copy_board, i, player_letter)
if is_win(copy_board, player_letter):
return i
if is_empty(board, 5):
return 5
empty_list = []
for i in [2, 4, 6, 8]:
if is_empty(board, i):
empty_list.append(i)
if empty_list:
print(empty_list)
return random.choice(empty_list)
empty_list = []
for i in [1, 3, 7, 9]:
if is_empty(board, i):
empty_list.append(i)
if empty_list:
return random.choice(empty_list)
def is_game_board_full(board):
for i in range(1, 10):
if board[i] == " ":
return False
return True
def is_game_over():
if is_win(board, player_letter):
print("you win!")
return True
elif is_win(board, computer_letter):
print("you lost!")
return True
elif is_game_board_full(board):
print("the game board is full!")
return True
return False
if __name__ == '__main__':
board = [' '] * 10
print_welcome_msg()
get_player_letter()
is_player_turn = choose_who_go_first()
while True:
if is_player_turn:
print("Player's turn")
print_board(board)
move = get_player_move(board)
make_move(board, move, player_letter)
if is_game_over():
break
is_player_turn = False
else:
move = get_computer_move(board)
if move:
make_move(board, move, computer_letter)
print_board(board)
if is_game_over():
break
is_player_turn = True