Commit e3cc7fb5 by huangzhihao

修复了一些bug

parent 33f9d1ce
...@@ -4,7 +4,7 @@ import time ...@@ -4,7 +4,7 @@ import time
import random import random
import re import re
commdir='.' #共享目录 commdir='share' #共享目录
def cd(path): def cd(path):
return os.path.join(commdir,path) return os.path.join(commdir,path)
......
...@@ -5,11 +5,11 @@ import os ...@@ -5,11 +5,11 @@ import os
import re import re
import time import time
commdir='.' #共享目录 commdir='share' #共享目录
def cd(path): def cd(path):
return os.path.join(commdir,path) return os.path.join(commdir,path)
elo=EloRatingSystem(recordSaveFile=cd('gameRecord.txt')) #初始化 elo=EloRatingSystem(recordSaveFile='gameRecord.txt') #初始化
def askYes(str): def askYes(str):
ans='' ans=''
...@@ -26,11 +26,11 @@ def askYes(str): ...@@ -26,11 +26,11 @@ def askYes(str):
def main(): def main():
print('--------------ELO rating system---------------') print('--------------ELO rating system---------------')
if os.path.exists(cd('models.txt')): if os.path.exists('models.txt'):
print('read models from models.txt') print('read models from models.txt')
with open(cd('models.txt'),'r') as f: with open('models.txt','r') as f:
lines=f.readlines() lines=f.readlines()
models=lines models=[l.strip() for l in lines]
else: else:
models=[str(i) for i in range(300)] #模型的名字 models=[str(i) for i in range(300)] #模型的名字
print('default models:range(0,300,1) or [0,299]') print('default models:range(0,300,1) or [0,299]')
...@@ -43,12 +43,12 @@ def main(): ...@@ -43,12 +43,12 @@ def main():
if os.path.exists(cd('stop.txt')): if os.path.exists(cd('stop.txt')):
os.remove(cd('stop.txt')) os.remove(cd('stop.txt'))
if os.path.exists(cd('gameRecord.txt')): if os.path.exists('gameRecord.txt'):
print('\n game records has found!') print('\n game records has found!')
if askYes('read it? if not,will delete it and create a new one <y/n> :'): if askYes('read it? if not,will delete it and create a new one <y/n> :'):
elo.readRecords(cd('gameRecord.txt')) elo.readRecords('gameRecord.txt')
else: else:
os.remove(cd('gameRecord.txt')) os.remove('gameRecord.txt')
commandList=[r'play (\d+)','summary','stop','save'] commandList=[r'play (\d+)','summary','stop','save']
print('commandList:\n '+'\n '.join(commandList)) print('commandList:\n '+'\n '.join(commandList))
...@@ -60,7 +60,7 @@ def main(): ...@@ -60,7 +60,7 @@ def main():
elif ans=='summary': elif ans=='summary':
elo.summary() elo.summary()
elif ans=='save': elif ans=='save':
elo.save(saveFile=cd('output.txt'),sortByRank=False) elo.save(saveFile='output.txt',sortByRank=False)
else: else:
matchRes=re.match(r'play (\d+)',ans) matchRes=re.match(r'play (\d+)',ans)
if matchRes: if matchRes:
...@@ -104,7 +104,7 @@ def play(total_game): ...@@ -104,7 +104,7 @@ def play(total_game):
def read(): def read():
#读取完成的比赛记录 #读取完成的比赛记录
count=0 count=0
dir=os.listdir('comm') dir=os.listdir(cd('comm'))
for d in dir: for d in dir:
res=re.match(r'(\d+)_(\d+)_(\d+)\.finish',d) res=re.match(r'(\d+)_(\d+)_(\d+)\.finish',d)
if res: if res:
...@@ -118,7 +118,7 @@ def play(total_game): ...@@ -118,7 +118,7 @@ def play(total_game):
def exist_game(): def exist_game():
count=0 count=0
dir=os.listdir('comm') dir=os.listdir(cd('comm'))
for d in dir: for d in dir:
res=re.match(r'(\d+)_(\d+)\.running',d) res=re.match(r'(\d+)_(\d+)\.running',d)
if res: if res:
...@@ -129,13 +129,17 @@ def play(total_game): ...@@ -129,13 +129,17 @@ def play(total_game):
count+=1 count+=1
continue continue
return count return count
idx=0
while complete_game<total_game: while complete_game<total_game:
complete_game+=read() complete_game+=read()
num=exist_game() num=exist_game()
create_game(min(max_game-num,total_game-complete_game-num+10)) create_game(min(max_game-num,total_game-complete_game-num+10))
print('{}/{}'.format(complete_game,total_game)) if idx==5:
time.sleep(5) print('{}/{}'.format(complete_game,total_game))
idx=0
idx+=1
time.sleep(1)
...@@ -145,4 +149,4 @@ def play(total_game): ...@@ -145,4 +149,4 @@ def play(total_game):
if __name__ == '__main__': if __name__ == '__main__':
main() main()
\ No newline at end of file
...@@ -190,17 +190,17 @@ class EloRatingSystem: ...@@ -190,17 +190,17 @@ class EloRatingSystem:
players.sort(key=sortKey,reverse=True) players.sort(key=sortKey,reverse=True)
print('{:<10s}{:<10s}{:<10s}{:<10s}{:<10s}'.format('model','rating','games','wins','losses')) print('{:<20s}{:<10s}{:<10s}{:<10s}{:<10s}'.format('model','rating','games','wins','losses'))
for p in players: for p in players:
print('{:<10s}{:<10.2f}{:<10d}{:<10d}{:<10d}'.format(str(p.name),p.rating,p.num_game,p.wins,p.losses)) print('{:<20s}{:<10.2f}{:<10d}{:<10d}{:<10d}'.format(str(p.name),p.rating,p.num_game,p.wins,p.losses))
def save(self,saveFile='elo_rating_output.txt',sortByRank=False): def save(self,saveFile='elo_rating_output.txt',sortByRank=False):
players=self.getPlayers(sortByRank) players=self.getPlayers(sortByRank)
with open(saveFile,'w') as f: with open(saveFile,'w') as f:
f.write('{:<10s}{:<10s}{:<10s}{:<10s}{:<10s}\n'.format('model','rating','games','wins','losses')) f.write('{:<20s}{:<10s}{:<10s}{:<10s}{:<10s}\n'.format('model','rating','games','wins','losses'))
for p in players: for p in players:
f.write('{:<10s}{:<10.2f}{:<10d}{:<10d}{:<10d}\n'.format(str(p.name),p.rating,p.num_game,p.wins,p.losses)) f.write('{:<20s}{:<10.2f}{:<10d}{:<10d}{:<10d}\n'.format(str(p.name),p.rating,p.num_game,p.wins,p.losses))
def getPlayers(self,sortByRank=False): def getPlayers(self,sortByRank=False):
......
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
**2、** **2、**
​ 共享目录请到ELo_server.py和ELO_client.py中修改,默认是'.' ​ 共享目录请到ELo_server.py和ELO_client.py中修改
所有的文件都会存到这个目录 临时文件都会存到这个目录,比赛的记录gameRecord.txt和output.txt会存放在server的代码文件那个目录
**3、** **3、**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment