Exemplo n.º 1
0
def both(allletters=''):
    if allletters == '':
        allletters = genletters()
    ref = player0(difficulty)
    numwords = len(ref.possible(allletters))
    biggestlength = max(len(x) for x in ref.possible(allletters))
        #logger.info(allletters + ' - number of possible words: ' +str(numwords))
        #logger.info(allletters + ' - longest word length: ' +str(biggestlength))
    g1res,g1len,b1time,r1time,fnwithpath = game(allletters,True)
    logger.info('result: '+g1res+' '+str(g1len) +' words played')
    #datetime,whowentfirst,whowon,gamelength,allletters,biggestwordlength,possiblewords,
    if g1res == 'blue':
        winner = 'player0'
    elif g1res == 'red':
        winner = 'player1'
    else:
        winner = 'tie'
    logtable.info('%s,%s,%s,%d,%d,%d,%s','player0',winner,allletters,g1len,biggestlength,numwords,fnwithpath)
    g2res,g2len,b2time,r2time,fnwithpath = game(allletters,False)
    logger.info('result: '+g2res+' '+str(g2len) +' words played')
    if g2res == 'blue':
        winner = 'player1'
    elif g2res == 'red':
        winner = 'player0'
    else:
        winner = 'tie'
    logtable.info('%s,%s,%s,%d,%d,%d,%s','player1',winner,allletters,g2len,biggestlength,numwords,fnwithpath)

    p0time = b1time+r2time
    p1time = r1time+b2time
    p0wins = 0
    p1wins = 0
    p0len = 0
    p1len = 0
    if g1res == 'blue':
        p0wins += 1
        p0len += g1len
    elif g1res == 'red':
        p1wins += 1
        p1len += g1len
    else:
        p0len += g1len
        p1len += g1len
    if g2res == 'blue':
        p1wins += 1
        p1len += g2len
    elif g2res == 'red':
        p0wins += 1
        p0len += g2len
    else:
        p0len += g2len
        p1len += g2len
    if p0wins > p1wins:
        logger.info('player0 won both games')
        return (2,0,'player0',p0time,p1time)
    elif p1wins > p0wins:
        logger.info('player1 won both games')
        return (0,2,'player1',p0time,p1time)
    else:
        temp = 'players tied 1-1. '
        if p0len < p1len:
            logger.info(temp+'however player0 won faster')
            return (1,1,'player0',p0time,p1time)

        elif p1len < p0len:
            logger.info(temp+'however player1 won faster')
            return (1,1,'player1',p0time,p1time)
        else:
            logger.info(temp+'they both played the same amount of moves to win')
            return (1,1,'tie',p0time,p1time)
##    cnt = b.count('1')
##    #cnt = sum([1<<i & map > 0 for i in range(25)])  #slower than counting the ones in the string representation
##    #x = sum([i*c for i,c in enumerate([b[row:row+5].count('1') for row in range(0,25,5)])]) / cnt
##    #y = sum([i*c for i,c in enumerate([b[col:col+21:5].count('1') for col in range(0,5)])]) / cnt
##    return (x,y)




if __name__ == '__main__':
    #pool = multiprocessing.Pool(3)

    centroidmemory = dict()

    beg = time()
    p = player0()
    for x in range(2**20):
        y = centroid(x)

    print(time()-beg,'seconds')

    #outlst = pool.map(centroid,inputlst)
    #print(time()-beg,'seconds to get outlst')

##    beg = time()
##    for x,y in zip(inputlst,outlst):
##        centroidmemory[x] = y
##    print(time()-beg,'seconds to get centroidmemory')
##    print(len(centroidmemory))
##
##    beg = time()
Exemplo n.º 3
0
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)