Skip to content

[BUG] TicTacToe check_block_player() has same board corruption bug #5

Description

@galpt

Bug Description

In cog/games.py lines 293-300, the check_block_player() function has the same board corruption pattern as check_fork_opportunities().

cog/games.py#L293-L300

def check_block_player(self):
    empty = [i for i, c in enumerate(self.board) if c == " "]
    for idx in empty:
        self.board[idx] = player_mark  # <-- Corrupts board
        if self.check_winner(player_mark):
            self.board[idx] = " "  # Only resets idx
            return idx
        self.board[idx] = " "
        for i in range(self.size * self.size):
            if self.board[i] == player_mark:  # <-- Line 296: corrupts board
                continue
            self.board[i] = player_mark
            if self.check_winner(player_mark):
                self.board[i] = " "  # Resets i but...
                return idx
            self.board[i] = " "  # Only resets i on each iteration
    return None

Impact: If check_winner(player_mark) returns True at line 297, the function returns before reaching line 299's cleanup. The board is left corrupted with extra player marks.


How to Fix

Apply the same fix - track all modified cells:

def check_block_player(self):
    empty = [i for i, c in enumerate(self.board) if c == " "]
    for idx in empty:
        self.board[idx] = player_mark
        modified = [idx]
        if self.check_winner(player_mark):
            for m in modified:
                self.board[m] = " "
            return idx
        self.board[idx] = " "
        for i in range(self.size * self.size):
            if self.board[i] == player_mark:
                continue
            self.board[i] = player_mark
            modified.append(i)
            if self.check_winner(player_mark):
                for m in modified:
                    self.board[m] = " "
                return idx
            self.board[i] = " "
    return None

Severity

Critical - Causes incorrect bot defensive moves

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions