1
+
2
+ # python interpreter
3
+ # Python 3.12.6 (tags/v3.12.6:a4a2d2b, Sep 6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)] on win32
4
+ # @Author(Aymar Sedami NAHUM)
5
+ # @Version(11/01/2024)
6
+
7
+ from collections import OrderedDict
8
+ import random
9
+
10
+ class RPSLS :
11
+
12
+ def __init__ (self , player_number , comp_number ):
13
+
14
+ self .player_number = player_number
15
+ self .comp_number = comp_number
16
+
17
+ # Step 1. and Step 2.
18
+ def name_to_number (self ):
19
+ """ From a helper function that converts name into a number to a dict"""
20
+
21
+ self .dict_name_to_number = OrderedDict ()
22
+ self .dict_name_to_number ["rock" ] = 0
23
+ self .dict_name_to_number ["paper" ] = 1
24
+ self .dict_name_to_number ["scissors" ] = 2
25
+ self .dict_name_to_number ["lizard" ] = 3
26
+ self .dict_name_to_number ["spock" ] = 4
27
+
28
+ #for key in dict_name_to_number.items():
29
+ #print(key)
30
+
31
+ return self .dict_name_to_number
32
+
33
+ # Step 3.
34
+ def rpsls_first_part (self ):
35
+ """ A method that returns the player choice"""
36
+
37
+ self .choices = ["ROCK" , "PAPER" , "SPOCK" , "LIZARD" , "SCISSORS" ]
38
+ print (" " )
39
+ print ("Dear Player" )
40
+ print ("Choose between those five : " )
41
+ print ("rock, spock, paper, lizard, scissors" )
42
+
43
+ self .player_choice = input (" " )
44
+
45
+ print ("" )
46
+ print (self .player_choice )
47
+
48
+ # then compute the number
49
+
50
+ try :
51
+
52
+ if self .choices .index (self .player_choice .upper ()) == - 1 :
53
+ print ("That choice is not in the list." )
54
+
55
+ for k , v in self .name_to_number ().items ():
56
+ if k == self .player_choice :
57
+ self .player_number = self .name_to_number ()[k ]
58
+ print ("The player_number is : " + str (self .player_number ))
59
+ # v1
60
+ except ValueError as e :
61
+ print ("That choice is not allowed, try one more time :" )
62
+ self .player_choice = input (" " )
63
+
64
+ # Step 4.
65
+ def rpsls_second_part (self ):
66
+ """ A method that returns the computer choice"""
67
+
68
+ print (" " )
69
+ print (" The computer guess is : " )
70
+ self .comp_number = random .randrange (5 )
71
+ for value in self .name_to_number ().values ():
72
+ if self .comp_number == value :
73
+ print (self .comp_number )
74
+ return self .comp_number
75
+
76
+ # Spep 5.
77
+ def rpsls_last_part (self ):
78
+ """ A method that returns the winner"""
79
+ print (" " )
80
+
81
+ if ((self .comp_number - self .player_number ) == 0 ):
82
+ print ("That is a tie. Try again " )
83
+
84
+ if (self .comp_number > self .player_number ):
85
+ if (self .comp_number - self .player_number <= 2 ):
86
+ print ("The computer wins. " )
87
+ #elif:
88
+ else :
89
+ print (" You wins !!! " )
90
+
91
+ elif (self .player_number > self .comp_number ):
92
+ if (self .player_number - self .comp_number <= 2 ):
93
+ print (" You wins !!! " )
94
+ #elif:
95
+ else :
96
+ print (" The computer wins. " )
97
+
98
+ def __str__ (self ):
99
+
100
+ self .instructions = " RPSLS, each choice wins against the preceding two choices and loses"
101
+ self .instructions = self .instructions + " RPSLS, each choice wins against the preceding two choices and loses"
102
+ self .instructions = self .instructions + " against the following two choices. "
103
+ self .expandedList = " • 0 — rock • 1 — Spock • 2 — paper • 3 — lizard • 4 — scissors "
104
+
105
+ return "\n " + self .instructions + "\n " + self .expandedList
106
+
107
+
108
+ Game_RPLS = RPSLS (0 , 0 )
109
+ print (Game_RPLS )
110
+ Game_RPLS .name_to_number ()
111
+ Game_RPLS .rpsls_first_part ()
112
+ Game_RPLS .rpsls_second_part ()
113
+ Game_RPLS .rpsls_last_part ()
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
0 commit comments