Skip to content

Instantly share code, notes, and snippets.

@NoobStuff
Created December 30, 2015 07:06
Show Gist options
  • Save NoobStuff/dfa84660c3d2bd5c626e to your computer and use it in GitHub Desktop.
Save NoobStuff/dfa84660c3d2bd5c626e to your computer and use it in GitHub Desktop.
rock paper scissors (lizard spock) noob project
import random
RED = '\x1b[31m'
YELLOW = '\x1b[33m'
GREEN = '\x1b[32m'
RESET = '\x1b[0m'
class RPS(object):
# Move : Counter
regular_moves = {
'rock': 'paper',
'paper': 'scissors',
'scissors': 'rock' }
# Move : Counters
lizardspock_moves = {
'rock': ['paper', 'spock'],
'paper': ['scissors', 'lizard'],
'scissors': ['spock', 'rock'],
'lizard': ['rock', 'scissors'],
'spock': ['lizard', 'paper'] }
def __init__(self):
self.round_length = 3
self.invalid_move_notify = 1
self.round_losses = 0
self.round_wins = 0
self.moves_dict = None
self.moves = None
def _setup(self):
while True:
user_input = input('Play [r]egular or [l]izardspock? ')
if user_input == 'q':
self._quit()
elif user_input == 'r':
self.moves_dict = self.regular_moves
self.moves = list(self.moves_dict.keys())
break
elif user_input == 'l':
self.moves_dict = self.lizardspock_moves
self.moves = list(self.moves_dict.keys())
break
else:
print('Invalid input.')
def _play_again(self):
while True:
user_input = input('Play again? (y/n) ').lower()
if user_input == 'y' or user_input == 'yes':
self.invalid_move_notify = 1
self.round_losses = 0
self.round_wins = 0
return
elif user_input == 'n' or user_input == 'no' or user_input == 'q':
self._quit()
else:
print('Invalid input.')
def _quit(self):
print('Goodbye!')
exit(0)
def play(self):
print('-*-*-*-*-*-*-*-*-*-')
print('Rock Paper Scissors')
print(' (LizardSpock)\n')
print(' Best of %s' % self.round_length)
print('-v-v-v-v-v-v-v-v-v-\n')
print('(type q to quit)\n')
self._setup()
while True:
if self.round_losses >= self.round_length:
print(RED + 'You lost this round!' + RESET)
self._play_again()
self._setup()
elif self.round_wins >= self.round_length:
print(GREEN + 'You won this round!' + RESET)
self._play_again()
self._setup()
user_move = input('Make your move >>> ').lower()
if user_move == 'q':
self._quit()
elif user_move not in self.moves:
print(RED + 'Invalid move.' + RESET)
if self.invalid_move_notify:
print(RED + 'Valid moves are %s.' % self.moves + RESET)
self.invalid_move_notify = 0
else:
ai_move = random.choice(self.moves)
print('Your move: %s' % user_move)
print(' AI move: %s' % ai_move)
if user_move == ai_move:
print(YELLOW + 'Draw!' + RESET)
elif ai_move in self.moves_dict[user_move]:
self.round_losses += 1
print(RED + 'You lost! (%d/%d)' % (self.round_losses, self.round_length) + RESET)
else:
self.round_wins += 1
print(GREEN + 'You won! (%d/%d)' % (self.round_wins, self.round_length) + RESET)
if __name__ == '__main__':
RPS().play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment