写了几遍的python源码(考虑到时间因素 没有增加server和client 功能): (阶段I代码如下:实现两轮自动按不同键值筛选和复活赛传递. python 已经算是代码简化之王了,处理起赛程代码来还是要超出上百行,杯具吖,不过既然是第一次拿python写.py源程序 缺乏经验 原谅自己了。
import random import string import msvcrt import sys
class player: name="" num=0 totalWin=0 compNum=0 sex=0 #'0' is female,'1' is male ScoreList=range(15) RivalList=range(15) def __init__(self,_name,_num,_sex): self.name=_name self.num=_num self.sex=_sex def Record(self,score,RivalNum): self.compNum=self.compNum+1 self.ScoreList[self.compNum]=score self.RivalList[self.compNum]=RivalNum if(score>0): self.totalWin=self.totalWin+1
def getPlayersFromFile(path): """To get the students-list of the path"""
str = file(path).read() str = str.replace("\n",";") num = str.count(";")+1 str = str.split(";") list = [elem.split(",") for elem in str ] if list[-1]==['']: num=num-1 del list[-1]
playerList=[player(list[0][1],string.atoi(list[0][0]),string.atoi(list[0][2]))] for i in range(1,num): playerList.append(player(list[i][1],string.atoi(list[i][0]),string.atoi(list[i][2]))) return (playerList,num)
def swap(a,b): return (b,a)
class competition: playerList = [] winPlayerList = [] winNum=0 winNumberList=[] failPlayerList = [] failNum=0 failNumberList=[] th=0 playerNumber=0 directUp =-1 def __init__(self,_playerList,_num,_index): self.playerList=_playerList self.playerNumber=_num self.th=_index
def setDirectUp(self): if(self.playerNumber%2==0): print "move!" return 1 b=0 for i in self.playerList: if(i.sex==0): b=b+1 else: break if(b%2!=0): print "!!!!" self.directUp=b-1 else: #print "move!" self.directUp=self.playerNumber-1
def setRival(self,isRandom=1,index=1):# index(1,2)=(sex,totalWin) if(isRandom==1): random.shuffle(self.playerList) k=0
for i in range(1,self.playerNumber): if(index==1): while((k<self.playerNumber and k<i)and(self.playerList[i].sex==0)): if(self.playerList[k].sex==1): (self.playerList[i],self.playerList[k])=swap(self.playerList[i],self.playerList[k]) k=k+1 break k=k+1 if(index==2): while(k<self.playerNumber): if(self.playerList[k].totalWin>self.playerList[i].totalWin): (self.playerList[i],self.playerList[k])=swap(self.playerList[i],self.playerList[k]) k=k+1 break k=k+1 self.setDirectUp() def search(self,i): for e in range(self.playerNumber): if(self.playerList[e].num==i): return self.playerList[e] return 0 def findList(self,list,value): for i in list: if(value==i): return 1 return 0 def setResult(self,winNumList): k=[] f=[] if(self.directUp!=-1): winNumList.append(self.playerList[-1].num) #print "%d:",self.playerList[self.directUp].num self.winNumberList=winNumList for i in winNumList: m=self.search(i) if(not m): print "!!-" return 0 else: k.append(m) numList=[] for i in self.playerList: numList.append(i.num) for i in numList:
v=self.findList(winNumList,i) if(not v): m=self.search(i) if(m): f.append(m) self.failNumberList.append(m) else: print "\nERROR In Finding %d!!!!!"% i self.winPlayerList=k self.failPlayerList=f self.winNum=self.playerNumber/2+self.playerNumber%2 self.failNum=self.playerNumber/2 return 1
def stoList(list): t=0
t=list.count(',')+1 list=list.split(",") listM=[] for i in range(t): listM.append(string.atoi(list[i])) return listM
def printResult(comp): print "\nWinners:" for i in comp.winPlayerList: print "%02d,%s" %(i.num,\ i.name) print "\nTBA:" for i in comp.failPlayerList: print "%02d,%s" %(i.num,\ i.name)
def isInList(list,i):#whether i in list for e in list: if(e==i): return 1 return 0 def belongList(list,subList):#whether subList belongs to list for e in subList: if(not isInList(list,e)): return 0 return 1
def runComp(comp,p,num,th): #print Init Player List comp = competition(p,num,th) comp.setRival(0,1) te=0 #print "!!%d"%comp.directUp if(comp.directUp!=-1): te=comp.playerList[comp.directUp] comp.playerList.append(te) del(comp.playerList[comp.directUp]) print "\n\nInit Player List for [%f]Group\n" % comp.th for i in range(0,comp.playerNumber-1,2):
print "(%02d,%s) - (%02d,%s)" %(comp.playerList[i].num,comp.playerList[i].name,\ comp.playerList[i+1].num,comp.playerList[i+1].name)
if(comp.directUp!=-1): print "direct promotion:(%02d,%s)" %(comp.playerList[comp.playerNumber-1].num,\ comp.playerList[comp.playerNumber-1].name) return comp
def runCompResult(comp): list="" #while(1): print "Please enter[%d] winner's numbers such as '5,7,2,9,11' "%comp.th sys.stdout.write('>>>') list=raw_input() """print "Please enter ditto again for assurance:" sys.stdout.write('>>>') list2=raw_input()
if((list==list2) and belongList(comp.winNumberList,list) ): print "Correct." break print "Your two inputs aren't equal,please enter again! " """ list=stoList(list) comp.setResult(list) printResult(comp)
if __name__ == "__main__":
(p,num)=getPlayersFromFile("g:\\cs_1.log") comp1=0 comp2=0 comp3=0 comp4=0 comp5=0 comp6=0 comp7=0 comp8=0 comp9=0
comp1=runComp(comp1,p,num,1) runCompResult(comp1)
print "%d,%d"%(comp1.winNum, comp1.failNum) comp2=runComp(comp2,comp1.winPlayerList,comp1.winNum,2.1) comp3=runComp(comp3,comp1.failPlayerList,comp1.failNum,2.2) runCompResult(comp2) runCompResult(comp3) #comp4=competition(comp2.winPlayerList,comp2.winNum,4.1)#jump to directly 4.1th
comp5=runComp(comp5,comp2.failPlayerList,comp2.failNum,3.1) comp6=runComp(comp6,comp3.winPlayerList,comp3.winNum,3.2) comp7=runComp(comp7,comp3.failPlayerList,comp3.failNum,3.3)
runCompResult(comp5) runCompResult(comp6) runCompResult(comp7)
comp7=runComp(comp7,comp7.winPlayerList,comp7.winNum,3.32) runCompResult(comp7)
comp8=runComp(comp8,comp2.winPlayerList+comp5.winPlayerList+comp6.winPlayerList+comp7.winPlayerList,\ comp2.winNum+comp5.winNum+comp6.winNum+comp7.winNum,4.1)
runCompResult(comp8)
j=0 while(1): if(comp8.winNum>=2): comp9=runComp(comp9,comp8.winPlayerList,comp8.winNum,5+j+0.1) runCompResult(comp9) comp8=comp9 j=j+1 else: break
已经用于实际班级乒乓球比赛,缺点如下: 1 每一大轮一定要等到所有人都比赛完才开始随机下一轮选手,导致先比完的人只能等待而不能提前进入下一轮预先随机。 2 没有提供临场替换选手和取消选手的功能,导致中场退出的人只能被假设处理。 但都不是运行时致命缺点。
|