Exemple #1
0
class Game:
    gameState = None
    player1 = None
    player2 = None

    def __init__(self, player1, player2):
        self.player1 = player1
        self.player2 = player2

    def play(self):
        self.gameState = State()

        while not self.gameState.game_over():
            player1Action = self.player1.choose_action(self.gameState)
            self.gameState.execute(player1Action)
            util.pprint(self.gameState)

            if self.gameState.game_over():
                break

            player2Action = self.player2.choose_action(self.gameState)
            self.gameState.execute(player2Action)
            util.pprint(self.gameState)

        return self.gameState.winner()
Exemple #2
0
def heuristic(state: State = None, playerchar: str = None): # -> a number boi
    if state.game_over():
        if state.winner() == playerchar:
            return float("inf")
        elif state.winner() != None: # Opponent wins
            return -1 * float("inf")
        else:
            return 0
    score = 0
    # number of player's pieces adjacent to open spaces
    # - number of opponent's pieces adjacent to open spaces
    for col in range(4):
        for row in range(3):
            if (piece := state.get(col, row)) and piece == " ":
                deltas = [-1, 0, +1]
                for dx in deltas:
                    for dy in deltas:
                        if None == (adjacent_piece := state.get(col+dx, row+dy)):
                            continue
                        if adjacent_piece == playerchar: # player's piece
                            score += 1
                        elif adjacent_piece != " ": # opponent's piece
                            score -= 1
                        else: # empty
                            score += 0