|
2 | 2 | import os |
3 | 3 | import time |
4 | 4 |
|
| 5 | + |
5 | 6 | def clear(): |
6 | | - os.system("clear") |
| 7 | + os.system("clear") |
| 8 | + |
7 | 9 |
|
8 | 10 | # Set of instructions for Rock-Paper-Scissors |
9 | 11 | def rps_instructions(): |
| 12 | + print("***********") |
| 13 | + print("Instructions for Rock-Paper-Scissors : ") |
| 14 | + print("***********") |
| 15 | + print("Rock crushes Scissors") |
| 16 | + print("Scissors cuts Paper") |
| 17 | + print("Paper covers Rock") |
| 18 | + print("***********") |
10 | 19 |
|
11 | | - print("***********") |
12 | | - print("Instructions for Rock-Paper-Scissors : ") |
13 | | - print("***********") |
14 | | - print("Rock crushes Scissors") |
15 | | - print("Scissors cuts Paper") |
16 | | - print("Paper covers Rock") |
17 | | - print("***********") |
18 | 20 |
|
19 | 21 | def rps(): |
20 | | - |
21 | | - global rps_table |
22 | | - global game_map |
23 | | - global name |
24 | | - |
25 | | - # Game Loop for each game of Rock-Paper-Scissors |
26 | | - while True: |
27 | | - |
28 | | - print("--------------------------------------") |
29 | | - print("\t\tMenu") |
30 | | - print("--------------------------------------") |
31 | | - print("Enter \"help\" for instructions") |
32 | | - print("Enter \"rock\",\"paper\",\"scissors\" to play") |
33 | | - print("Enter \"exit\" to quit") |
34 | | - print("--------------------------------------") |
35 | | - |
36 | | - print("***********") |
37 | | - |
38 | | - # Player Input |
39 | | - inp = raw_input("Enter your move : ") |
40 | | - |
41 | | - if inp == "help": |
42 | | - clear() |
43 | | - rps_instructions() |
44 | | - continue |
45 | | - elif inp == "exit": |
46 | | - clear() |
47 | | - break |
48 | | - elif inp == "rock": |
49 | | - player_move = 0 |
50 | | - elif inp == "paper": |
51 | | - player_move = 1 |
52 | | - elif inp == "scissors": |
53 | | - player_move = 2 |
54 | | - else: |
55 | | - clear() |
56 | | - print("Wrong Input!!") |
57 | | - rps_instructions() |
58 | | - continue |
59 | | - |
60 | | - print("Computer making a move....") |
61 | | - |
62 | | - print("***********") |
63 | | - time.sleep(2) |
64 | | - |
65 | | - # Get the computer move randomly |
66 | | - comp_move = random.randint(0, 2) |
67 | | - |
68 | | - # Print the computer move |
69 | | - print("Computer chooses ", game_map[comp_move].upper()) |
70 | | - |
71 | | - # Find the winner of the match |
72 | | - winner = rps_table[player_move][comp_move] |
73 | | - |
74 | | - # Declare the winner |
75 | | - if winner == player_move: |
76 | | - print(name, "WINS!!!") |
77 | | - elif winner == comp_move: |
78 | | - print("COMPUTER WINS!!!") |
79 | | - else: |
80 | | - print("TIE GAME") |
81 | | - |
82 | | - print("***********") |
83 | | - time.sleep(2) |
84 | | - clear() |
| 22 | + # Game Loop for each game of Rock-Paper-Scissors |
| 23 | + while True: |
| 24 | + print("--------------------------------------") |
| 25 | + print("\t\tMenu") |
| 26 | + print("--------------------------------------") |
| 27 | + print("Enter \"help\" for instructions") |
| 28 | + print("Enter \"rock\",\"paper\",\"scissors\" to play") |
| 29 | + print("Enter \"exit\" to quit") |
| 30 | + print("--------------------------------------") |
| 31 | + |
| 32 | + print("***********") |
| 33 | + |
| 34 | + # Player Input |
| 35 | + inp = raw_input("Enter your move : ") |
| 36 | + |
| 37 | + if inp == "help": |
| 38 | + clear() |
| 39 | + rps_instructions() |
| 40 | + continue |
| 41 | + elif inp == "exit": |
| 42 | + clear() |
| 43 | + break |
| 44 | + elif inp == "rock": |
| 45 | + player_move = 0 |
| 46 | + elif inp == "paper": |
| 47 | + player_move = 1 |
| 48 | + elif inp == "scissors": |
| 49 | + player_move = 2 |
| 50 | + else: |
| 51 | + clear() |
| 52 | + print("Wrong Input!!") |
| 53 | + rps_instructions() |
| 54 | + continue |
| 55 | + |
| 56 | + print("Computer making a move....") |
| 57 | + |
| 58 | + print("***********") |
| 59 | + time.sleep(2) |
| 60 | + |
| 61 | + # Get the computer move randomly |
| 62 | + comp_move = random.randint(0, 2) |
| 63 | + |
| 64 | + # Print the computer move |
| 65 | + print("Computer chooses ", game_map[comp_move].upper()) |
| 66 | + |
| 67 | + # Find the winner of the match |
| 68 | + winner = rps_table[player_move][comp_move] |
| 69 | + |
| 70 | + # Declare the winner |
| 71 | + if winner == player_move: |
| 72 | + print(name, "WINS!!!") |
| 73 | + elif winner == comp_move: |
| 74 | + print("COMPUTER WINS!!!") |
| 75 | + else: |
| 76 | + print("TIE GAME") |
| 77 | + |
| 78 | + print("***********") |
| 79 | + time.sleep(2) |
| 80 | + clear() |
| 81 | + |
| 82 | + |
| 83 | +def main(): |
| 84 | + global rps_table |
| 85 | + global game_map |
| 86 | + global name |
| 87 | + |
| 88 | + # The mapping between moves and numbers |
| 89 | + game_map = {0:"rock", 1:"paper", 2:"scissors"} |
| 90 | + |
| 91 | + # Win-lose matrix for traditional game |
| 92 | + rps_table = [[-1, 1, 0], [1, -1, 2], [0, 2, -1]] |
| 93 | + |
| 94 | + name = raw_input("Enter your name: ") |
| 95 | + |
| 96 | + # The GAME LOOP |
| 97 | + while True: |
| 98 | + |
| 99 | + # The Game Menu |
| 100 | + print("***********") |
| 101 | + print("Let's Play!!!") |
| 102 | + print("Enter 1 to play Rock-Paper-Scissors") |
| 103 | + print("Enter 2 to quit") |
| 104 | + print("***********") |
| 105 | + |
| 106 | + # Try block to handle the player choice |
| 107 | + try: |
| 108 | + choice = int(raw_input("Enter your choice = ")) |
| 109 | + except ValueError: |
| 110 | + clear() |
| 111 | + print("Wrong Choice") |
| 112 | + continue |
| 113 | + |
| 114 | + # Play the traditional version of the game |
| 115 | + if choice == 1: |
| 116 | + rps() |
| 117 | + |
| 118 | + # Quit the GAME LOOP |
| 119 | + elif choice == 2: |
| 120 | + break |
| 121 | + |
| 122 | + # Other wrong input |
| 123 | + else: |
| 124 | + clear() |
| 125 | + print("Wrong choice. Read instructions carefully.") |
85 | 126 |
|
86 | 127 |
|
87 | 128 | # The main function |
88 | 129 | if __name__ == '__main__': |
89 | | - |
90 | | - # The mapping between moves and numbers |
91 | | - game_map = {0:"rock", 1:"paper", 2:"scissors"} |
92 | | - |
93 | | - # Win-lose matrix for traditional game |
94 | | - rps_table = [[-1, 1, 0], [1, -1, 2], [0, 2, -1]] |
95 | | - |
96 | | - name = raw_input("Enter your name: ") |
97 | | - |
98 | | - # The GAME LOOP |
99 | | - while True: |
100 | | - |
101 | | - # The Game Menu |
102 | | - print("***********") |
103 | | - print("Let's Play!!!") |
104 | | - print("Enter 1 to play Rock-Paper-Scissors") |
105 | | - print("Enter 2 to quit") |
106 | | - print("***********") |
107 | | - |
108 | | - # Try block to handle the player choice |
109 | | - try: |
110 | | - choice = int(raw_input("Enter your choice = ")) |
111 | | - except ValueError: |
112 | | - clear() |
113 | | - print("Wrong Choice") |
114 | | - continue |
115 | | - |
116 | | - # Play the traditional version of the game |
117 | | - if choice == 1: |
118 | | - rps() |
119 | | - |
120 | | - # Quit the GAME LOOP |
121 | | - elif choice == 2: |
122 | | - break |
123 | | - |
124 | | - # Other wrong input |
125 | | - else: |
126 | | - clear() |
127 | | - print("Wrong choice. Read instructions carefully.") |
128 | | - |
129 | | - |
130 | | - |
| 130 | + main() |
0 commit comments