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

Simplified the logic for rock paper scissors and added tests #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 26 additions & 30 deletions Rock-Paper-Scissors/rock_paper_scissors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,32 @@ def get_computer_selection():
return action

def determine_winner(user_action, computer_action):
beats = {
Action.Rock: Action.Scissors,
Action.Paper: Action.Rock,
Action.Scissors: Action.Paper
}

if user_action == computer_action:
print(f"Both players selected {user_action.name}. It's a tie!")
elif user_action == Action.Rock:
if computer_action == Action.Scissors:
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == Action.Paper:
if computer_action == Action.Rock:
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == Action.Scissors:
if computer_action == Action.Paper:
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")

while True:
try:
user_action = get_user_selection()
except ValueError as e:
range_str = f"[0, {len(Action) - 1}]"
print(f"Invalid selection. Enter a value in range {range_str}")
continue

computer_action = get_computer_selection()
determine_winner(user_action, computer_action)

play_again = input("Play again? (y/n): ")
if play_again.lower() != "y":
break
elif beats[user_action] == computer_action:
print(f"{user_action.name} beats {computer_action.name}! You win!")
else:
print(f"{computer_action.name} beats {user_action.name}! You lose.")

if __name__ == '__main__':
while True:
try:
user_action = get_user_selection()
except ValueError as e:
range_str = f"[0, {len(Action) - 1}]"
print(f"Invalid selection. Enter a value in range {range_str}")
continue

computer_action = get_computer_selection()
determine_winner(user_action, computer_action)

play_again = input("Play again? (y/n): ")
if play_again.lower() != "y":
break

46 changes: 46 additions & 0 deletions Rock-Paper-Scissors/test_rock_paper_scissors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
from unittest.mock import patch
from rock_paper_scissors import get_user_selection, get_computer_selection, determine_winner, Action

class TestRockPaperScissors(unittest.TestCase):
@patch('builtins.input', return_value='0')
def test_get_user_selection_rock(self, mock_input):
self.assertEqual(get_user_selection(), Action.Rock)

@patch('builtins.input', return_value='1')
def test_get_user_selection_paper(self, mock_input):
self.assertEqual(get_user_selection(), Action.Paper)

@patch('builtins.input', return_value='2')
def test_get_user_selection_scissors(self, mock_input):
self.assertEqual(get_user_selection(), Action.Scissors)

@patch('random.randint', return_value=0)
def test_get_computer_selection_rock(self, mock_randint):
self.assertEqual(get_computer_selection(), Action.Rock)

@patch('random.randint', return_value=1)
def test_get_computer_selection_paper(self, mock_randint):
self.assertEqual(get_computer_selection(), Action.Paper)

@patch('random.randint', return_value=2)
def test_get_computer_selection_scissors(self, mock_randint):
self.assertEqual(get_computer_selection(), Action.Scissors)

@patch('builtins.print')
def test_determine_winner_tie(self, mock_print):
determine_winner(Action.Rock, Action.Rock)
mock_print.assert_called_once_with("Both players selected Rock. It's a tie!")

@patch('builtins.print')
def test_determine_winner_win(self, mock_print):
determine_winner(Action.Rock, Action.Scissors)
mock_print.assert_called_once_with("Rock beats Scissors! You win!")

@patch('builtins.print')
def test_determine_winner_lose(self, mock_print):
determine_winner(Action.Rock, Action.Paper)
mock_print.assert_called_once_with("Paper beats Rock! You lose.")

if __name__ == '__main__':
unittest.main()