-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.py
191 lines (162 loc) · 6.73 KB
/
protocol.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
#!/usr/bin/python3
from patterns import *
from board import *
from brain import *
from timer import *
# Print function with flush
def myPrint(toBePrint, myEnd):
print(toBePrint, flush=True, end=myEnd)
# Class which contain all mandatory protocol function
class Protocol:
def __init__(self):
self.input = ""
self.arg = []
self.output = ""
self.boardSize = 0
self.gameBoard = MyBoard()
self.isStart = True
# Read input and stock it in self.input
def readInput(self, args, file):
if len(args) == 2:
self.input = file.readline()
# print("DEBUG " + self.input)
elif len(args) == 1:
self.input = input()
# print("DEBUG " + self.input)
# Print output which is in self.output
def printOutput(self):
myPrint(self.output, "")
# START [VALUE] : Where value is the board size, the function return if the size is OK or NOT
def start(self, value):
try:
size = int(value)
if size < 5:
self.output = "ERROR wrong size\n"
return
self.gameBoard.createBoard(size)
self.boardSize = size
self.output = "OK\n"
except ValueError:
self.output = "ERROR can't create the board\n"
# TURN [X][Y] : Where X and Y are the position of the opponent's move, the function return the position X,Y of the player's move
def turn(self, arg1) -> str:
try:
value = arg1.split(',')
value1 = int(value[0])
value2 = int(value[1])
self.gameBoard.doMove(value1, value2, 2)
self.move_manager()
except ValueError:
return ("ERROR turn command\n")
# BEGIN : The player have to play first, the function return the position X,Y of the player's move
def begin(self):
pos = (self.boardSize - 1) / 2
pos = pos.__round__()
self.gameBoard.doMove(pos, pos, 1)
self.output = str(pos) + "," + str(pos) + "\n"
self.isStart = False
# BOARD [X][Y][FIELD] : Where X and Y are position and FIELD is the player, the function have to return the position X,Y of the player's move
def board(self, args, file):
self.readInput(args, file)
try:
self.arg = self.input.split()
nbArg = len(self.arg)
if (nbArg == 1 and self.arg[0] == "DONE"):
self.move_manager()
elif (nbArg == 1):
value = self.arg[0].split(',')
pos_x = int(value[0])
pos_y = int(value[1])
player = int(value[2])
self.gameBoard.doMove(pos_x, pos_y, player)
self.board(args, file)
except ValueError:
self.readInput(args, file)
# INFO [KEY][VALUE] : The player don't need to answer this
def info(arg1, arg2) -> str:
return ("")
# END : The player have to quit as fast as possible and should not write anything more
def end(self):
exit(0)
# ABOUT : The brain is expected to send some information about itself on one line. Each info must be written as keyword, equals sign, text value in quotation marks.
# Values should be separated by commas that can be followed by spaces.
def about(self):
self.output = "name=\"BBGOBB\", version=\"1.0\"\n"
def score(self, player):
score = 0
score += check_patterns(self.gameBoard, patternsAllyFour) * 16
score += check_patterns(self.gameBoard, patternsAllyThree) * 8
score += check_patterns(self.gameBoard, patternsAllyTwo) * 4
score -= check_patterns(self.gameBoard, patternsEnemyFour) * 16
score -= check_patterns(self.gameBoard, patternsEnemyThree) * 8
score -= check_patterns(self.gameBoard, patternsEnemyTwo) * 4
if (player == 2):
score -= score * 2
return (score)
def computeInput(self, args, file):
self.arg = self.input.split()
nbArg = len(self.arg)
if (self.arg[0] == "START" and nbArg == 2):
self.start(self.arg[1])
self.printOutput()
elif (self.arg[0] == "TURN" and nbArg == 2):
self.turn(self.arg[1])
self.printOutput()
elif (self.arg[0] == "BEGIN" and nbArg == 1):
self.begin()
self.printOutput()
elif (self.arg[0] == "BOARD"):
self.board(args, file)
self.printOutput()
elif (self.arg[0] == "INFO" and nbArg == 3):
self.info(self.arg[1], self.arg[2])
elif (self.arg[0] == "END" and nbArg == 1):
self.end()
elif (self.arg[0] == "ABOUT" and nbArg == 1):
self.about()
self.printOutput()
elif (self.arg[0] == "DISPLAY"):
self.gameBoard.displayBoard()
elif (self.arg[0] == "SCORE"):
self.score(2)
self.printOutput()
def move_manager(self):
t = Timer()
t.start()
pos = self.is_mandatory_move()
if (pos[0] == -1 and pos[1] == -1):
if (self.isStart == True):
pos = self.play_first_move(t)
else:
pos = find_move(self.gameBoard, self.boardSize, t)
if (pos[0] == -1 and pos[1] == -1):
pos = randPos(self.gameBoard, self.boardSize)
self.gameBoard.doMove(pos[0], pos[1], 1)
self.output = str(pos[0]) + "," + str(pos[1]) + "\n"
t.stop()
def is_mandatory_move(self):
res_match = is_matching_pattern(self.gameBoard, patternsAllyFour, 0)
res_match_enemy = is_matching_pattern(self.gameBoard, patternsEnemyFour, 0)
res_match3 = is_matching_pattern(self.gameBoard, patternsAllyThree, 1)
res_match_enemy3 = is_matching_pattern(self.gameBoard, patternsEnemyThree, 1)
if (res_match[0] == True):
return (res_match[1], res_match[2])
elif (res_match_enemy[0] == True):
return (res_match_enemy[1], res_match_enemy[2])
elif (res_match3[0] == True):
return (res_match3[1], res_match3[2])
elif (res_match_enemy3[0] == True):
return (res_match_enemy3[1], res_match_enemy3[2])
return (-1, -1)
def play_first_move(self, t):
pos = (self.boardSize - 1) / 2
pos = pos.__round__()
if (self.gameBoard.board[pos][pos] == 0):
self.gameBoard.doMove(pos, pos, 1)
self.output = str(pos) + "," + str(pos) + "\n"
self.isStart = False
return (pos, pos)
pos = find_move(self.gameBoard, self.boardSize, t)
if (pos[0] == -1 and pos[1] == -1):
pos = randPos(self.gameBoard, self.boardSize)
return (pos[0], pos[1])