-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
219 lines (205 loc) · 6.49 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import operators as op
import random
from datetime import datetime
#create the game board
game = [[0,0,0],[0,0,0],[0,0,0]]
#this function writes in board the mark
def writePosition(position, player):
if position < 3:
if int(game[0][position]) is 0:
game[0][position] = player
else:
print "Essa casa ja esta preenchida!"
getPlayerMark(player)
if position > 2 and position < 6:
if int(game[1][position - 3]) is 0:
game[1][position - 3] = player
else:
print "Essa casa ja esta preenchida!"
getPlayerMark(player)
if position > 5 and position < 9:
if int(game[2][position - 6]) is 0:
game[2][position - 6] = player
else:
print "Essa casa ja esta preenchida!"
getPlayerMark(player)
#this function get the position mark of the player
def getPlayerMark(player):
position = int(raw_input('Qual posicao escolhe? (0-8)'))
print "Voce escolheu a posicao %d." % position
writePosition(position, player)
print game[0]
print game[1]
print game[2]
def verifyWinner(player):
index = 0
for number in game[0]+game[1]+game[2]:
if number == 0:
index += 1
if index == 0:
print 'Fim do jogo! Ninguem ganhou!'
return True
#check lines
for line in [0,1,2]:
points = 0
for house in game[line]:
if int(house) == player:
points += 1
if points == 3:
print "Winner!"
return True
#check columns
for column in [0, 1, 2]:
points = 0
for line in [0,1,2]:
if int(game[line][column]) == player:
points += 1
if points == 3:
print "Winner!"
return True
#check diagonals
points = 0
for line in [0,1,2]:
if int(game[line][line]) == player:
points += 1
if points == 3:
print "Winner"
return True
points = 0
for line in [2,1,0]:
if int(game[line][2-line]) == player:
points += 1
if points == 3:
print "Winner"
return True
return False
def inteligence(player):
objects = []
#check lines, make a object for each line
for line in [0,1,2]:
blank = 0
other = 0
play = 0
for column in [0,1,2]:
field = int(game[line][column])
if field is 0:
blank += 1
elif field is player:
play += 1
else:
other += 1
objects.append(op.elements(other, blank, play))
#check columns, make a object for each column
for line in [0,1,2]:
blank = 0
other = 0
play = 0
for column in [0,1,2]:
field = int(game[column][line])
if field is 0:
blank += 1
elif field is player:
play += 1
else:
other += 1
objects.append(op.elements(other, blank, play))
#check diagonals, make a object for each diagonal
blank = 0
other = 0
play = 0
for line in [2,1,0]:
field = game[line][2-line]
if field is 0:
blank += 1
elif field is player:
play += 1
else:
other += 1
objects.append(op.elements(other, blank, play))
blank = 0
other = 0
play = 0
for line in [0,1,2]:
field = game[line][line]
if field is 0:
blank += 1
elif field is player:
play += 1
else:
other += 1
objects.append(op.elements(other, blank, play))
#check if there is a chance of the computer wins and complete sequence
for obj in objects:
if obj.iGoWins() is True:
position = objects.index(obj)
#mark lines
if position <= 2:
writePosition(game[position].index(0)+(position*3), player)
#mark columns
elif position > 2 and position < 6:
column = position - 3
for line in [0,1,2]:
if game[line][column] == 0:
writePosition(((line*3)+column), player)
#mark diagonals
elif position > 5:
if position == 6:
for line in [2,1,0]:
if game[line][2-line] == 0:
writePosition(((line*3)+(2-line)), player)
if position == 7:
for line in [0,1,2]:
if game[line][line] == 0:
writePosition(((line*3)+line), player)
return 0
#check if there is a chance of the Player wins and block it
for obj in objects:
if obj.otherGoWins() is True:
position = objects.index(obj)
#block lines
if position <= 2:
writePosition(game[position].index(0)+(position*3), player)
#block columns
elif position > 2 and position < 6:
column = position - 3
for line in [0,1,2]:
if game[line][column] == 0:
writePosition(((line*3)+column), player)
#block diagonals
elif position > 5:
if position == 6:
for line in [2,1,0]:
if game[line][2-line] == 0:
writePosition(((line*3)+(2-line)), player)
if position == 7:
for line in [0,1,2]:
if game[line][line] == 0:
writePosition(((line*3)+line), player)
return 0
#check what houses are blank and choose one randomly, but first, select the middle field if is blank
possibilities = []
index = 0
for number in game[0]+game[1]+game[2]:
if number == 0:
if index == 4:
writePosition(4, player)
return 0
possibilities.append(index)
index += 1
random.seed(datetime.now())
position = random.choice(possibilities)
print possibilities
writePosition(position, player)
win = False
while win is False:
#acertar verificacao, sair do loop imediatamente se houve ganhador
getPlayerMark(1)
win = verifyWinner(1)
if win:
break
inteligence(2)
print "O computador ja jogou!"
win = verifyWinner(2)
print game[0]
print game[1]
print game[2]