Repository: ray-project/ray
File: rllib/examples/envs/classes/multi_agent/tic_tac_toe.py (Python)

Code before:
        # we expect to act next).
        return {self.current_player: np.array(self.board, np.float32)}, {}

    # __sphinx_doc_3_end__

    # __sphinx_doc_4_begin__
    def step(self, action_dict):
        action = action_dict[self.current_player]
        opponent = "player2" if self.current_player == "player1" else "player1"
        self.timestep += 1

        # Invalid move: penalize and return without changing board.
        if self.board[action] != 0:
            truncated = self.timestep >= self.max_timesteps
            board_arr = np.array(self.board, np.float32)
            if truncated:
                return (
                    {self.current_player: board_arr, opponent: board_arr * -1},
                    {self.current_player: -0.5, opponent: 0.0},
                    {"__all__": False},
                    {"__all__": True},
                    {},
                )
            else:
                reward = {self.current_player: -0.5}
                self.current_player = opponent
                return (
                    {opponent: board_arr * -1},
                    reward,
                    {"__all__": False},
                    {"__all__": False},
                    {},
                )

        # Place the piece on the board.
        self.board[action] = 1
        board_arr = np.array(self.board, np.float32)

        # Check for win.
        if any(all(self.board[i] == 1 for i in line) for line in self.WIN_LINES):
            return (
                {self.current_player: board_arr, opponent: board_arr * -1},
                {self.current_player: 1.0, opponent: -1.0},
                {"__all__": True},
                {"__all__": False},
                {},
            )

        # Check for draw (board full, no winner).
        if 0 not in self.board:
            return (

Review comment (suggestion):
I assuem this does the same as `board_arr * -1` ? Should we keep that way of turning the board? Asking mostly because I'm confused whether this will do something else.

Code after:
        # we expect to act next).
        return {self.current_player: np.array(self.board, np.float32)}, {}

    # __sphinx_doc_3_end__

    # __sphinx_doc_4_begin__
    def step(self, action_dict):
        action = action_dict[self.current_player]
        opponent = "player2" if self.current_player == "player1" else "player1"
        self.timestep += 1

        # Invalid move: penalize and return without changing board.
        if self.board[action] != 0:
            # The time limit is reached
            if self.timestep >= self.max_timesteps:
                board_arr = np.array(self.board, np.float32)
                return (
                    {self.current_player: board_arr, opponent: board_arr * -1},
                    {self.current_player: -0.5, opponent: 0.0},
                    {"__all__": False},
                    {"__all__": True},
                    {},
                )
            else:
                reward = {self.current_player: -0.5}
                self.board = [-x for x in self.board]
                self.current_player = opponent
                return (
                    {opponent: np.array(self.board, np.float32)},
                    reward,
                    {"__all__": False},
                    {"__all__": False},
                    {},
                )

        # Place the piece on the board.
        self.board[action] = 1

        # Check for win.
        if any(all(self.board[i] == 1 for i in line) for line in self.WIN_LINES):
            board_arr = np.array(self.board, np.float32)
            return (
                {self.current_player: board_arr, opponent: board_arr * -1},
                {self.current_player: 1.0, opponent: -1.0},
                {"__all__": True},
                {"__all__": False},
                {},
            )

        # Check for draw (board full, no winner).
        if 0 not in self.board: