-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe_tie.py
85 lines (67 loc) · 2.74 KB
/
tic_tac_toe_tie.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
# Customizing board
""" Playing Tic Tac Toe with Computer """
import random
import moves
BOARD_VALUE = {}
for number in range(1, 10):
BOARD_VALUE[number] = " "
COMP_MOVES = BOARD_VALUE.keys()
COMP_MOVES = list(COMP_MOVES)
def computer_play(*turn):
""" Computer move on the board """
comp_val = moves.random_moves(BOARD_VALUE, COMP_MOVES, turn[1])
print(" Computer Move : ", comp_val)
BOARD_VALUE[comp_val] = turn[0]
del COMP_MOVES[COMP_MOVES.index(comp_val)]
def user_play(turn):
""" User move on the board """
user_val = int(input(" Make your Move : "))
BOARD_VALUE[user_val] = turn
del COMP_MOVES[COMP_MOVES.index(user_val)]
def print_board():
""" Printing the Board """
print(" _____ _____ _____")
print("| | | |")
print(f"|{BOARD_VALUE[1]}|{BOARD_VALUE[2]}|{BOARD_VALUE[3]}|")
print("| | | |")
print(" -----+-----+-----")
print("| | | |")
print(f"|{BOARD_VALUE[4]}|{BOARD_VALUE[5]}|{BOARD_VALUE[6]}|")
print("| | | |")
print(" -----+-----+-----")
print("| | | |")
print(f"|{BOARD_VALUE[7]}|{BOARD_VALUE[8]}|{BOARD_VALUE[9]}|")
print("|_____|_____|_____|")
def winner(turn):
""" Win the Game """
return ((BOARD_VALUE[1] == turn and BOARD_VALUE[2] == turn and BOARD_VALUE[3] == turn) or
(BOARD_VALUE[4] == turn and BOARD_VALUE[5] == turn and BOARD_VALUE[6] == turn) or
(BOARD_VALUE[7] == turn and BOARD_VALUE[8] == turn and BOARD_VALUE[9] == turn) or
(BOARD_VALUE[1] == turn and BOARD_VALUE[4] == turn and BOARD_VALUE[7] == turn) or
(BOARD_VALUE[2] == turn and BOARD_VALUE[5] == turn and BOARD_VALUE[8] == turn) or
(BOARD_VALUE[3] == turn and BOARD_VALUE[6] == turn and BOARD_VALUE[9] == turn) or
(BOARD_VALUE[1] == turn and BOARD_VALUE[5] == turn and BOARD_VALUE[9] == turn) or
(BOARD_VALUE[3] == turn and BOARD_VALUE[5] == turn and BOARD_VALUE[7] == turn))
def main():
""" Main function """
comp_turn = " o "
chance = random.randint(0, 1) # 0 for user and 1 for computer
try:
user_turn = " " + str(input(" 'x' or 'o' : ")) + " "
if user_turn == " o ":
comp_turn = " x "
for i in range(chance, chance + 9): # chance == 0 then we've 0-9
# and chance == 1 then we've 1-10
if i % 2 != 0:
user_play(user_turn)
win = winner(user_turn)
else:
computer_play(comp_turn, user_turn)
win = winner(comp_turn)
print_board()
if win:
print(" WINNNEEEERRRRR ")
break
except (IndexError, ValueError) as err:
print(err)
main()