-
Notifications
You must be signed in to change notification settings - Fork 52
/
kyon034.py
65 lines (57 loc) · 1.74 KB
/
kyon034.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
from reversi2022.reversi import *
from reversi2022.canvas import game2
import random
def get_key_from_value(d, val):
keys = [k for k, v in d.items() if v == val]
if keys:
return random.choice(keys)
return [6,6]
# 貪欲法クラス
class GreedAI(GameAI):
def name(self):
return '貪欲なAI'
def play(self, board, color):
l_score = {}
for y in range(board.N):
for x in range(board.N):
s = board.put_and_reverse(x, y, color, reverse=False)
if s> 0:
l_score[(x,y)] = s
key = get_key_from_value(l_score, max(l_score.values()))
return (key)
# 評価表クラス
class TableAI(GameAI):
def name(self):
return '評価表を使ったAI'
def play(self, board, color):
l_score = {}
score = [[30, -5, 10, 10, -5, 30],[-5, 0, 5, 5, 0, -5], [10, 5, 0, 0, 5, 10], [10, 5, 0, 0, 5, 10], [-5, 0, 5, 5, 0, -5], [30, -5, 10, 10, -5, 30]]
for y in range(board.N):
for x in range(board.N):
if board.put_and_reverse(x, y, color, reverse=False) > 0:
s = score[x][y]
l_score[(x,y)] = s
key = get_key_from_value(l_score, max(l_score.values()))
return (key)
# 評価表と貪欲のミックス
class Greed_and_TableAI(GameAI):
def name(self):
return '034のAI'
def play(self, board, color):
corner = [(0,0),(0,5),(5,0),(5,5)]
l_score = {}
for y in range(board.N):
for x in range(board.N):
s = board.put_and_reverse(x, y, color, reverse=False)
if s> 0:
l_score[(x,y)] = s
for i in l_score.keys():
if i in corner:
return i
c = np.count_nonzero(board.b==0)
if c >= 24:
AI = GreedAI()
return AI.play(board, color)
else:
AI = TableAI()
return AI.play(board, color)