-
Notifications
You must be signed in to change notification settings - Fork 0
/
xand0.py
180 lines (149 loc) · 5.06 KB
/
xand0.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
171
172
173
174
175
176
177
178
179
180
import random
def winner(player):
#DECLARING WINNERS
for k,v in player_data.items():
if v == player:
print(f'[{k}] has won the match')
xand0(True,True)
def void_places():
#TO CHECK VOID PLACES IN THE BOARD
for spaces in range(len(board)):
if board[spaces] == "-":
remaining_places.append(spaces+1)
return remaining_places
def main(choice,board):
'''operates each an every function of
games either it may be for checking the winner
or playing playerx or playery.
This is kind of gateway to pass through the set of rules of games to played'''
print(board[0],"|",board[1],"|",board[2])
print("--|---|--")
print(board[3],"|",board[4],"|",board[5])
print("--|---|--")
print(board[6],"|",board[7],"|",board[8])
print('\n')
if choice=="x":
if win(board,"0"):
playerx(board)
if choice=="0":
if win(board,"x"):
player0(board)
else:
print("invalid choice")
def win(board,choice):
#CHECKING ALL POSSIBILITES OF WINNERS
if (board[0] == choice and board[1] == choice and board[2] == choice):
winner(choice)
return False
if (board[3] == choice and board[4] == choice and board[5] == choice):
winner(choice)
return False
if (board[6] == choice and board[7] == choice and board[8] == choice):
winner(choice)
return False
if (board[0] == choice and board[4] == choice and board[8] == choice):
winner(choice)
return False
if (board[0] == choice and board[3] == choice and board[6] == choice):
winner(choice)
return False
if (board[1] == choice and board[4] == choice and board[8] == choice):
winner(choice)
return False
if (board[2] == choice and board[5] == choice and board[8] == choice):
winner(choice)
return False
if (board[2] == choice and board[4] == choice and board[6] == choice):
winner(choice)
return False
if board.count('-') <=0 :
#CHECKS FOR TIE
print("Tie!")
xand0(True , True)
else:
return True
def playerx(board):
#PLAYING X
try:
print("enter the position for x:")
position=int(input())
position=position-1
print(f'{player_1} plays {position+1}')
if board[position]=="0" or board[position]=="x" :
print("[PLACE ALREADY OCCUPIED]")
choice="x"
main(choice,board)
board[position]='x'
choice="0"
main(choice,board)
except:
print('ENTER A VALID NUMBER BETWEEN 1-9')
playerx(board)
def second_player_move(position):
if board[position]=="x" or board[position]=='0':
print("[PLACE ALREADY OCCUPIED]")
choice="0"
main(choice,board)
board[position]='0'
choice="x"
main(choice,board)
def player0(board):
#PLAYING 0
if player_2 == 'Computer':
void_places()
position = int(random.choice(remaining_places))
position = position -1
print(f'Computer playes {position +1}')
remaining_places.clear() #REMOVES ALL ITEMS FROM LIST SO THAT REDUNDENCY WILL NOT BE THERE
second_player_move(position)
else:
try:
print("enter position for 0:")
position=int(input())
position=position-1
print(f'{player_2} plays {position+1} ')
second_player_move(position)
except:
print('ENTER A VALID NUUMBER BETWEEN 1-9')
player0(board)
def xand0(game,loop):
#MAIN GAME LOOP
global player_1
global player_2
global board
while game:
board=["-","-","-",
"-","-","-",
"-","-","-",]
while loop:
play_again=input("press q to quit or c to play again:")
if play_again =="q" :
print('[THANK YOUR FOR PLAYING]')
quit()
xand0(True,False)
if play_again=="c":
xand0(True,False)
try:
no_of_players = int(input("[HOW MANY PLAYERS ARE PLAYING?]:"))
player_1 = input('[ENTER THE NAME OF FIRST PLAYER]:')
if (no_of_players) == 2:
player_2 = input('[ENTER THE NAME OF SECOND PLAYER]:')
elif no_of_players == 1 :
player_2 = 'Computer'
choice = 'x'
player_data[player_1] = 'x'
player_data[player_2] = '0'
for key , value in player_data.items():
print(f'{key} plays {value}')
print('\n')
main(choice,board)
except:
print("[WRONG INPUT]")
print("[CHOOSE NO OF PLAYERS IN NUMBERS EITHER 1 OR 2]")
xand0(True,False)
if __name__== "__main__":
remaining_places = []
player_data = dict()
game=True
loop=False
xand0(game,loop)