Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoのscoreサブコマンドやinfoコマンド自体の入力が無いときに、InfoListenerがKeyErrorを吐かないようにした #52

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions cshogi/usi/Engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InfoListener:
print(info_listener.pv)
print(info_listener.info)
"""
re_info = re.compile(r'^info (.* |)score (cp|mate) ([+\-0-9]+) (.* |)pv ([^ ]+)(.*)$')
re_info = re.compile(r'^info (.* |)pv (\S+)(.*)$')
re_bestmove = re.compile(r'bestmove ([^ ]+).*$')

def __init__(self, mate_score: int = 100000, listner: Optional[Callable] = None):
Expand All @@ -32,28 +32,34 @@ def __init__(self, mate_score: int = 100000, listner: Optional[Callable] = None)

@staticmethod
def _split_info(m: re.Match) -> Dict:
items = (m[1] + m[4]).split(' ')
items = m[1].strip().split(' ')
info_dict = {}
for name, value in zip(items[::2], items[1::2]):
info_dict[name] = int(value)
if m[2] == 'cp':
info_dict['score'] = 'cp'
info_dict['cp'] = int(m[3])
else:
info_dict['score'] = 'mate'
if m[3] == '+' or m[3] == '-':
info_dict['mate'] = m[3]
else:
info_dict['mate'] = int(m[3])
info_dict['pv'] = (m[5] + m[6]).split(' ')
while items:
name = items.pop(0)
if name == 'pv':
info_dict['pv'] = items
break
elif name == 'string':
info_dict['string'] = m[1][7:]
break
elif name == 'score':
name = items.pop(0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nameを上書きして大丈夫でしょうか?

info_dict['score'] = name

try:
info_dict[name] = int(items.pop(0))
except ValueError:
info_dict[name] = items.pop(0)

info_dict['pv'] = (m[2] + m[3]).split(' ')
return info_dict

def __call__(self, line: str):
if self.__listner is not None:
self.__listner(line)
m = InfoListener.re_info.match(line)
if m:
self.__info[m[5]] = m
self.__info[m[2]] = m
self._last_line = line

def listen(self) -> 'InfoListener':
Expand Down Expand Up @@ -89,6 +95,9 @@ def info(self) -> Dict:

:return: A dictionary containing detailed information of the best move.
"""
if self.bestmove not in self.__info.keys():
return None

return InfoListener._split_info(self.__info[self.bestmove])

@property
Expand All @@ -97,28 +106,33 @@ def score(self) -> int:

:return: The score for the best move.
"""
m = self.__info[self.bestmove]
if m[2] == 'cp':
return int(m[3])
if self.bestmove not in self.__info.keys():
return None

info = InfoListener._split_info(self.__info[self.bestmove])
if 'cp' in info.keys():
return int(info['cp'])
else:
if m[3] == '+':
if 'mate' in info.keys():
mate = info['mate']
else:
return None
if mate == '+' or mate > 0:
return self.__mate_score
elif m[3] == '-':
elif mate == '-' or mate <= 0:
return -self.__mate_score
else:
if int(m[3]) > 0:
return self.__mate_score
else:
return -self.__mate_score

@property
def pv(self) -> List[str]:
"""Gets the Principal Variation (PV) for the best move.

:return: A list containing the Principal Variation.
"""
if self.bestmove not in self.__info.keys():
return None

m = self.__info[self.bestmove]
return (m[5] + m[6]).split(' ')
return (m[2] + m[3]).split(' ')


class MultiPVListener:
Expand Down