Skip to content

Commit

Permalink
1.8.4: new zealand/tt rules, katago 1.8.1 (#406)
Browse files Browse the repository at this point in the history
* new zealand and tt rules

* test

* 1.8.4

* note at start, avoid spaces

* fix dict rules, test
  • Loading branch information
sanderland authored Apr 11, 2021
1 parent 82757e5 commit 7dcb80e
Show file tree
Hide file tree
Showing 26 changed files with 149 additions and 38 deletions.
8 changes: 6 additions & 2 deletions katrain/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ def _message_loop_thread(self):
"save_game",
"find_mistake",
]:
self.controls.set_status(i18n._("gui-locked").format(action=msg), STATUS_INFO, check_level=False)
self.controls.set_status(
i18n._("gui-locked").format(action=msg), STATUS_INFO, check_level=False
)
continue
fn = getattr(self, f"_do_{msg}")
fn(*args, **kwargs)
Expand All @@ -292,7 +294,9 @@ def __call__(self, message, *args, **kwargs):
if self.game:
if message.endswith("popup"): # gui code needs to run in main kivy thread.
if self.contributing and "save" not in message and message != "contribute-popup":
self.controls.set_status(i18n._("gui-locked").format(action=message), STATUS_INFO, check_level=False)
self.controls.set_status(
i18n._("gui-locked").format(action=message), STATUS_INFO, check_level=False
)
return
fn = getattr(self, f"_do_{message.replace('-', '_')}")
Clock.schedule_once(lambda _dt: fn(*args, **kwargs), -1)
Expand Down
2 changes: 1 addition & 1 deletion katrain/core/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PROGRAM_NAME = "KaTrain"
VERSION = "1.8.3"
VERSION = "1.8.4"
HOMEPAGE = "https://github.com/sanderland/katrain"
CONFIG_MIN_VERSION = "1.8.0" # keep config files from this version
ANALYSIS_FORMAT_VERSION = "1.0"
Expand Down
11 changes: 7 additions & 4 deletions katrain/core/contribute_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ def __init__(self, katrain):
self.move_speed = self.config.get("movespeed", 2.0)

exe = self.get_engine_path(self.config.get("katago"))
cacert_path = os.path.join(os.path.split(exe)[0], 'cacert.pem')
cacert_path = os.path.join(os.path.split(exe)[0], "cacert.pem")
if not os.path.isfile(cacert_path):
try:
shutil.copyfile( find_package_resource('katrain/KataGo/cacert.pem'), cacert_path)
shutil.copyfile(find_package_resource("katrain/KataGo/cacert.pem"), cacert_path)
except Exception as e:
self.katrain.log(f"Could not copy cacert file ({e}), please add it manually to your katago.exe directory",OUTPUT_ERROR)
self.katrain.log(
f"Could not copy cacert file ({e}), please add it manually to your katago.exe directory",
OUTPUT_ERROR,
)
cfg = find_package_resource(self.config.get("config"))

settings_dict = {
Expand All @@ -63,7 +66,7 @@ def __init__(self, katrain):
"maxSimultaneousGames": self.config.get("maxgames") or self.DEFAULT_MAX_GAMES,
"includeOwnership": self.config.get("ownership") or False,
"logGamesAsJson": True,
"homeDataDir": os.path.expanduser(DATA_FOLDER)
"homeDataDir": os.path.expanduser(DATA_FOLDER),
}
self.max_buffer_games = 2 * settings_dict["maxSimultaneousGames"]
settings = {f"{k}={v}" for k, v in settings_dict.items()}
Expand Down
8 changes: 4 additions & 4 deletions katrain/core/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ class EngineDiedException(Exception):

class BaseEngine: # some common elements between analysis and contribute engine

# TODO: we don't support suicide in game.py, so no "tt": "tromp-taylor", "nz": "new-zealand"
RULESETS_ABBR = [
("jp", "japanese"),
("cn", "chinese"),
("ko", "korean"),
("aga", "aga"),
("tt", "tromp-taylor"),
("nz", "new zealand"),
("stone_scoring", "stone_scoring"),
]
RULESETS = {fromkey: name for abbr, name in RULESETS_ABBR for fromkey in [abbr, name]}
Expand All @@ -41,8 +42,7 @@ def __init__(self, katrain, config):
self.config = config

