repl.it
@sharpvik/

Rock | Paper | Scissors

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from random import choice

class RPS:
	def __init__(self):
		self.opt = ['rock', 'paper', 'scissors']
		self.usr_score = 0
		self.cpu_score = 0
		self.rules = {
			'rock' 		  : 'scissors',
			'paper'		  : 'rock',
			'scissors'	: 'paper'
		}
		self.match()

	def err(self, msg):
		print(f"ERROR: {msg}")

	def disp_scores(self):
		print(f"SCORES:\n\tYOU: {self.usr_score}\n\tCPU: {self.cpu_score}\n")
	
	def ask(self):
		for i in range( len(self.opt) ): print(f"{i + 1}. {self.opt[i]}")
		usr = input("\nSelect one option: ")
		try: usr = int(usr) - 1
		except ValueError: 
			self.err("INPUT INVALID! YOU MUST INPUT A NUMBER!\n")
			return self.ask()
		if -1 < usr < len(self.opt): return self.opt[usr]
		else:
			self.err( "INPUT INVALID! YOU MUST INPUT A NUMBER BETWEEN 1 AND {len(self.opt)}!\n" )
			return self.ask()

	def tie(self):
		print("It's a tie")
		self.usr_score += 1
		self.cpu_score += 1

	def usr_win(self):
		print("You won!")
		self.usr_score += 1

	def cpu_win(self):
		print("CPU won!")
		self.cpu_score += 1

	def match(self):
		cpu = choice(self.opt)
		usr = self.ask()
		print(f"You chose {usr.upper()} and CPU chose {cpu.upper()}")
		if usr == cpu: self.tie()
		else:
			if self.rules[usr] == cpu: self.usr_win()
			else: self.cpu_win()
		self.disp_scores()
		endit = input("If you want to stop, enter 'q'. Otherwise, press ENTER. ").lower() == 'q'
		print()
		if not endit: self.match()
		else: print("Bye-bye!")

game = RPS()