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

Tsumego frame #506

Merged
merged 7 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
menus
  • Loading branch information
Sander Land committed Mar 4, 2022
commit 7bc475cdf5fc9efb20de211310158c444d743f31
33 changes: 22 additions & 11 deletions katrain/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,24 @@ def _do_engine_recovery_popup(self, error_message, code):
popup.content.popup = popup
popup.open()

def _do_tsumego_frame(self, ko, margin):
from katrain.core.tsumego_frame import tsumego_frame_from_katrain_game
if not self.game.stones:
return

black_to_play_p = self.next_player_info.player == "B"
node, analysis_region = tsumego_frame_from_katrain_game(
self.game, self.game.komi, black_to_play_p, ko_p=ko, margin=margin
)
self.game.set_current_node(node)
if self.play_mode.mode == MODE_PLAY:
self.play_mode.switch_ui_mode() # go to analysis mode
if analysis_region:
flattened_region = [analysis_region[0][1],analysis_region[0][0],analysis_region[1][1],analysis_region[1][0]]
self.game.set_region_of_interest(flattened_region)
node.analyze(self.game.engines[node.next_player])
self.update_state(redraw_board=True)

def play_mistake_sound(self, node):
if self.config("timer/sound") and node.played_sound is None and Theme.MISTAKE_SOUNDS:
node.played_sound = True
Expand Down Expand Up @@ -650,15 +668,6 @@ def popup_open(self) -> Popup:
first_child = app.root_window.children[0]
return first_child if isinstance(first_child, Popup) else None

def tsumego_frame(self, ko_p):
from katrain.core.tsumego_frame import tsumego_frame_from_katrain_game

black_to_play_p = self.next_player_info.player == "B"
node, analysis_region = tsumego_frame_from_katrain_game(self.game, self.game.komi, black_to_play_p, ko_p)
self.game.set_current_node(node)
self.update_state(redraw_board=True)
# todo: set region of interest by analysis_region (if it is truthy)

def _on_keyboard_down(self, _keyboard, keycode, _text, modifiers):
self.last_key_down = keycode
ctrl_pressed = "ctrl" in modifiers or ("meta" in modifiers and kivy_platform == "macosx")
Expand All @@ -674,9 +683,11 @@ def _on_keyboard_down(self, _keyboard, keycode, _text, modifiers):
Theme.KEY_TEACHER_POPUP,
Theme.KEY_AI_POPUP,
Theme.KEY_CONFIG_POPUP,
Theme.KEY_TSUMEGO_FRAME,
Theme.KEY_CONTRIBUTE_POPUP,
]: # switch between popups
popup.dismiss()

return
elif keycode[1] in Theme.KEY_SUBMIT_POPUP:
fn = getattr(popup.content, "on_submit", None)
Expand Down Expand Up @@ -726,10 +737,10 @@ def _on_keyboard_down(self, _keyboard, keycode, _text, modifiers):
self("undo", "main-branch")
elif keycode[1] == Theme.KEY_DEEPERANALYSIS_POPUP:
self.analysis_controls.dropdown.open_game_analysis_popup()
elif keycode[1] == Theme.KEY_TSUMEGO_FRAME:
self.analysis_controls.dropdown.open_tsumego_frame_popup()
elif keycode[1] == Theme.KEY_REPORT_POPUP:
self.analysis_controls.dropdown.open_report_popup()
elif keycode[1] == "u":
self.tsumego_frame(shift_pressed)
elif keycode[1] == "f10" and self.debug_level >= OUTPUT_EXTRA_DEBUG:
import yappi

Expand Down
3 changes: 1 addition & 2 deletions katrain/core/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class Lang(Observable):
observers = []
callbacks = []
FONTS = {"jp": "NotoSansJP-Regular.otf",
"tr": "NotoSans-Regular.ttf"}
FONTS = {"jp": "NotoSansJP-Regular.otf", "tr": "NotoSans-Regular.ttf"}

def __init__(self, lang):
super(Lang, self).__init__()
Expand Down
17 changes: 7 additions & 10 deletions katrain/core/tsumego_frame.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import itertools
from katrain.core.game_node import GameNode
from katrain.core.sgf_parser import Move

