--- a/envs/connect4_env/server/connect4_environment.py +++ b/envs/connect4_env/server/connect4_environment.py @@ -40,25 +40,31 @@ def step(self, action: Connect4Action): col = action.column - # reward = 0.0 done = False + reward = 0.0 # check action validity if col < 0 or col >= self.COLUMNS or self.board[0, col] != 0: self.invalid_move_played = True - reward = -1 # penalty for invalid move + reward = -1.0 # penalty for invalid move done = True else: - # drop piece - for row in range(self.ROWS - 1, -1, -1): - if self.board[row, col] == 0: - self.board[row, col] = self.next_player - break - - # check win / full board - reward, done = self._check_win_or_draw(row, col) - - self.next_player *= -1 + # Agent is always player 1. + agent_row = self._drop_piece(col, 1) + reward, done = self._check_win_or_draw(agent_row, col) + + # Let the opponent respond when the game is still in progress. + if not done: + opponent_col = self._choose_opponent_move() + if opponent_col is None: + reward = 0.0 + done = True + else: + opponent_row = self._drop_piece(opponent_col, -1) + reward, done = self._check_win_or_draw(opponent_row, opponent_col) + + # The agent moves first again on the next turn. + self.next_player = 1 self._state = Connect4State( board=self.board.copy().tolist(), @@ -79,27 +85,84 @@ metadata={"next_player": self.next_player}, ) - def _check_win_or_draw(self, row, col): - # Implement 4-in-a-row check (like your Gymnasium code) + def _drop_piece(self, col, player): + for row in range(self.ROWS - 1, -1, -1): + if self.board[row, col] == 0: + self.board[row, col] = player + return row + return -1 + + def _find_drop_row(self, col): + for row in range(self.ROWS - 1, -1, -1): + if self.board[row, col] == 0: + return row + return -1 + + def _is_four(self, row, col): player = self.board[row, col] directions = [(1, 0), (0, 1), (1, 1), (1, -1)] for dr, dc in directions: - count = 0 - for step in range(-3, 4): - r, c = row + step * dr, col + step * dc - if ( - 0 <= r < self.ROWS - and 0 <= c < self.COLUMNS - and self.board[r, c] == player - ): - count += 1 - if count >= 4: - return 1.0, True - else: - count = 0 + count = 1 + for step in (-1, 1): + r, c = row, col + while True: + r += step * dr + c += step * dc + if ( + 0 <= r < self.ROWS + and 0 <= c < self.COLUMNS + and self.board[r, c] == player + ): + count += 1 + else: + break + if count >= 4: + return True + return False + + def _check_win_or_draw(self, row, col): + player = self.board[row, col] + if self._is_four(row, col): + # +1 when the agent (player 1) wins, -1 when the opponent wins. + return (1.0 if player == 1 else -1.0), True if np.all(self.board != 0): return 0.0, True return 0.0, False + + def _choose_opponent_move(self): + legal_actions = [c for c in range(self.COLUMNS) if self.board[0, c] == 0] + if not legal_actions: + return None + + # Allow callers to inject a custom opponent strategy. + if callable(self._opponent): + try: + col = self._opponent(self.board.copy(), legal_actions) + except TypeError: + col = self._opponent(legal_actions) + if col in legal_actions: + return col + + # Prefer an immediate winning move. + for col in legal_actions: + row = self._find_drop_row(col) + self.board[row, col] = -1 + wins = self._is_four(row, col) + self.board[row, col] = 0 + if wins: + return col + + # Otherwise block an immediate agent win. + for col in legal_actions: + row = self._find_drop_row(col) + self.board[row, col] = 1 + wins = self._is_four(row, col) + self.board[row, col] = 0 + if wins: + return col + + # Fall back to the most central available column. + return min(legal_actions, key=lambda c: (abs(c - 3), c)) def web_actions( self, observation: Dict[str, Any]