-
Notifications
You must be signed in to change notification settings - Fork 0
/
stone,paper,scissor.py
169 lines (125 loc) · 4.33 KB
/
stone,paper,scissor.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import json
import random
# for user input
print("""
Stone -> 0
Paper -> 1
Scissor -> 2""")
def bot():
global user_choice
try:
user_choice_str = input(">") # to get user choice of wish
if len(user_choice_str) != 1:
raise ValueError("Choice should be a single character.")
user_choice = int(user_choice_str)
if user_choice < 0 or user_choice > 2:
raise ValueError("Choice should be between 0 and 2.")
except ValueError as e:
print(f"Invalid input: {e}")
print("Try again!")
return
bot_choice = ai(user_choice)
wrong = False
# Logic
case_1 = {'won': 0, 'loss': 0} # draw
case_2 = {'won': 1, 'loss': 0} # paper
case_3 = {'won': 0, 'loss': 2} # stone
case_4 = {'won': 1, 'loss': 0} # paper
case_5 = {'won': 1, 'loss': 1} # draw
case_6 = {'won': 2, 'loss': 1} # scissor
case_7 = {'won': 0, 'loss': 2} # stone
case_8 = {'won': 2, 'loss': 1} # scissor
case_9 = {'won': 2, 'loss': 2} # draw
# only user win's in this all 9 case
if case_1['won'] == user_choice and case_1['loss'] == bot_choice:
won = 'draw'
elif case_2['won'] == user_choice and case_2['loss'] == bot_choice:
won = 'user'
elif case_3['won'] == user_choice and case_3['loss'] == bot_choice:
won = 'user'
elif case_4['won'] == user_choice and case_4['loss'] == bot_choice:
won = 'user'
elif case_5['won'] == user_choice and case_5['loss'] == bot_choice:
won = 'draw'
elif case_6['won'] == user_choice and case_6['loss'] == bot_choice:
won = 'user'
elif case_7['won'] == user_choice and case_7['loss'] == bot_choice:
won = 'user'
elif case_8['won'] == user_choice and case_8['loss'] == bot_choice:
won = 'user'
elif case_9['won'] == user_choice and case_9['loss'] == bot_choice:
won = 'draw'
# if those 9 case is not applied than the bot will always win
else:
won = 'bot'
# number in words for final result
words = {0: 'STONE', 1: 'PAPER', 2: 'SCISSOR'}
# final result
print("User choice: " + f"{words.get(user_choice)}")
print("Bot choice: " + f"{words.get(bot_choice)}")
print("--------W I N--------")
# for wrong number choice
if wrong:
won = 'Draw'
print(f" {won}")
# increment user's choice by 1 in the JSON file
adder(user_choice)
def adder(user_choice):
try:
# Read the JSON file
with open('data.json', 'r') as files:
data = json.load(files)
if user_choice == 0:
data['rock'] += 1
elif user_choice == 1:
data['paper'] += 1
elif user_choice == 2:
data['scissor'] += 1
# Write the updated JSON data to the file
with open('data.json', 'w') as file:
file.write(json.dumps(data, indent=4))
except json.JSONDecodeError as e:
print(f"Error reading JSON file: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
def ai(user_choice_here):
try:
# load json file into a python object
with open('data.json', 'r') as file:
data = json.load(file)
except json.JSONDecodeError as e:
print(f"Error: {e}")
return None
except Exception as e:
print(f"Unexpected error: {e}")
return None
rock = int(data.get("rock"))
paper = int(data.get("paper"))
scissor = int(data.get("scissor"))
data = [rock, paper, scissor]
sample_space = rock + paper + scissor
output = []
for x in range(len(data)):
# probabilty = favorable / sample_space
probability = float(data[x]) / sample_space
# then we will store in list to compare the highest
output.append(probability)
max_probability = max(output)
for i, probability in enumerate(output):
if probability == max_probability:
if i == 0:
return 0
elif i == 1:
return 1
elif i == 2:
return 2
else:
return f"{random.randrange(3)}"
try:
adder(bot.user_choice)
except Exception as e:
print(f"Unexpected error: {e}")
# for continues game play
while True:
bot()
# by kamesh who written this code Thank you!