# tsumego frame ported from lizgoban by kaorahi
# note: coords = (j, i) in katrain

margin = 4
near_to_edge = 2
offence_to_win = 5

BLACK = "B"
WHITE = "W"

# note: coords = (j, i) in katrain


def tsumego_frame_from_katrain_game(game, komi, black_to_play_p, ko_p):
def tsumego_frame_from_katrain_game(game, komi, black_to_play_p, ko_p, margin):
current_node = game.current_node
bw_board = [[game.chains[c][0].player if c >= 0 else "-" for c in line] for line in game.board]
isize, jsize = ij_sizes(bw_board)
blacks, whites, analysis_region = tsumego_frame(bw_board, komi, black_to_play_p, ko_p)
blacks, whites, analysis_region = tsumego_frame(bw_board, komi, black_to_play_p, ko_p, margin)
sgf_blacks = katrain_sgf_from_ijs(blacks, isize, jsize, "B")
sgf_whites = katrain_sgf_from_ijs(whites, isize, jsize, "W")

Expand All @@ -32,9 +29,9 @@ def katrain_sgf_from_ijs(ijs, isize, jsize, player):
return [Move((j, i)).sgf((jsize, isize)) for i, j in ijs]


def tsumego_frame(bw_board, komi, black_to_play_p, ko_p):
def tsumego_frame(bw_board, komi, black_to_play_p, ko_p, margin):
stones = stones_from_bw_board(bw_board)
filled_stones = tsumego_frame_stones(stones, komi, black_to_play_p, ko_p)
filled_stones = tsumego_frame_stones(stones, komi, black_to_play_p, ko_p, margin)
region_pos = pick_all(filled_stones, "tsumego_frame_region_mark")
bw = pick_all(filled_stones, "tsumego_frame")
blacks = [(i, j) for i, j, black in bw if black]
Expand All @@ -55,7 +52,7 @@ def get_analysis_region(region_pos):
return ri[0] < ri[1] and rj[0] < rj[1] and (ri, rj)


def tsumego_frame_stones(stones, komi, black_to_play_p, ko_p):
def tsumego_frame_stones(stones, komi, black_to_play_p, ko_p, margin):
sizes = ij_sizes(stones)
isize, jsize = sizes
ijs = [
Expand Down Expand Up @@ -83,7 +80,7 @@ def tsumego_frame_stones(stones, komi, black_to_play_p, ko_p):
)
if True in flip_spec:
flipped = flip_stones(stones, flip_spec)
filled = tsumego_frame_stones(flipped, komi, black_to_play_p, ko_p)
filled = tsumego_frame_stones(flipped, komi, black_to_play_p, ko_p, margin)
return flip_stones(filled, flip_spec)
# put outside stones
i0 = imin - margin
Expand Down
5 changes: 5 additions & 0 deletions katrain/gui.kv
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,11 @@
icon: 'analysis.png'
shortcut: 'F3'
on_action: root.open_report_popup()
AnalysisMenuItem:
text: i18n._("analysis:tsumegoframe")
icon: 'New-Game.png'
shortcut: ''
on_action: root.open_tsumego_frame_popup()
AnalysisMenuItem:
text: i18n._("analysis:continuous")
icon: 'play.png'
Expand Down
10 changes: 9 additions & 1 deletion katrain/gui/badukpan.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from katrain.core.lang import i18n
from katrain.core.utils import evaluation_class, format_visits, var_to_grid, json_truncate_arrays
from katrain.gui.kivyutils import draw_circle, draw_text, cached_texture
from katrain.gui.popups import I18NPopup, ReAnalyzeGamePopup, GameReportPopup
from katrain.gui.popups import I18NPopup, ReAnalyzeGamePopup, GameReportPopup, TsumegoFramePopup
from katrain.gui.theme import Theme


Expand Down Expand Up @@ -805,6 +805,14 @@ def open_report_popup(self, *_args):
report_popup.content.popup = report_popup
report_popup.open()

def open_tsumego_frame_popup(self, *_args):
analysis_popup = I18NPopup(
title_key="analysis:tsumegoframe", size=[dp(500), dp(350)], content=TsumegoFramePopup()
)
analysis_popup.content.popup = analysis_popup
analysis_popup.content.katrain = MDApp.get_running_app().gui
analysis_popup.open()


