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 220 221 222 223 224 225 226
| # -*- coding: utf-8 -*-
"""
Created on Mon Apr 6 13:26:32 2020
@author: Nguyen
"""
from Model import*
from random import*
class SamplePlayer:
def __init__(self):
self.board = []
def initGame(self,color):
self.color=color
self.board = []
for i in range(19):
self.board.append(19*[None])
self.capturesPlayer = 0
self.capturesAdv = 0
self.turn = 1 # Numéros du tour
self.currentMove = None
def searchMove(self, moves):
#Mise a jour du plateau du jeu d'après le coup de l'adversaire
if (moves != []):
self.apply(moves[0], self.color.next())
if (self.turn == 1 and self.color == Color.White):
self.currentMove = Move(0,0)
self.turn += 1
elif (self.turn == 1):
found = False
while (not found):
x = randint(-9, 9)
y = randint(-9, 9)
found = self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
elif (self.turn == 2 and self .color == Color.Black):
found = False
while (not found):
x = randint(-9,9)
y = randint(-9,9)
found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
### ligne rajoutée
elif (self.turn == 3 and self .color == Color.Black):
found = False
while (not found):
x = randint(-9,9)
y = randint(-9,9)
found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
elif (self.turn == 4 and self .color == Color.Black):
found = False
while (not found):
x = randint(-9,9)
y = randint(-9,9)
found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
elif (self.turn == 5 and self .color == Color.Black):
found = False
while (not found):
x = randint(-9,9)
y = randint(-9,9)
found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
elif (self.turn == 6 and self .color == Color.Black):
found = False
while (not found):
x = randint(-9,9)
y = randint(-9,9)
found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
elif (self.turn == 7 and self .color == Color.Black):
found = False
while (not found):
x = randint(-9,9)
y = randint(-9,9)
found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
elif (self.turn == 8 and self .color == Color.Black):
found = False
while (not found):
x = randint(-9,9)
y = randint(-9,9)
found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
self.currentMove = Move(x,y)
self.turn += 1
else:
self.currentMove = None # currentMove = coup à jouer
self.movesAlign4 = []
self.movesAlign3 = []
self.movesAlign2 = []
found = self.evalBoard(self.board)
if (not found):
if (self.movesAlign4 != []):
n = randint(len(self.movesAlign4))
self.currentMove = self.movesAlign4[n]
## elif (self.movesAlign3 != []):
## n= randint(len(self.movesAlign3))
## self.currentMove = self.movesAlign3[n]
## elif (self.movesAlign2 != []):
## n= randint(len(self.movesAlign2))
## self.currentMove = self.movesAlign2[n]
#print('pass') # A compléter
self.apply(self.currentMove, self.color)
def apply(self, move, color):
i = move.x + 9
j = move.y + 9
self.board[i][j] = color
for d in list(Direction):
self.checkCapture(i, j, d, color)
def checkCapture(self, i, j, d, color):
i1 = i + d.deltaX()
j1 = j + d.deltaY()
i2 = i + 2*d.deltaX()
j2 = j + 2*d.deltaY()
i3 = i + 3*d.deltaX()
j3 = j + 3*d.deltaY()
if (self.inBoard(i3, j3) and self.board[i1][j1] == color.next() and self.board[i2][j2] == color.next() and self.board[i3][j3] == color):
self.board[i2][j2] = None
self.board[i1][j1] = None
if (color == self.color):
self.capturesPlayer += 1
else:
self.capturesAdv += 1
def inBoard(self, i, j):
return 0 <= i < 19 and 0 <= j < 19
def evalBoard(self, t):
for i in range(19):
for j in range(19):
if (t[i][j] == self.color):
d = Direction.E
while (d != Direction.W):
found = self.evalDir(t, i, j, d)
if (found):
return True
else:
d = d.next()
elif (t[i][j] == self.color.next()):
d = Direction.E
while (d != Direction.W):
found = self.evalDir(t, i, j, d)
if (found):
return True
else:
d = d.next()
#print('pass') # A compléter !
def evalDir(self, t, i, j, d):
i1, j1 = self.nextCoord(i, j, d)
i2, j2 = self.nextCoord(i1, j1, d)
i3, j3 = self.nextCoord(i2, j2, d)
i4, j4 = self.nextCoord(i3, j3, d)
i_1, j_1 = self.nextCoord(i, j, d.next())
# _ o o o o _
if (self.inBoard(i3,j3) and t[i3][j3] == t[i2][j2] == t[i1][j1] == self.color):
if (self.inBoard(i_1, j_1) and t[i_1][j_1] == None):
self.currentMove = Move(i_1 - 9, j_1 - 9)
return True
elif (self.inBoard(i4,j4) and t[i4][j4] == None):
self.currentMove = Move(i4 - 9, j4 - 9)
return True
# _ o _ o o
elif (self.inBoard(i3, j3) and t[i3][j3] == t[i2][j2] == self.color and t[i1][j1] == None):
self.movesAlign4.append(Move(i1-9,j1-9))
# _ o o _
elif (self.inBoard(i2, j2) and t[i1][j1] == self.color and t[i2][j2] == None):
self.movesAlign3.append(Move(i2-9,j2-9))
# O_O_O
elif (self.inBoard(i2, j2) and t[i1][j1] == t[i3][j3] == self.color and t[i2][j2] == None):
self.movesAlign3.append(Move(i2-9,j2-9))
def nextCoord(self, i, j, d):
return i + d.deltaX(), j + d.deltaY()
# Liste de tous les coups possibles
# moves = [(Move(7, 7), 100), Move(4, 3), 50),]
class RandomPlayer:
def __init__(self):
self.color=None
self.freeMove=[]
self.currentMove=None
def initGame(self,color):
self.color=color
self.freeMove=[]
for i in range(-9,10):
for j in range(-9,10):
self.freeMove.append(Move(i,j))
self.freeMove.remove(Move(0,0))
self.freeMove.remove(Move(0,3))
self.freeMove.remove(Move(0,-3))
shuffle(self.freeMove) #pour modifier l'ordre (aléatoire)
self.freeMove=[Move(0,0),Move(0,3),Move(0,-3)]+self.freeMove
self.currentMove=None
def searchMove(self,moves): #pour ne pas mettre 2 fois le même pion dans la liste
if(moves!=[]):
self.freeMove.remove(moves[0])
self.currentMove=self.freeMove[0]
del self.freeMove[0] |
Partager