Skip to content

Commit 025bbb9

Browse files
committed
refactor: reduce complexity of resume_game_menu
- change _get_empty_message from method to attribute - reduce nesting - isolate condition to a separate function
1 parent 0d411a9 commit 025bbb9

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

src/mastermind/ui/menu/resume_game_menu.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, Union
22

33
import pandas as pd
4+
45
from mastermind.main.game_history import GameHistoryManager
56
from mastermind.ui.menu.data_menu import DataDisplayMenu
67
from mastermind.utils.render_dataframe import render_dataframe
@@ -13,6 +14,7 @@ class ResumeGameMenu(DataDisplayMenu):
1314

1415
name = "Resume Game"
1516
width = 27
17+
_empty_message = "No continuable game found."
1618

1719
def __init__(self):
1820
"""
@@ -36,12 +38,6 @@ def _render_data(self, data: pd.DataFrame) -> None:
3638
render_dataframe(data)
3739
print("\n(0) Return to Main Menu")
3840

39-
def _empty_message(self) -> str:
40-
"""
41-
Returns the message to display when there are no continuable games.
42-
"""
43-
return "No continuable game found."
44-
4541
def _process_option(self, option: str) -> Union[str, int]:
4642
"""
4743
Processes the selected option, returning either "return" or the index of the selected game.
@@ -56,19 +52,16 @@ def get_option(self) -> Union[str, int]:
5652
if self.menu_length == 0:
5753
input("\nPress Enter to continue...")
5854
return 0
59-
else:
60-
while True:
61-
option = input("Select a game to resume: ")
62-
try:
63-
option = int(option)
6455

65-
except ValueError:
66-
print("Invalid input. Please enter a number.")
56+
while True:
57+
option = input("Select a game to resume: ")
58+
59+
if _is_option_valid(option, self.menu_length):
60+
return int(option)
6761

68-
else:
69-
if 0 <= option <= self.menu_length:
70-
return option
62+
print("Invalid input or option. Try again.")
63+
self.display()
7164

72-
print("Invalid option. Try again.")
7365

74-
self.display()
66+
def _is_option_valid(option: int, number_of_options: int) -> bool:
67+
return option.isdigit() and 0 <= option <= number_of_options

0 commit comments

Comments
 (0)