Beispiel #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()
Beispiel #2
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)
Beispiel #3
0
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)
Beispiel #4
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)
Beispiel #5
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