-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
106 lines (98 loc) · 3.45 KB
/
tic_tac_toe.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
board = {
'T1': ' ', 'T2': ' ', 'T3': ' ',
'M1': ' ', 'M2': ' ', 'M3': ' ',
'D1': ' ', 'D2': ' ', 'D3': ' '
}
player = 1 # to initialise first player
total_moves = 0 # to count the moves
end_check = 0
def check():
# checking the moves of player one
# for horizontal(start)
if board['T1'] == 'X' and board['T2'] == 'X' and board['T3'] == 'X':
print('Player one won !')
return 1
if board['M1'] == 'X' and board['M2'] == 'X' and board['M3'] == 'X':
print('Player One Won!!')
return 1
if board['D1'] == 'X' and board['D2'] == 'X' and board['D3'] == 'X':
print('Player One Won!!')
return 1
# for horizontal(end)
# for diagonal(start)
if board['T1'] == 'X' and board['M2'] == 'X' and board['D3'] == 'X':
print('Player One Won!!')
return 1
# for diagonal(end)
# for vertical(start)
if board['T1'] == 'X' and board['M1'] == 'X' and board['D1'] == 'X':
print('Player One Won!!')
return 1
if board['T2'] == 'X' and board['M2'] == 'X' and board['D2'] == 'X':
print('Player One Won!!')
return 1
if board['T3'] == 'X' and board['M3'] == 'X' and board['D3'] == 'X':
print('Player One Won!!')
return 1
# for vertical(end)
# checking the moves of player two
if board['T1'] == 'O' and board['T2'] == 'O' and board['T3'] == 'O':
print('Player Two Won!!')
return 1 # used to end the game
if board['M1'] == 'O' and board['M2'] == 'O' and board['M3'] == 'O':
print('Player Two Won!!')
return 1
if board['D1'] == 'O' and board['D2'] == 'O' and board['D3'] == 'O':
print('Player Two Won!!')
return 1
if board['T1'] == 'O' and board['M2'] == 'O' and board['D3'] == 'O':
print('Player Two Won!!')
return 1
if board['T1'] == 'O' and board['M1'] == 'O' and board['D1'] == 'O':
print('Player Two Won!!')
return 1
if board['T2'] == 'O' and board['M2'] == 'O' and board['D2'] == 'O':
print('Player Two Won!!')
return 1
if board['T3'] == 'O' and board['M3'] == 'O' and board['D3'] == 'O':
print('Player Two Won!!')
return 1
return 0
print('T1|T2|T3')
print('- +- +-')
print('M1|M2|M3')
print('- +- +-')
print('D1|D2|D3')
print('***************************')
while True:
print(board['T1']+'|'+board['T2']+'|'+board['T3'])
print('-+-+-')
print(board['M1'] + '|' + board['M2'] + '|' + board['M3'])
print('-+-+-')
print(board['D1'] + '|' + board['D2'] + '|' + board['D3'])
end_check = check()
if total_moves == 9 or end_check == 1:
break
while True: # input from players
if player == 1: # choose player
p1_input = input('player one')
if p1_input.upper() in board and board[p1_input.upper()] == ' ':
board[p1_input.upper()] = 'X'
player = 2
break
# on wrong input
else:
print('Invalid input, please try again')
continue
else:
p2_input = input('player two')
if p2_input.upper() in board and board[p2_input.upper()] == ' ':
board[p2_input.upper()] = 'O'
player = 1
break
else: # on wrong input
print('Invalid input, please try again')
continue
total_moves += 1
print('***************************')
print()