-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
171 lines (140 loc) · 5.72 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from config import * # for default values for our program
import random # for making the toss
import time # for sleep() function
class tic_tac_toe:
def __init__(self):
'''
Initializing the Toss,choosing winner,assigning symbol and turn
Variables Assigned Here: symbol(list) , turn(list)
'''
print('Welcome Champions\n')
time.sleep(2)
print('''Here are the instructions about the game:
1. Toss will be entirely based on random value generated by the computer
2. Toss Winner will get first turn with choice to choose between 'O' and 'X'
3. Position of move would be like - (row_number)(column_number) eg- 13 (1st row and 3rd column)
''')
time.sleep(7)
print("Let's do the toss!!!")
winner = random.randint(0,1) # Toss
time.sleep(2)
if winner == 0: # when player A wins
print('Player A won the Toss')
else: # when player B wins
print('Player B won the Toss')
symbol[winner] = input('Please choose your symbol(O or X): ')
# assigning turn to the winner
if winner == 0:
turn[0] = True
symbol[1] = ''.join([sy for sy in ['O','X'] if sy != symbol[winner] ])
else:
symbol[0] = ''.join([sy for sy in ['O','X'] if sy != symbol[winner] ])
turn[1] = True
def doing_turn(self):
'''
1. If user chooses an invalid box id, asking him to do again
2. Updating the move of the Player in the list
3. Reversing the turns of the Players
'''
# informing Players about the turn
print("\nPlayer A's Turn") if turn[0] else print("\nPlayer B's Turn")
box_id = int(input('Enter the box id: '))
# checking if player entered a reserved index
if l[int(box_id/10 - 1)][box_id%10 - 1] :
print('Try Again')
self.doing_turn()
else: # if not
l[int(box_id/10 - 1)][box_id%10 - 1] = symbol[0] if(turn[0]) else symbol[1]
#reverting the turns
turn[0] = not turn[0]
turn[1] = not turn[1]
def printing(self):
'''
Printing the tic tac toe board
'''
print('\n' + '▒'*19)
for horizontal_position in range(nor):
print('▒', end='')
for vertical_position in range(noc):
if l[horizontal_position][vertical_position] :
print(str(l[horizontal_position][vertical_position]).center(5),end='')
else:
print(' '*5,end='')
if vertical_position != noc-1 :
print('|',end='')
print('▒')
if horizontal_position != nor-1 :
print( '▒' + (noc*5 + 2)*'-' + '▒')
print('▒'*19,'\n')
def return_nor(self):
# returning number of rows
return nor
def return_noc(self):
# returning number of columns
return noc
def winner_check(self):
'''
Use : Checking whether any player won the game and returning the Symbol of the winner
Variables Used: 1. d_1 and d_2 representing diagonal element
2. d1_won and d2_won representing whether diagonal contains same element
3. flag_r and flag_c representing whether a row or column contains same element
'''
'''
Checking if any player won Diagonally
'''
d_1 = l[0][0]
d_2 = l[noc-1][0]
d1_won, d2_won = True, True
# diagonal-> top-left to bottom-right
for horizontal_position in range(nor):
if l[horizontal_position][horizontal_position] != d_1:
d1_won = False
break
if d1_won:
return d_1
# diagonal-> top-right to botton-left
horizontal_position, vertical_position = 0, noc-1
while vertical_position >= 0:
if l[horizontal_position][vertical_position] != d_2 :
d2_won = False
break
horizontal_position += 1
vertical_position -= 1
if d2_won :
return d_2
'''
1. If player didn't won diagonally
2. Checking if any Player won row-wise or column-wise
'''
for horizontal_position in range(nor):
r, c = l[horizontal_position][0], l[0][horizontal_position]
flag_r, flag_c = True, True
for vertical_position in range(noc):
if(l[horizontal_position][vertical_position] != r and flag_r == True):
flag_r = False
if(l[vertical_position][horizontal_position] != c and flag_c == True):
flag_c = False
# returning the symbol of the winning Player
if flag_r :
return l[horizontal_position][vertical_position]
elif flag_c :
return l[vertical_position][horizontal_position]
return None # if there is no win
def result_calculator(result, n):
'''
Checking whether it's draw(returns D) or a winner(returns winner code( A or B))
'''
if(not result and n != nor*noc-1):
return None
elif(not result and n == nor*noc-1):
return 'D'
elif result == symbol[0] :
return 'A'
elif result == symbol[1] :
return 'B'
def winner_display(result):
print('''\nWE GOT OUR WINNER :\n
╔════════════════════╗
║ PLAYER ''' + result + ''' WINS ║
╚════════════════════╝
''')