杭电胡老师的实验三:
要求:
通过在远端主机上搭建一个远程骰宝服务器,其它主机可以通过客户端程序
RemoteBet与远程的骰宝服务器联系,进行交互式游戏设计。命令行格式如下:
RemoteBet
如: RemoteBet 127.0.0.1
规则如下:
ya tc <数量> <coin|silver|gold> 押头彩(两数顺序及点数均正确) 一赔三十五
ya dc <数量> <coin|silver|gold> 押大彩(两数点数正确) 一赔十七
ya kp <数量> <coin|silver|gold> 押空盘(两数不同且均为偶数) 一赔五
ya qx <数量> <coin|silver|gold> 押七星(两数之和为七) 一赔五
ya dd <数量> <coin|silver|gold> 押单对(两数均为奇数) 一赔三
ya sx <数量> <coin|silver|gold> 押散星(两数之和为三、五、九、十一) 一赔二
每盘按从上到下的顺序只出现一种点型(头彩和大彩可同时出现),其他情况都算庄家赢。
庄家唱道:新开盘!预叫头彩!
庄家将两枚玉骰往银盘中一撒。
┌───┐ ┌───┐
│● ● │ │ │
│● ● │ │ ● │
│● ● │ │ │
└───┘ └───┘
庄家唱道:头彩骰号是六、一!
输入你压的值:
ya tc 10 gold
庄家将两枚玉骰扔进两个金盅,一手持一盅摇将起来。
庄家将左手的金盅倒扣在银盘上,玉骰滚了出来。
┌───┐
│● ●│
│ ● │
│● ●│
└───┘
庄家将右手的金盅倒扣在银盘上,玉骰滚了出来。
┌───┐
│ ● │
│ │
│ ● │
└───┘
庄家叫道:五、二……七星。
你赢了多少?or 你输了多少?
假设服务器的IP地址为127.0.0.1,客户端程序连接到服务器进行远程骰宝游戏,然后按照服务器发回的游戏规则和提示进行游戏。
·
·
·
·
·
我用协程做的,使用时要导入gevent
这个包
游戏服务器:
"""
@author: Bre Athy
@contact: https://www.zhihu.com/people/you-yi-shi-de-hu-xi
@productware: PyCharm
@file: 游戏服务器.py
@time: 2019/11/10 23:40
"""
import socket,json,random,gevent
from gevent import monkey
users=[]
# 初始钱币
thecoin=thesilver=thegold=30
def service_client(new_socket):
def sendToClient(dict_info):
new_socket.send(json.dumps(dict_info).encode("utf-8"))
# 接收请求
while True:
recv_data=new_socket.recv(1024).decode("utf-8")
data=json.loads(recv_data)
print(data)
dice1, dice2 = random.randint(1, 6), random.randint(1, 6)
if data['alt']=="游戏开始":
uid=len(users)
user={
"uid":uid,"coin":thecoin,"silver":thesilver,"gold":thegold,'dice1':dice1,'dice2':dice2}
users.append({
"uid":uid,"money":thecoin+thesilver*100+thegold*10000,"dice1":dice1,"dice2":dice2})
sendToClient(user)
elif data['alt']=="压注":
bet=True
if data['type']=='tc' and dice1==data['dice1'] and dice2==data['dice2']:users[data["uid"]]["money"]+=data['money']*35
elif data['type']=='dc' and ((dice1==data['dice1'] and dice2==data['dice2']) or (dice1==data['dice2'] and dice2==data['dice1'])):users[data["uid"]]["money"]+=data['money']*17
elif data['type']=='kp' and (dice1!=dice2 and dice1%2==0 and dice2%2==0):users[data["uid"]]["money"]+=data['money']*5
elif data['type']=='qx' and dice1+dice2==7 :users[data["uid"]]["money"]+=data['money']*5
elif data['type']=='dd' and dice1%2==1 and dice2%2==1