为完成课程设计写的比较糙
仅实现基本要求
题目
24点游戏是一种使用扑克牌来进行的益智类游戏。游戏内容是从一
副扑克牌中抽去大小王剩下52张,任意抽取4张牌,把牌面上的数运
用加、减、乘、除和括号等进行运算得出24。每张牌都必须使用一
次,但不能重复使用,在本设计题目中,可以只选取在1-9这九张牌中
随机取牌,降低软件开发的难度。
数据库使用mysql
语言python3
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
username | char(10) | YES | NULL | ||
passwd | char(10) | YES | NULL | ||
score | int(10) | YES | NULL |
比较简单详细说了
import socket
import pymysql
def select(list_name, user_name): # 查询用
sql = "SELECT %s FROM user WHERE username = '%s'" % (list_name, user_name)
try:
cursor.execute(sql)
results = cursor.fetchall()
data = results[0][0]
return data
except:
print("Error: unable to fetch data")
def login(user_name, passwd):
passwd_sql = select('passwd', user_name)
if passwd_sql == passwd:
return 1
else:
return 0
def update(user_score, user_name): # 更新分数
sql = "UPDATE user SET score='%s' WHERE username='%s'" % (user_score, user_name)
print(sql)
try:
cursor.execute(sql)
db.commit()
return 1
except:
return 2
def paihang():
return_str, num = '', 1
sql = "SELECT * FROM user order by score desc"
cursor.execute(sql)
results = cursor.fetchall()
# print(results, results[0])
for i in results:
if (i[2] != None):
print(i)
return_str += str(num) + ' ' + str(i[0]) + ' ' + str(i[2]) + '\n'
num += 1
return return_str
if __name__ == "__main__":
# 数据库实例化
db = pymysql.connect("192.168.87.128", "root", "152131", "user")
cursor = db.cursor()
# socket实例化
s = socket.socket()
s.bind(('', 8885))
s.listen(5)
conn, addr = s.accept()
#等待接收用户名密码
str_recv = conn.recv(1024)
user_passwd = str_recv.decode()
username, passwd = user_passwd.split(sep=' ')
login_bool = login(username, passwd)
if login_bool == 1:
conn.send('1'.encode("gbk"))
else:
conn.send('0'.encode("gbk"))
#接收成绩并更新
str_recv = conn.recv(1024)
score = str_recv.decode()
if select(score)
update(score, username)
#发送排行榜
pai = paihang()
conn.send(pai.encode("gbk"))
db.close()
from tkinter import *
from socket import *
import random
import time
e, time_1, root, root1, root2, window = 0, 0, 0, 0, 0, 0
def score(formula, user_time):
a = eval(formula)
if a == 24:
return_score = 75 - (user_time / 120) * 75 + 25
if return_score <= 0:
return 0
else:
return return_score
else:
return 0
def send():
global e, time_1, root, root2, window
root2 = Tk()
text = e.get()
time_2 = time.time() - time_1
time_2 = int(time_2)
sc = score(text, time_2)
sc = int(sc)
xianshi = '您的分数是%s!\n' % sc
sc = str(sc)
tcp_socket_client.send(sc.encode('gbk'))
data_re = tcp_socket_client.recv(1024)
data_re = data_re.decode()
xianshi = xianshi + '排行榜:\n'+data_re
Label(root2, text=xianshi, bg='red', fg='blue', width=30, height=8, anchor='c').pack()
def random_24():
return_list = []
list_1 = [i for i in range(1, 14)]
list_1 = list_1 + list_1 + list_1 +list_1
for j in range(4):
return_list.append(list_1[random.randint(0, 51)])
return return_list
def game():
global e, time_1, root, root2, window
window = Tk()
window.title('1')
window.geometry('200x300')
list_24 = random_24()
for i in range(4):
Label(window, text=list_24[i],
bg='green',
font=('Arial', 12),
width=6,
height=2).grid(row=i, column=1, padx=10, pady=10)
e = Entry(window)
e.grid(row=4, column=1)
time_1 = time.time()
Button(window, text='提交', anchor='c', width=6, height=1, command=send).grid(row=5, column=1)
Button(window, text='提交', anchor='c', width=6, height=1, command=send).grid(row=5, column=1)
def Show():
global e, time_1, root, root2, window
root1 = Tk()
username = En.get()
passwd = En1.get()
loginstr = username + ' ' + passwd
tcp_socket_client.send(loginstr.encode('gbk'))
data_re = tcp_socket_client.recv(1024)
if data_re.decode('gbk') == '1':
# if '1' == '1':
Label(root1, text='登录成功', bg='Gold', fg='blue', width=30, height=8, anchor='c').pack()
game()
else:
Label(root1, text='用户名或密码错误!', bg='red', fg='blue', width=30, height=8, anchor='c').pack()
# 建立TCP连接
tcp_socket_client = socket(AF_INET, SOCK_STREAM)
tcp_socket_client.connect(('127.0.0.1', 8885))
# GUI
root = Tk()
root.title('24点')
label = Label(root, text='用户名:', anchor='c').grid(row=0)
En = Entry(root)
En.grid(row=0, column=1)
label1 = Label(root, text='密码 :', anchor='c').grid(row=1)
En1 = Entry(root, show='*')
En1.grid(row=1, column=1)
Button(root, text='退出', anchor='c', width=6, height=1, command=root.quit).grid(row=2, column=0)
Button(root, text='确定', anchor='c', width=6, height=1, command=Show).grid(row=2, column=1)
root.mainloop()