예제 #1
0
 def default(self, c):
     # c is the chosen move, else an error
     # c should be in the range A-F
     c = c.upper()
     opts = ['A', 'B', 'C', 'D', 'E', 'F']
     try:
         move = s.translateMove(self.game.gamestate, opts.index(c))
         self.game.priorstate = self.game.gamestate[:]
         self.game.gamestate = s.doMove(self.game.gamestate, move)
         if s.isGameOver(self.game.gamestate):
             raise EndGame(self.game.gamestate)
     except s.InvalidMove:
         print("That is not a legal move.")
         return
     except s.InvalidPlayer:
         print("You're not valid.")
         return
     except s.InvalidIndex as e:
         print("That index is invalid.")
         raise e
         return
     except ValueError:
         print("You must choose one of the six bowls for your move "
               "(letters A-F).")
         return
     except Exception as e:
         raise e
         return
     self.game.betweenMoves()
     self.setPrompt()
예제 #2
0
파일: aiplay.py 프로젝트: stobra/mancalai
def play_game(players):
    game = s.init()
    done = False
    moves = []
    while not done:
        # do move for someone
        player = s.getCurrentPlayer(game)
        move = players[player]['ai'].move(game)
        if move is None:
            print("null move! ", game)
        mt = {
            "move": s.flipMove(move, player),
            "board": s.flipBoardCurrentPlayer(game),
            "player": player,
            "name": players[player]['module'].__name__
        }
        moves.append(mt)
        game = s.doMove(game, move)
        done = s.isGameOver(game)
    winner = s.getWinner(game)
    # make training set with move, gamestate, and 1 for win, 0 for lose
    trainingset = [dict(d, winner=int(winner == d['player'])) for d in moves]
    i = 0
    for p in players:
        p['ai'].gameOver(i == winner)
        i += 1
    return (winner, trainingset)
예제 #3
0
def play_one_game(players, lucky):
    game = s.init()
    done = False
    moves = []
    while not done:
        # do move for someone
        player = s.getCurrentPlayer(game)
        if needRandomMove(len(moves)):
            move = lucky.move(game)
        else:
            move = players[player]['ai'].move(game)
        if move is None:
            logger.error("null move! ", game)
        mt = [s.flipBoardCurrentPlayer(game), s.flipMove(move, player), player]
        moves.append(mt)
        game = s.doMove(game, move)
        done = s.isGameOver(game)
    winner = s.getWinner(game)
    score = s.getScore(game)
    # make training set with move, gamestate, and 1 for win, 0 for lose
    trainingset = [
        d[0:2] + [int(winner == d[2])] + list(score)[::1 - d[2] * 2]
        for d in moves
    ]
    for move in trainingset:
        results.info(move)
    i = 0
    for p in players:
        isWinner = (1 if i == winner else 0)
        p['ai'].gameOver(isWinner)
        p['wins'] += isWinner
        i += 1
    return (winner, trainingset)
예제 #4
0
def genMoves(state, chain=[]):
    """
    List possible moves. Moves are a list of moves, because some moves
    result in the player getting to move again.
    >>> genMoves([1, 2, 4, 4, 5, 6, 0, 12, 11, 10, 9, 8, 7, 0, 0])
    ([[0], [1], [2, 0], [2, 1], [2, 3], [2, 4], [2, 5], [3], [4], [5]], 11)
    >>> genMoves([0, 2, 0, 0, 1, 5, 17, 0, 0, 0, 0, 0, 1, 20, 1])
    ([[12]], 1)
    """
    movecount = 0
    player = s.getCurrentPlayer(state)
    new = s.getLegalMoves(state)
    if len(new) == 0:
        # no legal moves, so game is over: return move chain so far
        result = [chain] if len(chain) > 0 else []
    else:
        result = []
        for m in new:
            movecount += 1
            newstate = s.doMove(state, m)
            currentChain = chain + [m]
            if player == s.getCurrentPlayer(newstate):
                # still our move after move m, so recurse and grow the chain
                r, moves = genMoves(newstate, currentChain)
                movecount = movecount + moves
                # r is list of move chains we generated, so merge lists
                result = result + r
            else:
                # no more moves in this chain, add it to result list
                result = result + [currentChain]
    return (result, movecount)
예제 #5
0
def play_game(*players):
    game = s.init()
    done = False
    while not done:
        player = s.getCurrentPlayer(game)
        move = players[player].move(game)
        game = s.doMove(game, move)
        done = s.isGameOver(game)
    return s.getWinner(game)
예제 #6
0
def applyMove(node, moveseq):
    """
    >>> state = [1, 2, 4, 4, 5, 6, 0, 12, 11, 10, 9, 8, 7, 0, 0]
    >>> applyMove(state, [2, 3])
    [1, 2, 0, 0, 7, 8, 2, 13, 12, 10, 9, 8, 7, 0, 1]
    """
    child = node[:]
    for move in moveseq:
        child = s.doMove(child, move)
    return child
예제 #7
0
def makeVector(state, m=None, isWinner=None, myscore=None, oppscore=None):
    """
    Create a vector of scores for winning moves and losing moves. Treat any
    illegal move as a losing move.

    >>> state = [1, 2, 3, 4, 5, 6, 0, 12, 11, 10, 9, 8, 7, 0, 0]
    >>> makeVector(state, 0, 1)
    [101, 1, 1, 2, 2, 2]

    >>> state = [1, 2, 3, 0, 5, 6, 0, 12, 11, 7, 9, 8, 7, 0, 0]
    >>> makeVector(state, 2, 0)
    [1, 9, 0, 0, 2, 2]

    >>> state = [1, 2, 3, 0, 5, 6, 0, 12, 11, 7, 9, 8, 7, 0, 0]
    >>> makeVector(state)
    [1, 9, 1, 0, 2, 2]
    """
    score_now = s.getScore(state)
    vector = ([0] * 6)[:]
    for move in range(6):
        if s.isLegalMove(state, move):
            # legal, so start at 1
            vector[move] = 1
            newstate = s.doMove(state, move)
            score_new = s.getScore(newstate)
            if s.getCurrentPlayer(newstate) == s.getCurrentPlayer(state):
                vector[move] += SCORE_MOVE_AGAIN
            vector[move] += SCORE_CAPTURE * (score_new[0] - score_now[0])
            if move == m:
                if isWinner:
                    vector[move] += SCORE_WINNER_MOVE
                else:
                    vector[move] -= SCORE_LOSER_MOVE
        else:
            # illegal, always zero
            vector[move] = 0
        vector[move] = max(0, vector[move])
    return vector[:6]
예제 #8
0
 def betweenMoves(self):
     current = s.getCurrentPlayer(self.gamestate)
     module = self.player_algorithm.get(current)
     isHumanNext = True if module is None else False
     while not isHumanNext:
         if s.isGameOver(self.gamestate):
             raise EndGame(self.gamestate)
         current = s.getCurrentPlayer(self.gamestate)
         module = self.player_algorithm.get(current)
         isHumanNext = True if module is None else False
         if not isHumanNext:
             try:
                 d.drawState(self.gamestate, self.priorstate)
                 cprint("My move!", PLAYER_COLORS[current])
                 move = module.move(self.gamestate)
                 self.priorstate = self.gamestate[:]
                 self.gamestate = s.doMove(self.gamestate, move)
                 cprint(module.taunt(), PLAYER_COLORS[current])
             except s.NoMoves as n:
                 cprint("Oops! I have no moves!", PLAYER_COLORS[current])
                 raise EndGame(self.gamestate)
             except Exception:
                 raise NoAI(current)
     pass