-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.py
167 lines (154 loc) · 5.35 KB
/
rpc.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
import random
import os
import time
import json
import socket
choices = ["rock", "paper", "scissor"]
if not os.path.exists(".config"):
os.makedirs(".config")
def save_config(scores, filename):
with open(f'.config/{filename}', 'w') as file:
json.dump(scores, file, indent=4)
def load_local_scores():
if not os.path.exists('.config/local_scores.json'):
return {
"computer": 0,
name: 0,
}
else:
with open('.config/local_scores.json', 'r') as file:
return json.load(file)
def load_multiplayer_scores():
if not os.path.exists('.config/multiplayer_scores.json'):
return {
name: 0,
"opponent": 0
}
else:
with open('.config/multiplayer_scores.json', 'r') as file:
return json.load(file)
if not os.path.exists(".username.rpc"):
name = input("Enter Your Username: ")
f=open(".username.rpc", "w")
f.write(name)
else:
file=open(".username.rpc", "r")
name = file.read()
local_scores = load_local_scores()
multiplayer_scores = load_multiplayer_scores()
def game_logic():
print("1. Single Player\n2. Multiplayer\n3. Scores\n4. Exit")
player_select = input(": ")
if player_select == "1":
local_play()
elif player_select == "2":
start_multiplayer()
elif player_select == "3":
display_scores()
elif player_select == "4":
print("GOODBYE!")
exit()
else:
print("Wrong input")
game_logic()
def local_play():
user_choice = input("enter Rock/Paper/Scissor: ").lower()
computer_choice = random.choice(choices)
time.sleep(0.25)
print(f"You: {user_choice}\nComputer: {computer_choice}")
time.sleep(0.25)
if user_choice == computer_choice:
print("Tie")
elif user_choice == "rock" and computer_choice == "scissor":
print("You Win")
local_scores[name] += 1
elif user_choice == "paper" and computer_choice == "rock":
print("You Win")
local_scores[name] += 1
elif user_choice == "scissor" and computer_choice == "paper":
print("You Win")
local_scores[name] += 1
else:
print("You Lose")
local_scores["computer"] += 1
save_config(local_scores, "local_scores.json")
question = input("Play Again? (Y/N): ").lower()
if question == "y":
local_play()
else:
game_logic()
def start_multiplayer():
question = input("1. Create Room\n2. Join Room\n: ")
if question == "1":
host = "0.0.0.0"
port = 12345
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen()
print("Waiting for player 2 to connect...")
conn, addr = sock.accept()
print("Player 2 connected!")
while True:
player1_choice = input("Enter Rock/Paper/Scissor: ").lower()
conn.send(player1_choice.encode())
player2_choice = conn.recv(1024).decode()
print(f"Player 2: {player2_choice}")
if player1_choice == player2_choice:
print("Tie")
elif player1_choice == "rock" and player2_choice== "scissor":
print("You Win")
multiplayer_scores[name] += 1
elif player1_choice == "paper" and player2_choice == "rock":
print("You Win")
multiplayer_scores[name] += 1
elif player1_choice == "scissor" and player2_choice == "paper":
print("You Win")
multiplayer_scores[name] += 1
else:
print("You Lose")
multiplayer_scores["opponent"] += 1
save_config(multiplayer_scores, "multiplayer_scores.json")
elif question == "2":
host = input("Enter host ip address: ")
port = 12345
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print("Connected to player 1!")
while True:
player2_choice = input("Enter Rock/Paper/Scissor: ").lower()
sock.send(player2_choice.encode())
player1_choice = sock.recv(1024).decode()
print(f"Player 1: {player1_choice}")
if player2_choice == player1_choice:
print("Tie")
elif player2_choice == "rock" and player1_choice== "scissor":
print("You Win")
multiplayer_scores[name] += 1
elif player2_choice == "paper" and player1_choice == "rock":
print("You Win")
multiplayer_scores[name] += 1
elif player2_choice == "scissor" and player1_choice == "paper":
print("You Win")
multiplayer_scores[name] += 1
else:
print("You Lose")
multiplayer_scores["opponent"] += 1
save_config(multiplayer_scores, "multiplayer_scores.json")
else:
print("Wrong Input")
start_multiplayer()
def display_scores():
print("1. Local Scores\n2. Multiplayer scores\n3. Back")
ans = input(": ")
if ans == "1":
print(local_scores)
display_scores()
elif ans == "2":
print(multiplayer_scores)
display_scores()
elif ans == "3":
game_logic()
else:
print("Wrong input")
display_scores()
game_logic()