@staticmethod
def get_rules(node):
ruleset = node.ruleset
def get_rules(ruleset):
if ruleset.strip().startswith("{"):
try:
ruleset = json.loads(ruleset)
Expand Down Expand Up @@ -386,7 +386,7 @@ def request_analysis(
if self.config.get("wide_root_noise", 0.0) > 0.0: # don't send if 0.0, so older versions don't error
settings["wideRootNoise"] = self.config["wide_root_noise"]
query = {
"rules": self.get_rules(analysis_node),
"rules": self.get_rules(analysis_node.ruleset),
"priority": self.base_priority + priority,
"analyzeTurns": [len(moves)],
"maxVisits": visits,
Expand Down
31 changes: 20 additions & 11 deletions katrain/core/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ def neighbours(moves):
if self.board[move.coords[1]][move.coords[0]] != -1:
raise IllegalMoveException("Space occupied")

# merge chains connected by this move, or create a new one
nb_chains = list({c for c in neighbours([move]) if c >= 0 and self.chains[c][0].player == move.player})
if nb_chains:
this_chain = nb_chains[0]
self.board = [
[nb_chains[0] if sq in nb_chains else sq for sq in line] for line in self.board
] # merge chains connected by this move
self.board = [[nb_chains[0] if sq in nb_chains else sq for sq in line] for line in self.board]
for oc in nb_chains[1:]:
self.chains[nb_chains[0]] += self.chains[oc]
self.chains[oc] = []
Expand All @@ -168,9 +167,10 @@ def neighbours(moves):
self.chains.append([move])
self.board[move.coords[1]][move.coords[0]] = this_chain

# check captures
opp_nb_chains = {c for c in neighbours([move]) if c >= 0 and self.chains[c][0].player != move.player}
for c in opp_nb_chains:
if -1 not in neighbours(self.chains[c]):
if -1 not in neighbours(self.chains[c]): # no liberties
self.last_capture += self.chains[c]
for om in self.chains[c]:
self.board[om.coords[1]][om.coords[0]] = -1
Expand All @@ -179,8 +179,21 @@ def neighbours(moves):
raise IllegalMoveException("Ko")
self.prisoners += self.last_capture

if -1 not in neighbours(self.chains[this_chain]): # TODO: NZ rules?
raise IllegalMoveException("Suicide")
# suicide: check rules and throw exception if needed
if -1 not in neighbours(self.chains[this_chain]):
rules = self.rules
if len(self.chains[this_chain]) == 1: # even in new zealand rules, single stone suicide is not allowed
raise IllegalMoveException("Single stone suicide")
elif (isinstance(rules, str) and rules in ["tromp-taylor", "new zealand"]) or (
isinstance(rules, dict) and rules.get("suicide", False)
):
self.last_capture += self.chains[this_chain]
for om in self.chains[this_chain]:
self.board[om.coords[1]][om.coords[0]] = -1
self.chains[this_chain] = []
self.prisoners += self.last_capture
else: # suicide not allowed by rules
raise IllegalMoveException("Suicide")

# Play a Move from the current position, raise IllegalMoveException if invalid.
def play(self, move: Move, ignore_ko: bool = False):
Expand Down Expand Up @@ -290,7 +303,7 @@ def prisoner_count(

@property
def rules(self):
return self.root.ruleset
return KataGoEngine.get_rules(self.root.ruleset)

@property
def manual_score(self):
Expand Down Expand Up @@ -437,10 +450,6 @@ def analyze_all_nodes(self, priority=0, analyze_fast=False, even_if_present=True
node.clear_analysis()
node.analyze(self.engines[node.next_player], priority=priority, analyze_fast=analyze_fast)

@property
def rules(self): # maybe not needed
return self.engines["B"].get_rules(self.root)

def set_current_node(self, node):
if self.insert_mode:
self.katrain.controls.set_status(i18n._("finish inserting before navigating"), STATUS_ERROR)
Expand Down
15 changes: 9 additions & 6 deletions katrain/core/game_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def add_list_property(self, property: str, values: List):
for c in v.split(SGF_SEPARATOR_MARKER)
if c.strip() and SGF_INTERNAL_COMMENTS_MARKER not in c
]
self.note = "".join(comments) # no super call intended, just save as note to be editable
self.note = "".join(comments).strip() # no super call intended, just save as note to be editable
else:
return super().add_list_property(property, values)

Expand Down Expand Up @@ -143,10 +143,13 @@ def sgf_properties(
properties["SQ"] = best_sq
if top_x and "MA" not in properties:
properties["MA"] = [top_x]
comments.append(self.comment(sgf=True, interactive=False) + SGF_INTERNAL_COMMENTS_MARKER)
comments.append("\n" + self.comment(sgf=True, interactive=False) + SGF_INTERNAL_COMMENTS_MARKER)
if self.is_root:
comments = [
i18n._("SGF start message") + SGF_INTERNAL_COMMENTS_MARKER + "\n",
if save_marks:
comments = [i18n._("SGF start message") + SGF_INTERNAL_COMMENTS_MARKER + "\n"]
else:
comments = []
comments += [
*comments,
f"\nSGF generated by {PROGRAM_NAME} {VERSION}{SGF_INTERNAL_COMMENTS_MARKER}\n",
]
Expand All @@ -162,9 +165,9 @@ def sgf_properties(
elif "KTSID" in properties:
del properties["KTSID"]
if note:
comments.append(f"{self.note}")
comments.insert(0, f"{self.note}\n") # user notes at top!
if comments:
properties["C"] = [SGF_SEPARATOR_MARKER.join(comments)]
properties["C"] = [SGF_SEPARATOR_MARKER.join(comments).strip("\n")]
elif "C" in properties:
del properties["C"]
return properties
Expand Down
16 changes: 8 additions & 8 deletions katrain/gui/popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,20 @@ class BaseConfigPopup(QuickConfigGui):

KATAGOS = {
"win": {
"OpenCL v1.8.0": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-opencl-windows-x64.zip",
"Eigen AVX2 (Modern CPUs) v1.8.0": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-eigenavx2-windows-x64.zip",
"Eigen (CPU, Non-optimized) v1.8.0": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-eigen-windows-x64.zip",
"OpenCL v1.8.1": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-opencl-windows-x64.zip",
"Eigen AVX2 (Modern CPUs) v1.8.1": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-eigenavx2-windows-x64.zip",
"Eigen (CPU, Non-optimized) v1.8.1": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-eigen-windows-x64.zip",
"OpenCL v1.6.1 (bigger boards)": "https://github.com/lightvector/KataGo/releases/download/v1.6.1%2Bbs29/katago-v1.6.1+bs29-gpu-opencl-windows-x64.zip",
},
"linux": {
"OpenCL v1.8.0": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-opencl-linux-x64.zip",
"Eigen AVX2 (Modern CPUs) v1.8.0": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-eigenavx2-linux-x64.zip",
"Eigen (CPU, Non-optimized) v1.8.0": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-eigen-linux-x64.zip",
"OpenCL v1.8.1": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-opencl-linux-x64.zip",
"Eigen AVX2 (Modern CPUs) v1.8.1": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-eigenavx2-linux-x64.zip",
"Eigen (CPU, Non-optimized) v1.8.1": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-eigen-linux-x64.zip",
"OpenCL v1.6.1 (bigger boards)": "https://github.com/lightvector/KataGo/releases/download/v1.6.1%2Bbs29/katago-v1.6.1+bs29-gpu-opencl-linux-x64.zip",
},
"just-descriptions": {
"CUDA v1.8.0 (Windows)": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-gpu-cuda10.2-windows-x64.zip",
"CUDA v1.8.0 (Linux)": "https://github.com/lightvector/KataGo/releases/download/v1.8.0/katago-v1.8.0-gpu-cuda10.2-linux-x64.zip",
"CUDA v1.8.1 (Windows)": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-gpu-cuda10.2-windows-x64.zip",
"CUDA v1.8.1 (Linux)": "https://github.com/lightvector/KataGo/releases/download/v1.8.1/katago-v1.8.1-gpu-cuda10.2-linux-x64.zip",
},
}

Expand Down
Binary file modified katrain/i18n/locales/cn/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/cn/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,11 @@ msgstr "这会产生部分的对局让你接著下。例如:你可以使用它
#. change komi etc in current game
msgid "editgame"
msgstr "编辑对局资讯"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
Binary file modified katrain/i18n/locales/de/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/de/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,11 @@ msgstr ""
#. change komi etc in current game
msgid "editgame"
msgstr "Spiel editieren"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
Binary file modified katrain/i18n/locales/en/LC_MESSAGES/katrain.mo
Binary file not shown.
6 changes: 6 additions & 0 deletions katrain/i18n/locales/en/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ msgstr "Korean"
msgid "aga"
msgstr "AGA"

msgid "tromp-taylor"
msgstr "Tromp-Taylor"

msgid "new zealand"
msgstr "New Zealand"

msgid "stone_scoring"
msgstr "Ancient Chinese"

Expand Down
Binary file modified katrain/i18n/locales/es/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/es/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,11 @@ msgstr ""
#. TODO - change komi etc in current game
msgid "editgame"
msgstr "Edit Game Info"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
Binary file modified katrain/i18n/locales/fr/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/fr/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,11 @@ msgstr ""

msgid "editgame"
msgstr "Partie en cours"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
Binary file modified katrain/i18n/locales/jp/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/jp/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,11 @@ msgstr ""
#. change komi etc in current game
msgid "editgame"
msgstr "対局情報を編集"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
Binary file modified katrain/i18n/locales/ko/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/ko/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -836,3 +836,11 @@ msgstr ""

msgid "editgame"
msgstr "대국 정보 편집"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
Binary file modified katrain/i18n/locales/ru/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/ru/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,11 @@ msgstr ""

msgid "editgame"
msgstr "Редактировать игры"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
Binary file modified katrain/i18n/locales/tw/LC_MESSAGES/katrain.mo
Binary file not shown.
8 changes: 8 additions & 0 deletions katrain/i18n/locales/tw/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,11 @@ msgstr "這會產生部分的對局讓你接著下。例如:你可以使用它
#. change komi etc in current game
msgid "editgame"
msgstr "編輯對局資訊"

#. TODO
msgid "new zealand"
msgstr "New Zealand"

#. TODO
msgid "tromp-taylor"
msgstr "Tromp-Taylor"
26 changes: 24 additions & 2 deletions tests/test_board.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from katrain.core.base_katrain import KaTrainBase
from katrain.core.engine import BaseEngine
from katrain.core.game import Game, IllegalMoveException, Move, KaTrainSGF
from katrain.core.game_node import GameNode

Expand Down Expand Up @@ -55,9 +56,8 @@ def test_capture(self, new_game):
assert 3 == len(b.stones)
assert 2 == len(b.prisoners)
b.play(Move.from_gtp("B1", player="B"))
with pytest.raises(IllegalMoveException) as exc:
with pytest.raises(IllegalMoveException, match="Single stone suicide"):
b.play(Move.from_gtp("A1", player="W"))
assert "Suicide" in str(exc.value)
assert 1 == len(self.nonempty_chains(b))
assert 4 == len(b.stones)
assert 2 == len(b.prisoners)
Expand Down Expand Up @@ -118,3 +118,25 @@ def test_handicap_load(self):
root2 = KaTrainSGF.parse_sgf("(;GM[1]FF[4]SZ[19]HA[2];)")
game2 = Game(MockKaTrain(force_package_config=True), MockEngine(), move_tree=root2)
assert 2 == len(game2.root.placements)

def test_suicide(self):
rulesets_to_test = BaseEngine.RULESETS_ABBR + [('{"suicide":true}', ""), ('{"suicide":false}', "")]
for shortrule, _ in rulesets_to_test:
new_game = GameNode(properties={"SZ": 19, "RU": shortrule})
b = Game(MockKaTrain(force_package_config=True), MockEngine(), move_tree=new_game)
b.play(Move.from_gtp("A18", player="B"))
b.play(Move.from_gtp("B18", player="B"))
b.play(Move.from_gtp("C19", player="B"))
b.play(Move.from_gtp("A19", player="W"))
assert 4 == len(b.stones)
assert 0 == len(b.prisoners)

if shortrule in ["tt", "nz", '{"suicide":true}']:
b.play(Move.from_gtp("B19", player="W"))
assert 3 == len(b.stones)
assert 2 == len(b.prisoners)
else:
with pytest.raises(IllegalMoveException, match="Suicide"):
b.play(Move.from_gtp("B19", player="W"))
assert 4 == len(b.stones)
assert 0 == len(b.prisoners)

0 comments on commit 7dcb80e

Please sign in to comment.