Python 3.6自定义棋盘尺寸的井字棋胜负判定实现求助
Hey there! Awesome work building a flexible Tic-Tac-Toe game that supports board sizes from 3x3 to 26x26—this is a great way to level up your Python skills. Let’s walk through how to implement win detection using just two core variables, keeping your code clean and scalable.
The Core Idea
Winning in any sized Tic-Tac-Toe means getting N consecutive identical symbols (where N is your board size). We only need two variables to track this:
target_streak: Set equal to your board size (e.g., 3 for a 3x3 board, 10 for a 10x10 board) — this is how many in a row we need to win.current_streak: Tracks how many consecutive matching symbols we’ve found as we scan the board.
We’ll check four possible win directions: horizontal rows, vertical columns, top-left to bottom-right diagonal, and top-right to bottom-left diagonal.
Example Implementation
Here’s how to put this into code. First, let’s assume your board is a 2D list (e.g., board = [[" ", "X", "O"], ["X", "X", "X"], ["O", " ", "O"]] for a 3x3 board):
def check_win(board, player_symbol, board_size): target_streak = board_size # Check horizontal rows for row in board: current_streak = 1 for i in range(1, board_size): if row[i] == row[i-1] == player_symbol: current_streak += 1 if current_streak == target_streak: return True else: current_streak = 1 # Reset streak if symbols don't match # Check vertical columns for col in range(board_size): current_streak = 1 for row in range(1, board_size): if board[row][col] == board[row-1][col] == player_symbol: current_streak += 1 if current_streak == target_streak: return True else: current_streak = 1 # Check top-left to bottom-right diagonal current_streak = 1 for i in range(1, board_size): if board[i][i] == board[i-1][i-1] == player_symbol: current_streak += 1 if current_streak == target_streak: return True else: current_streak = 1 # Check top-right to bottom-left diagonal current_streak = 1 for i in range(1, board_size): if board[i][board_size - 1 - i] == board[i-1][board_size - i] == player_symbol: current_streak += 1 if current_streak == target_streak: return True else: current_streak = 1 # No win found return False
How to Use This Function
When a player makes a move, call this function with the current board, their symbol ("X" or "O"), and the board size. For example:
# Example board (5x5, X has a winning vertical streak) board = [ [" ", "O", " ", " ", " "], ["X", "O", " ", " ", " "], ["X", "O", " ", " ", " "], ["X", "O", " ", " ", " "], ["X", "O", " ", " ", " "] ] board_size = 5 if check_win(board, "X", board_size): print("Player X wins!") else: print("No winner yet.")
Key Notes
- We reset
current_streakto 1 every time we hit a non-matching symbol (or empty cell, since we check== player_symbol). - This logic works for any board size between 3 and 26—just update
board_sizeandtarget_streakautomatically matches. - Keep your board initialized with empty values (like
" ") so we don’t accidentally count unplayed cells as part of a streak.
This approach keeps your win-check logic simple, uses only the two variables you asked for, and scales perfectly with your customizable board sizes. Happy coding!
内容的提问来源于stack exchange,提问作者Lachlan Shead

