From 987651bfc46e15af4b1b59c43465e01e5606e731 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 12:11:19 +0000 Subject: [PATCH 1/2] Initial plan From 99d9ce290918dc8f7d6d4c1ab326afc7fd719187 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 12:18:42 +0000 Subject: [PATCH 2/2] Centralize all input handling through UserInterface.getInput() Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com> --- src/location/bank.py | 16 ++++--------- src/location/home.py | 2 +- src/location/tavern.py | 8 ++----- src/ui/userInterface.py | 19 +++++++++++++++ tests/location/test_bank.py | 42 ++++++++++------------------------ tests/location/test_home.py | 4 ++-- tests/ui/test_userInterface.py | 19 +++++++++++++++ 7 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/location/bank.py b/src/location/bank.py index 6854091..40b51cd 100644 --- a/src/location/bank.py +++ b/src/location/bank.py @@ -57,13 +57,9 @@ def run(self): def deposit(self): while True: - self.userInterface.lotsOfSpace() - self.userInterface.divider() - print(self.currentPrompt.text) - self.userInterface.divider() - try: - amount = float(input("> ")) + userInput = self.userInterface.getInput(self.currentPrompt.text) + amount = float(userInput) except ValueError: self.currentPrompt.text = "Try again. Money: $%.2f" % self.player.money continue @@ -79,13 +75,9 @@ def deposit(self): def withdraw(self): while True: - self.userInterface.lotsOfSpace() - self.userInterface.divider() - print(self.currentPrompt.text) - self.userInterface.divider() - try: - amount = float(input("> ")) + userInput = self.userInterface.getInput(self.currentPrompt.text) + amount = float(userInput) except ValueError: self.currentPrompt.text = ( "Try again. Money In Bank: $%.2f" % self.player.moneyInBank diff --git a/src/location/home.py b/src/location/home.py index 145d80a..7257667 100644 --- a/src/location/home.py +++ b/src/location/home.py @@ -57,4 +57,4 @@ def displayStats(self): print("Times Gotten Drunk: %d" % self.stats.timesGottenDrunk) print("Money Lost Gambling: %d" % self.stats.moneyLostFromGambling) print("") - input(" [ CONTINUE ]") + self.userInterface.getInput(" [ CONTINUE ]") diff --git a/src/location/tavern.py b/src/location/tavern.py index 0fa7bf1..e68fd30 100644 --- a/src/location/tavern.py +++ b/src/location/tavern.py @@ -116,13 +116,9 @@ def gamble(self): continue def changeBet(self, prompt): - self.userInterface.lotsOfSpace() - self.userInterface.divider() - print(prompt) - self.userInterface.divider() - try: - self.amount = int(input("> ")) + userInput = self.userInterface.getInput(prompt) + self.amount = int(userInput) except ValueError: self.currentPrompt.text = "Try again. Money: $%d" % self.player.money return diff --git a/src/ui/userInterface.py b/src/ui/userInterface.py index 48e16f9..c845b11 100644 --- a/src/ui/userInterface.py +++ b/src/ui/userInterface.py @@ -78,3 +78,22 @@ def showOptions( return choice self.currentPrompt.text = "Try again!" + + def getInput(self, prompt): + """Get text input from the user with a prompt. + + This method centralizes all input handling, allowing for future + implementation of alternative UIs (e.g., pygame) without changing + location code. + + Args: + prompt: The text to display to the user + + Returns: + The user's input as a string + """ + self.lotsOfSpace() + self.divider() + print(prompt) + self.divider() + return input("> ") diff --git a/tests/location/test_bank.py b/tests/location/test_bank.py index 471cbdc..f625f19 100644 --- a/tests/location/test_bank.py +++ b/tests/location/test_bank.py @@ -104,18 +104,15 @@ def test_run_go_to_docks_action(): def test_deposit_success(): # prepare bankInstance = createBank() - bankInstance.userInterface.lotsOfSpace = MagicMock() - bankInstance.userInterface.divider = MagicMock() + bankInstance.userInterface.getInput = MagicMock(return_value="10") bankInstance.player.money = 100 bankInstance.player.moneyInBank = 0 - bank.print = MagicMock() - bank.input = MagicMock(return_value="10") # call bankInstance.deposit() # check - bank.print.assert_called_once() + bankInstance.userInterface.getInput.assert_called_once() assert bankInstance.player.moneyInBank == 10 assert bankInstance.player.money == 90 @@ -123,18 +120,15 @@ def test_deposit_success(): def test_deposit_failure_not_enough_money(): # prepare bankInstance = createBank() - bankInstance.userInterface.lotsOfSpace = MagicMock() - bankInstance.userInterface.divider = MagicMock() + bankInstance.userInterface.getInput = MagicMock(return_value="10") bankInstance.player.money = 5 bankInstance.player.moneyInBank = 0 - bank.print = MagicMock() - bank.input = MagicMock(return_value="10") # call bankInstance.deposit() # check - bank.print.assert_called_once() + bankInstance.userInterface.getInput.assert_called_once() assert bankInstance.player.moneyInBank == 0 assert bankInstance.player.money == 5 @@ -142,18 +136,15 @@ def test_deposit_failure_not_enough_money(): def test_withdraw_success(): # prepare bankInstance = createBank() - bankInstance.userInterface.lotsOfSpace = MagicMock() - bankInstance.userInterface.divider = MagicMock() + bankInstance.userInterface.getInput = MagicMock(return_value="10") bankInstance.player.moneyInBank = 100 bankInstance.player.money = 0 - bank.print = MagicMock() - bank.input = MagicMock(return_value="10") # call bankInstance.withdraw() # check - bank.print.assert_called_once() + bankInstance.userInterface.getInput.assert_called_once() assert bankInstance.player.moneyInBank == 90 assert bankInstance.player.money == 10 @@ -161,18 +152,15 @@ def test_withdraw_success(): def test_withdraw_failure_not_enough_money(): # prepare bankInstance = createBank() - bankInstance.userInterface.lotsOfSpace = MagicMock() - bankInstance.userInterface.divider = MagicMock() + bankInstance.userInterface.getInput = MagicMock(return_value="10") bankInstance.player.moneyInBank = 5 bankInstance.player.money = 0 - bank.print = MagicMock() - bank.input = MagicMock(return_value="10") # call bankInstance.withdraw() # check - bank.print.assert_called_once() + bankInstance.userInterface.getInput.assert_called_once() assert bankInstance.player.moneyInBank == 5 assert bankInstance.player.money == 0 @@ -180,18 +168,15 @@ def test_withdraw_failure_not_enough_money(): def test_deposit_with_decimal(): # prepare bankInstance = createBank() - bankInstance.userInterface.lotsOfSpace = MagicMock() - bankInstance.userInterface.divider = MagicMock() + bankInstance.userInterface.getInput = MagicMock(return_value="10.25") bankInstance.player.money = 100.50 bankInstance.player.moneyInBank = 0 - bank.print = MagicMock() - bank.input = MagicMock(return_value="10.25") # call bankInstance.deposit() # check - bank.print.assert_called_once() + bankInstance.userInterface.getInput.assert_called_once() assert bankInstance.player.moneyInBank == 10.25 assert bankInstance.player.money == 90.25 @@ -199,17 +184,14 @@ def test_deposit_with_decimal(): def test_withdraw_with_decimal(): # prepare bankInstance = createBank() - bankInstance.userInterface.lotsOfSpace = MagicMock() - bankInstance.userInterface.divider = MagicMock() + bankInstance.userInterface.getInput = MagicMock(return_value="10.50") bankInstance.player.moneyInBank = 100.75 bankInstance.player.money = 0 - bank.print = MagicMock() - bank.input = MagicMock(return_value="10.50") # call bankInstance.withdraw() # check - bank.print.assert_called_once() + bankInstance.userInterface.getInput.assert_called_once() assert bankInstance.player.moneyInBank == 90.25 assert bankInstance.player.money == 10.50 diff --git a/tests/location/test_home.py b/tests/location/test_home.py index 1611581..a1ecb04 100644 --- a/tests/location/test_home.py +++ b/tests/location/test_home.py @@ -116,8 +116,8 @@ def test_displayStats(): # prepare homeInstance = createHome() homeInstance.userInterface.lotsOfSpace = MagicMock() + homeInstance.userInterface.getInput = MagicMock() home.print = MagicMock() - home.input = MagicMock() # call homeInstance.displayStats() @@ -125,4 +125,4 @@ def test_displayStats(): # check assert homeInstance.userInterface.lotsOfSpace.call_count == 1 assert home.print.call_count == 7 - assert home.input.call_count == 1 + homeInstance.userInterface.getInput.assert_called_once() diff --git a/tests/ui/test_userInterface.py b/tests/ui/test_userInterface.py index 65aaa6b..d5b05a2 100644 --- a/tests/ui/test_userInterface.py +++ b/tests/ui/test_userInterface.py @@ -67,3 +67,22 @@ def test_showOptions(): userInterfaceInstance.lotsOfSpace.assert_called() assert userInterfaceInstance.divider.call_count == 3 userInterface.input.assert_called_with("\n> ") + + +def test_getInput(): + # setup + userInterfaceInstance = createUserInterface() + userInterface.print = MagicMock() + userInterface.input = MagicMock(return_value="test input") + userInterfaceInstance.lotsOfSpace = MagicMock() + userInterfaceInstance.divider = MagicMock() + + # call + result = userInterfaceInstance.getInput("Test prompt") + + # check + assert result == "test input" + userInterfaceInstance.lotsOfSpace.assert_called_once() + assert userInterfaceInstance.divider.call_count == 2 + userInterface.print.assert_called_with("Test prompt") + userInterface.input.assert_called_with("> ")