-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoueur.py
More file actions
69 lines (60 loc) · 2.24 KB
/
joueur.py
File metadata and controls
69 lines (60 loc) · 2.24 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 14 09:51:20 2022
"""
import random
import copy
class Joueur:
def __init__(self,nom,couleur,AI=False,AI_type='random'):
self.couleur=couleur
self.nom=nom
self.AI=AI
self.AI_type=AI_type
def creationAdversaire(self):
self.adversaire=Joueur("Adversaire",AI=True,AI_type=self.AI_type)
def Choice(self,board):
if self.AI_type=='random':
return self.randomChoice(board)
else:
return self.best_choice(board)
def randomChoice(self,board):
current_board=board
# check valid position
liste_pos_valide=[]
for row in current_board.arr:
for pion in row:
pos_valide,__= current_board.valide_position_ai(pion.position,self.couleur)
if pos_valide:
liste_pos_valide.append(pion.position)
#choose position at random
position_choisie=random.choice(liste_pos_valide)
return position_choisie
def best_choice(self,board):
current_board=board
liste_pos_valide=[]
for row in current_board.arr:
for pion in row:
pos_valide,__= current_board.valide_position_ai(pion.position,self.couleur)
if pos_valide:
liste_pos_valide.append(pion.position)
simulated_board=[current_board.duplicate() for position in liste_pos_valide]
liste_score=[]
for new_board,pos in zip(simulated_board,liste_pos_valide):
new_board.placePion(pos,self.couleur)
new_board.score()
black=new_board.score_black
white=new_board.score_white
liste_score.append((pos,black,white))
for (position,black,white) in liste_score:
score_max_couleur=0
best_position=0
if self.couleur=='Blanc':
if white>=score_max_couleur:
score_max_couleur=white
best_position=position
else:
if black>=score_max_couleur:
score_max_couleur=white
best_position=position
return best_position