def do_generation(competitors, boards): comp_scores = dict() comp_objs = dict() for p in competitors: comp_scores[p] = 0 comp_objs[p] = player1(difficulty=d, weights=p) pairs = list(combinations(competitors,2)) pairs_obj = [(comp_objs[x], comp_objs[y]) for (x,y) in pairs] for board in boards: boardzip = [board]*len(pairs) args = list(zip(boardzip, pairs_obj)) outlst = pool.map(run_both,args) for out in outlst: p1 = out[0] p1s = out[1] p2 = out[2] p2s = out[3] comp_scores[p1] += p1s comp_scores[p2] += p2s logger.info(comp_scores) return comp_scores
def game(allletters='',player0blue=False): if allletters == '': allletters = genletters() logger.info(allletters) allletters = allletters.upper() filename = '' if player0blue: b = player0(difficulty) logger.debug('player0 plays blue') r = player1(difficulty) logger.debug('player1 plays red') filename = strftime('%Y_%m_%d_%H_%M_%S_b0r1.cgd') else: b = player1(difficulty) logger.debug('player1 plays blue') r = player0(difficulty) logger.debug('player0 plays red') filename = strftime('%Y_%m_%d_%H_%M_%S_b1r0.cgd') datadir = os.path.dirname(inspect.stack()[0][1]) +os.sep+'tests' fnwithpath = os.path.join(datadir, filename) logger.info(fnwithpath) saveList = list() saveDict = dict() b.cache = dict() r.cache = dict() b.possible(allletters) r.possible(allletters) turn = 'blue' board = '----- ----- ----- ----- -----' saveList.append(('I001','[Initial Position]','',board.replace(' ',''), allletters, '')) playedwords = list() bluePassed = redPassed = False btime = rtime = 0 score = 0 early = False while board.find('-') != -1: if turn == 'blue': start = time() oldscore = score word,board,score = b.turn(allletters,board,1) blue,blued,red,redd = numscore(board) playedwords.append(word) r.playword(allletters,word) t = round(time() - start,2) btime += t logger.debug(' '.join(('blue plays',word.ljust(25),board,str(len(playedwords)),'blue:',blue+'('+str(blued)+')','red:',red+'('+str(redd)+')','time:',str(t),'seconds',str(round(score,4))))) if word == '': bluePassed = True else: bluePassed = False num = len(playedwords)+1 saveList.append(('I%03d' % num,word,round(score,4),board.replace(' ',''),allletters, turn)) turn = 'red' else: start = time() oldscore = score word,board,score = r.turn(allletters,board,-1) blue,blued,red,redd = numscore(board) playedwords.append(word) b.playword(allletters,word) t = round(time() - start,2) rtime += t logger.debug(' '.join((' red plays',word.ljust(25),board,str(len(playedwords)),'blue:',blue+'('+str(blued)+')','red:',red+'('+str(redd)+')','time:',str(t),'seconds',str(round(score,4))))) if word == '': redPassed = True else: redPassed = False num = len(playedwords)+1 saveList.append(('I%03d' % num,word,round(score,4),board.replace(' ',''),allletters, turn)) turn = 'blue' if len(playedwords) > 100 and ((oldscore < 0 and score < 0) or (oldscore > 0 and score > 0)): early = True break if bluePassed and redPassed: break saveDict['history'] = saveList saveDict['letters'] = allletters saveDict['colors'] = '-'*25 saveDict['selected'] = 'I001' f = open(fnwithpath,'wb') pickle.dump(saveDict,f) f.close() if not early: if blue > red: logger.debug('Blue wins!') return ('blue', len(playedwords), btime, rtime, fnwithpath) elif red > blue: logger.debug('Red wins!') return ('red', len(playedwords), btime, rtime, fnwithpath) else: logger.debug('Tie') return ('', len(playedwords), btime, rtime, fnwithpath) else: if score > 0: logger.debug('Blue wins!') return ('blue', len(playedwords), btime, rtime, fnwithpath) else: logger.debug('Red wins!') return ('red', len(playedwords), btime, rtime, fnwithpath)