-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.py
116 lines (85 loc) · 4.24 KB
/
Player.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
import Move
class Player:
def __init__(self,color,controller):
self.color=color
self.points=0
self.controller=controller
def makeMove(self, position):
self.controller.placeTile( position, self.color)
def getPoints(self):
return self.points
def setPoints(self,points):
self.points=points
def getColor():
return self.color
def returnValidMoves(self, color, playingField):
allValidMoves = []
#allValidMoves = [[0 for i in range(len(playingField))] for j in range(len(playingField))]
for i in range(len(playingField)):
for j in range(len(playingField)):
tilesToFlip=[]
if playingField[i][j] == 0:
nw = self.isValid(color, -1, -1, i, j, playingField)
if nw:
for count in range(nw+ 1):
tilesToFlip.append([i-count, j-count])
nn = self.isValid(color, -1, 0, i, j, playingField)
if nn:
for count in range(nn+ 1):
tilesToFlip.append([i-count, j])
ne = self.isValid(color, -1, 1, i, j, playingField)
if ne:
for count in range(ne+ 1):
tilesToFlip.append([i-count,j+count])
ee = self.isValid(color, 0, 1, i, j, playingField)
if ee:
for count in range(ee+ 1):
tilesToFlip.append([i,j+count])
se = self.isValid(color, 1, 1, i, j, playingField)
if se:
for count in range(se+ 1):
tilesToFlip.append([i+count,j+count])
ss = self.isValid(color, 1, 0, i, j, playingField)
if ss:
for count in range(ss + 1):
tilesToFlip.append([i+count,j])
sw = self.isValid(color, 1, -1, i, j, playingField)
if sw:
for count in range(sw+ 1):
tilesToFlip.append([i+count,j-count])
ww = self.isValid(color, 0, -1, i, j, playingField)
if ww:
for count in range(ww+ 1):
tilesToFlip.append([i,j-count])
points = nw + nn + ne + ee + se + ss + sw + ww
if (points > 0):
move = Move.Move([i,j], points, color,tilesToFlip)
allValidMoves.append(move)
return allValidMoves
def isValid(self, playerColor, offseti, offsetj, i, j, playingField):
other = ""
if(playerColor == "B"):
other = "W"
elif (playerColor == "W"):
other = "B"
else:
print("problemo with colorino")
if (((offseti + i) < 0) or ((offseti + i) > (len(playingField)-1)) or ((offsetj + j) < 0) or ((offsetj + j) >(len(playingField)-1))):
return 0
if (playingField [offseti + i][offsetj + j] != other):
return 0
elif (playingField [offseti + i][offsetj + j] == other):
# kolla samma linje check line function
return self.findSelfInLine(playerColor, offseti, offsetj, offseti + i, offsetj + j, playingField, other, 1)
def findSelfInLine(self, playerColor, offseti, offsetj, i, j, playingField, other, counter):
if (((offseti + i) < 0) or ((offseti + i )>(len(playingField)-1)) or ((offsetj + j) < 0) or ((offsetj + j) >(len(playingField)-1))):
return 0
else:
if (playingField [offseti + i][offsetj + j] == other):
counter += 1
counter=self.findSelfInLine(playerColor, offseti, offsetj, offseti + i, offsetj + j, playingField, other, counter)
return counter
elif (playingField [offseti + i][offsetj + j] == playerColor):
return counter
else:
return 0