UI
import easygui as ui
from Learn.ruishilun import PlayCenter
class Brain:
def __init__(self):
ui.msgbox("请选择参赛选手文件,文件包含三个字段:id,name,score")
FileName = r'D:\work\2023\month11\date20\epoch.xlsx'
self.PLAY = PlayCenter(FileName)
self.currentScore = self.PLAY.printScore()
self.currentMsg = self.currentScore
self.MainLoop()
def MainLoop(self):
while True:
reply = ui.buttonbox(msg=self.currentMsg, title="main", choices=["开始一轮比赛", "结束对局"])
if reply == "开始一轮比赛":
self.StartOneRound()
self.WriteAmidithion()
elif reply == "结束对局":
self.endCurrentRound()
elif not reply:
break
def StartOneRound(self):
self.currentMsg = self.PLAY.oneRound()
self.currentBattleList = self.currentMsg.split('\n')
if(self.currentBattleList[-1] == ''): self.currentBattleList.remove('')
def endCurrentRound(self):
reply = self.PLAY.save()
ui.msgbox(reply)
def WriteAmidithion(self):
while True:
inputText = ui.multenterbox("请输入战绩", "战绩登记", self.currentBattleList)
if (inputText):
self.PLAY.addScore(inputText)
self.currentMsg = self.PLAY.printScore()
break
def ShowCurrentRank(self):
ui.msgbox(self.PLAY.printScore(), title="当前战绩", ok_button="确定")
if __name__ == '__main__':
brain = Brain()
pass
瑞士轮广场
import random
import time
import pandas as pd
from enum import Enum
class PlayCenter:
def __init__(self,FileName):
self.FileName = FileName
self.df = pd.read_excel(FileName, engine='openpyxl',sheet_name=0)
self.Init()
self.SortDf()
self.printScore()
print("初始化完成!可开始比赛!")
def Init(self):
self.df["score"] = self.df["score"].astype("int")
def SortDf(self):
self.df.sort_values(by='score', ascending=False, inplace=True,kind='stable')
self.df.reset_index(inplace=True)
self.df.drop('index',axis=1,inplace=True)
def printScore(self):
text = ''
rank = 1
for index,line in self.df.iterrows():
text += "名次:{0}--{2}--积分:{3}".format(rank,line["id"],line["name"],line["score"]) + "\n"
rank += 1
return text
def oneRound(self):
sublists = []
for i in range(0, len(self.df), 2):
sublist = list(self.df.index)[i:i + 2]
sublists.append(sublist)
text = ''
for epoch in sublists:
if(len(epoch)>1):
text += "ID:{0} 姓名:{1} vs ID:{2} 姓名:{3}".format(self.df.iloc[epoch[0]]["id"], self.df.iloc[epoch[0]]["name"],
self.df.iloc[epoch[1]]["id"],
self.df.iloc[epoch[1]]["name"]) + '\n'
else:
text += "ID:{0} 姓名:{1} vs 轮空".format(self.df.iloc[epoch[0]]["id"], self.df.iloc[epoch[0]]["name"]) + '\n'
return text
def addScore(self,WinIDList):
'''
:return:
'''
WinIDList = list(set(WinIDList))
if(WinIDList.__contains__('')): WinIDList.remove('')
WinIDList = [int(x) for x in WinIDList]
for index,line in self.df.iterrows():
if(line["id"] in WinIDList):
line["score"] += 3
self.df.loc[index] = line
self.SortDf()
def save(self):
'''
保存表格
:return:
'''
curTime = time.strftime("%m月%d日 %H时%M分%S秒", time.localtime())
with pd.ExcelWriter(self.FileName,mode='a',engine='openpyxl') as writer:
self.df.to_excel(writer, sheet_name="{}".format(curTime),
index=False)
writer.save()
return "输出完成,页名:{}".format(curTime)
if __name__ == '__main__':
FileName = r'D:\work\2023\month11\date20\epoch.xlsx'
Play = PlayCenter(FileName)
pass