Skip to content

Commit 3706ca2

Browse files
committed
Swarag-N | Rock-Paper-Scissors
1 parent 3b4435d commit 3706ca2

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

Rock-Paper-Scissors/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Rock Paper Scissors #
2+
3+
## Description ##
4+
5+
Play more options than simple rock-paper-scissor, this script includes other options for
6+
you to try.
7+
8+
## Author ##
9+
10+
[Swarag-N](https://github.com/Swarag-N)
11+
12+
## How to Play ##
13+
14+
Create a `rating.txt` file to store your score aganist computer.
15+
16+
```console
17+
$python rock-paper-scissors.py
18+
```
19+
20+
|Option|Description|
21+
|---|---|
22+
|`!rating`|Shows score of current user|
23+
|`!exit`|Saves current score and exits|
24+
25+
Output:
26+
27+
``` code
28+
$ python rock-paper-scissors.py
29+
Enter Your name: Swarag
30+
Hello, Swarag
31+
Press Enter to cont. with default options or give options
32+
rock,paper,wolf
33+
Okay, let's start
34+
wolf
35+
There is a draw wolf
36+
rock
37+
Sorry, but computer chose paper
38+
wolf
39+
Sorry, but computer chose rock
40+
!rating
41+
Your rating: 50
42+
!exit
43+
Swarag 50
44+
Data Saved
45+
Bye!
46+
```
47+
48+
```code
49+
$ python rock-paper-scissors.py
50+
Hello, Swarag
51+
Loaded Score from Last Game
52+
Press Enter to cont. with default options or give options
53+
54+
Okay, let's start
55+
!rating
56+
Your rating: 50
57+
rock
58+
Sorry, but computer chose paper
59+
paper
60+
Well done. Computer chose rock and failed
61+
!exit
62+
Swarag 150
63+
Data Saved
64+
Bye!
65+
```

Rock-Paper-Scissors/rating.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Apple 300
2+
Swarag 150
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import random
2+
3+
all_options = ['rock', 'gun', 'lightning', 'devil', 'dragon', 'water', 'air',
4+
'paper', 'sponge', 'wolf', 'tree', 'human', 'snake', 'scissors', 'fire']
5+
6+
game_mode = True
7+
8+
double_all_options = all_options * 2
9+
all_losing_options = {}
10+
for key in range(len(all_options)):
11+
all_losing_options[all_options[key]] = double_all_options[key + 1:key + 8]
12+
13+
14+
player = str(input("Enter Your name: "))
15+
print("Hello, " + player)
16+
17+
file = open("rating.txt", "rt")
18+
data_read = file.readlines()
19+
data = [plr.split(" ") for plr in data_read]
20+
score = 0
21+
file.close()
22+
23+
for plr in data:
24+
if plr[0] == player:
25+
print("Loaded Score from Last Game")
26+
score = int(plr[1])
27+
break
28+
29+
user_choose = str(input("Press Enter to cont. with default options or give options\n"))
30+
if user_choose == "":
31+
values = ["rock", "paper", "scissors"]
32+
else:
33+
values = user_choose.split(",")
34+
35+
active_options = {}
36+
for key in all_losing_options:
37+
if key in values:
38+
temp = []
39+
for op in all_losing_options[key]:
40+
if op in values:
41+
temp.append(op)
42+
active_options[key] = temp
43+
44+
print("Okay, let's start")
45+
46+
47+
def save_in_txt():
48+
print(player,score)
49+
for plr in data:
50+
if plr[0] == player:
51+
plr[1] = str(score)
52+
break
53+
else:
54+
data.append([str(player),str(score)])
55+
write_data = ["{0} {1}\n".format(x1, x2) for (x1, x2) in data]
56+
with open('rating.txt', 'w') as out_file:
57+
out_file.writelines(write_data)
58+
print("Data Saved")
59+
60+
61+
while game_mode:
62+
try:
63+
system_choice = values[random.randrange(len(values))]
64+
user_choice = str(input())
65+
if user_choice == system_choice:
66+
score += 50
67+
print("There is a draw " + system_choice)
68+
elif user_choice == "!exit":
69+
game_mode = False
70+
save_in_txt()
71+
print("Bye!")
72+
break
73+
elif user_choice == "!rating":
74+
print("Your rating: ", score)
75+
elif system_choice in active_options[user_choice] :
76+
print("Sorry, but computer chose " + system_choice)
77+
elif user_choice in active_options[system_choice] :
78+
score += 100
79+
print("Well done. Computer chose " + system_choice + " and failed")
80+
except KeyError:
81+
print("Invalid input")
82+

0 commit comments

Comments
 (0)