class AnalysisControls(MDBoxLayout):
dropdown = ObjectProperty(None)
Expand Down
8 changes: 8 additions & 0 deletions katrain/gui/popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,14 @@ def on_submit(self):
self.button.trigger_action(duration=0)


class TsumegoFramePopup(BoxLayout):
katrain = ObjectProperty(None)
popup = ObjectProperty(None)

def on_submit(self):
self.button.trigger_action(duration=0)


class GameReportPopup(BoxLayout):
def __init__(self, katrain, **kwargs):
super().__init__(**kwargs)
Expand Down
1 change: 1 addition & 0 deletions katrain/gui/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class Theme:
KEY_SELFPLAY_TO_END = "l"
KEY_STOP_ANALYSIS = "escape"
KEY_TOGGLE_CONTINUOUS_ANALYSIS = "spacebar"
KEY_TSUMEGO_FRAME = "u"

KEY_COPY = "c"
KEY_PASTE = "v"
Expand Down
Binary file modified katrain/i18n/locales/cn/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/cn/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,15 @@ msgstr "Prune Branch"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/de/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/de/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,15 @@ msgstr "Prune Branch"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/en/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/en/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ msgstr "Fast analysis of all moves"
msgid "analysis:game"
msgstr "Deeper full game analysis"

#. Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"

#. View a player performance report
msgid "analysis:report"
msgstr "Performance Report"
Expand Down Expand Up @@ -1008,3 +1012,11 @@ msgstr "Target score for Black:"
#. Description for how many moves
msgid "setup position move number"
msgstr "Generate game until move:"

#. distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"
Binary file modified katrain/i18n/locales/es/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/es/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,15 @@ msgstr "Prune Branch"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/fr/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/fr/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,15 @@ msgstr "Prune Branch"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/jp/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/jp/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -971,3 +971,15 @@ msgstr "この枝だけ残して他を削除"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/ko/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/ko/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,15 @@ msgstr "Prune Branch"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/ru/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/ru/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,15 @@ msgstr "Prune Branch"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/tr/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/tr/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -1034,3 +1034,15 @@ msgstr "Şu hamleye kadar oyun üret:"
#. path UP TO this node
msgid "Prune Branch"
msgstr "Prune Branch"

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
Binary file modified katrain/i18n/locales/tw/LC_MESSAGES/katrain.mo
Binary file not shown.
12 changes: 12 additions & 0 deletions katrain/i18n/locales/tw/LC_MESSAGES/katrain.po
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,15 @@ msgstr "Prune Branch"
#. TODO
msgid "aihelp:antimirror"
msgstr "Anti Mirror-go Mode of KataGo."

#. TODO - distance of wall to existing stones
msgid "tsumego:walldistance"
msgstr "Distance of wall"

#. TODO - tsumego: ko is good enough?
msgid "tsumego:ko"
msgstr "Ko allowed?"

#. TODO - Menu option for tsumego frame around position
msgid "analysis:tsumegoframe"
msgstr "Tsumego Frame"
33 changes: 33 additions & 0 deletions katrain/popups.kv
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,39 @@
root.katrain("analyze-extra", "game", visits=visits.input_value, mistakes_only=mistakes.input_value)
root.popup.dismiss()

<TsumegoFramePopup>:
ko: ko
margin: margin
orientation: 'vertical'
button: button
BoxLayout:
size_hint: 1, 1.5
orientation: 'horizontal'
DescriptionLabel:
text: i18n._('tsumego:walldistance')
AnchorLayout:
LabelledIntInput:
id: margin
text: '5'
BoxLayout:
size_hint: 1, 1.5
orientation: 'horizontal'
DescriptionLabel:
text: i18n._('tsumego:ko')
AnchorLayout:
LabelledCheckBox:
id: ko
AnchorLayout:
size_hint: 1, 1
AutoSizedRoundedRectangleButton:
id: button
padding_x: 15
size_hint: None,0.66
text: i18n._("analysis:tsumegoframe") # re-use string
on_press:
root.katrain("tsumego-frame", ko=ko.input_value, margin=margin.input_value)
root.popup.dismiss()

<LoadSGFPopup>:
fast: fast
rewind: rewind
Expand Down