From 170a74bcea308716fe674215fae93d46ff296c0c Mon Sep 17 00:00:00 2001 From: Sander Land Date: Sat, 12 Dec 2020 14:42:23 +0100 Subject: [PATCH] previous mistake --- README.md | 3 ++- katrain/__main__.py | 17 +++++++++-------- katrain/core/game.py | 14 +++++++++++--- katrain/gui.kv | 12 ++++++++---- katrain/gui/theme.py | 1 + 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7d320c0..9d1182d 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,8 @@ In addition to shortcuts mentioned above and those shown in the main menu: * **[Ctrl-delete]** Delete current node. * **[c]** Collapse/Uncollapse the branch from the current node to the previous branching point. * **[b]** Go back to the previous branching point. -* **[n]** As in clicking the red arrow, go to one move before the next mistake (orange or worse) by a human player. +* **[n]** As in clicking the forward red arrow, go to one move before the next mistake (orange or worse) by a human player. +* **[Shift-n]** As in clicking the backward red arrow, go to one move before the previous mistake. * **[scroll up]**: Undo move. Only works when hovering the cursor over the board. * **[scroll down]**: Redo move. Only works when hovering the cursor over the board. * **[click on a move]**: See detailed statistics for a previous move, along with expected variation that was best instead of this move. diff --git a/katrain/__main__.py b/katrain/__main__.py index 85703c1..6f2af61 100644 --- a/katrain/__main__.py +++ b/katrain/__main__.py @@ -313,9 +313,9 @@ class KaTrainGui(Screen, KaTrainBase): self.board_gui.animating_pv = None self.game.redo(n_times) - def _do_next_mistake(self): + def _do_find_mistake(self, fn="redo"): self.board_gui.animating_pv = None - self.game.redo(999, stop_on_mistake=self.config("trainer/eval_thresholds")[-4]) + getattr(self.game, fn)(9999, stop_on_mistake=self.config("trainer/eval_thresholds")[-4]) def _do_cycle_children(self, *args): self.board_gui.animating_pv = None @@ -480,7 +480,7 @@ class KaTrainGui(Screen, KaTrainBase): self.engine, analyze_fast=False ) # speed up result for looking at end of game self._do_new_game(move_tree=move_tree, analyze_fast=True) - self("redo", 999) + self("redo", 9999) self.log("Imported game from clipboard.", OUTPUT_INFO) def on_touch_up(self, touch): @@ -514,7 +514,6 @@ class KaTrainGui(Screen, KaTrainBase): "i": ("insert-mode",), "p": ("play", None), "l": ("play-to-end",), - "n": ("next-mistake",), "b": ("undo", "branch"), "down": ("switch-branch", 1), "up": ("switch-branch", -1), @@ -561,15 +560,17 @@ class KaTrainGui(Screen, KaTrainBase): elif keycode[1] in ["`", "~", "f12"]: self.zen = (self.zen + 1) % 3 elif keycode[1] in ["left", "z"]: - self("undo", 1 + shift_pressed * 9 + ctrl_pressed * 999) + self("undo", 1 + shift_pressed * 9 + ctrl_pressed * 9999) elif keycode[1] in ["right", "x"]: - self("redo", 1 + shift_pressed * 9 + ctrl_pressed * 999) + self("redo", 1 + shift_pressed * 9 + ctrl_pressed * 9999) elif keycode[1] == "home": - self("undo", 999) + self("undo", 9999) elif keycode[1] == "end": - self("redo", 999) + self("redo", 9999) elif keycode[1] == "pageup": self.controls.move_tree.make_selected_node_main_branch() + elif keycode[1] == "n": + self("find-mistake", "undo" if shift_pressed else "redo") elif keycode[1] == "delete" and ctrl_pressed: self.controls.move_tree.delete_selected_node() elif keycode[1] == "c" and not ctrl_pressed: diff --git a/katrain/core/game.py b/katrain/core/game.py index feae7d8..a4dc887 100644 --- a/katrain/core/game.py +++ b/katrain/core/game.py @@ -257,10 +257,10 @@ class Game: self.current_node = node self._calculate_groups() - def undo(self, n_times=1): + def undo(self, n_times=1, stop_on_mistake=None): # allow undo/delete only in insert mode cn = self.current_node # avoid race conditions - if self.insert_mode: + if self.insert_mode: # in insert mode, undo = delete if n_times == 1 and cn not in self.insert_after.nodes_from_root: cn.parent.children = [c for c in cn.parent.children if c != cn] self.current_node = cn.parent @@ -270,7 +270,15 @@ class Game: if n_times == "branch": n_times = 9999 break_on_branch = True - for _ in range(n_times): + for move in range(n_times): + if ( + stop_on_mistake is not None + and cn.points_lost is not None + and cn.points_lost >= stop_on_mistake + and self.katrain.players_info[cn.player].player_type != PLAYER_AI + ): + self.set_current_node(cn.parent) + return if cn.shortcut_from: cn = cn.shortcut_from elif not cn.is_root: diff --git a/katrain/gui.kv b/katrain/gui.kv index 0049206..82898dd 100644 --- a/katrain/gui.kv +++ b/katrain/gui.kv @@ -186,9 +186,13 @@ pos_hint: {'center_x': 0.5,'center_y':0.5} Label: size_hint: 0.075, 0.5 + NavIconButton: + icon: 'Previous-Mistake.png' + color: Theme.MISTAKE_BUTTON_COLOR + on_press: root.katrain('find-mistake','undo') NavIconButton: icon: 'Previous-End.png' - on_press: root.katrain('undo',999) + on_press: root.katrain('undo',9999) NavIconButton: icon: 'Previous-5.png' on_press: root.katrain('undo',10) @@ -218,11 +222,11 @@ on_press: root.katrain('redo',10) NavIconButton: icon: 'Next-End.png' - on_press: root.katrain('redo',999) + on_press: root.katrain('redo',9999) NavIconButton: icon: 'Next-Mistake.png' - color: [0.79,0.06,0.06,1] - on_press: root.katrain('next-mistake') + color: Theme.MISTAKE_BUTTON_COLOR + on_press: root.katrain('find-mistake','redo') ClickableLabel: pos_hint: {'center_x': 0.85,'center_y':0.5} size_hint: None, 1 diff --git a/katrain/gui/theme.py b/katrain/gui/theme.py index 00db9c2..cd97442 100644 --- a/katrain/gui/theme.py +++ b/katrain/gui/theme.py @@ -46,6 +46,7 @@ class Theme: MENU_ITEM_SHORTCUT_COLOR = LIGHT_GREY PLAY_ANALYZE_TAB_COLOR = YELLOW INPUT_FONT_COLOR = WHITE + MISTAKE_BUTTON_COLOR = [0.79, 0.06, 0.06, 1] # gui spacing CP_SPACING = 6