Skip to content

Instantly share code, notes, and snippets.

@musale
Last active June 15, 2017 14:53
Show Gist options
  • Save musale/485ec4310f604744f85b6c5082fb0951 to your computer and use it in GitHub Desktop.
Save musale/485ec4310f604744f85b6c5082fb0951 to your computer and use it in GitHub Desktop.
xxx
"""TestCase for rps game."""
import unittest
from homework_2 import playRockPaperScissors
class RPSRelationCase(unittest.TestCase):
"""RPSRelationCase unittest."""
def test_rock_paper_scissor_function(self):
"""Test the relations between options."""
# Do the rest of the test cases
# Remember to also test for invalid arguments depending on
# how you will refactor your code to handle such situations
self.assertTrue(playRockPaperScissors("r", "r"), "Tie!")
self.assertEqual(playRockPaperScissors("r", "p"), "Player 1 wins")
if __name__ == '__main__':
unittest.main()
def playRockPaperScissors(player1, player2):
"""Rock, Paper, Scissors game."""
if player1 == player2:
return "Tie!"
# You can even refactor this so that instead of if ... elif ... you
# do a single statement like
# if player2 == "p" or player2 == "s"
# then return the single similar result
elif player1 == "r":
if player2 == "p":
return "Player 1 wins"
elif player2 == "s":
return "Player 1 wins"
elif player1 == "p":
if player2 == "r":
return "Player 2 wins"
elif player2 == "s":
return "Player 2 wins"
elif player1 == "s":
if player2 == "r":
return "Player 2 wins"
elif player2 == "p":
return "Player 1 wins"
# How you run the game
if __name__ == '__main__':
# something like python xxx r p
player1 = sys.argv[1]
player2 = sys.argv[2] # or you can hook that random move you have implemented here :)
print playRockPaperScissors(player1, player2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment