qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K
create a chess game
- User
create a chess game
- Assistant
Sure! Here's a simple text-based chess game implemented in Python. This version runs in the terminal and allows two players to take turns entering moves using standard algebraic notation (e.g.,
e2 e4).
🧩 Features:
- Two-player mode (local)
- Basic move validation
- Text-based board display
- Move input in algebraic notation
✅ Requirements:
- Python 3.x
No external libraries required.
📜 Code:
import re import copy # Define the board BOARD_SIZE = 8 BOARD_START = [ ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], ['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'], ['.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.'], ['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'], ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'] ] def print_board(board): print(" a b c d e f g h") for i, row in enumerate(board): print(8 - i, end=' ') print(' '.join(row)) print() def parse_move(move): # Converts algebraic notation like e2 to (row, col) match = re.match(r"([a-h])([1-8])", move) if not match: return None col, row = match.groups() col = ord(col) - ord('a') row = 8 - int(row) return (row, col) def is_valid_move(board, src, dest, turn): # Very basic move validation: only checks that there's a piece of the current player at src sr, sc = src dr, dc = dest if not (0 <= sr < 8 and 0 <= sc < 8 and 0 <= dr < 8 and 0 <= dc < 8): return False piece = board[sr][sc] if piece == '.': return False # Ensure the piece belongs to the current player if turn == 'white' and piece.islower(): return False if turn == 'black' and piece.isupper(): return False return True def make_move(board, src, dest): sr, sc = src dr, dc = dest new_board = copy.deepcopy(board) piece = new_board[sr][sc] new_board[sr][sc] = '.' new_board[dr][dc] = piece return new_board def main(): board = BOARD_START turn = 'white' while True: print_board(board) print(f"{turn.capitalize()}'s move (e.g., e2 e4):") try: move_input = input("Enter move: ").strip().lower() if move_input == "quit": print("Game ended.") break src_str, dest_str = move_input.split() src = parse_move(src_str) dest = parse_move(dest_str) if not src or not dest: print("Invalid move format. Use algebraic notation (e.g., e2 e4).") continue if not is_valid_move(board, src, dest, turn): print("Invalid move.") continue board = make_move(board, src, dest) turn = 'black' if turn == 'white' else 'white' except Exception as e: print("Error:", e) print("Please enter a valid move in the format 'e2 e4'.") if __name__ == "__main__": main()
▶️ How to Run:
- Save the code to a file, e.g.,
chess.py - Open your terminal/command prompt.
- Run it with:
python chess.py
📝 Notes:
- This is a minimal implementation — no check/checkmate detection, castling, en passant, or pawn promotion.
- It’s great for beginners learning how to model a chessboard and handle input/output.
Would you like me to add any of these advanced features (like check detection, AI opponent, GUI, etc.)?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.