Create a chess game
- User
Create a chess game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Create a chess game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a text-based chess game implementation in Python that allows two players to move pieces according to basic rules. It supports valid moves for all pieces (except special moves like castling, en passant, and promotion), and alternates turns between players.
quit).def parse_pos(s):
"""Convert algebraic notation (e.g., 'e2') to board indices (row, col)"""
if len(s) != 2:
return None
col = s[0].lower()
row = s[1]
if not ('a' <= col <= 'h') or not ('1' <= row <= '8'):
return None
x = ord(col) - ord('a') # 0 to 7
y = int(row) - 1 # 0 to 7
return (y, x)
def print_board(board):
print(" a b c d e f g h")
print(" +----------------------------+")
for i in range(7, -1, -1):
print(f"{i+1}|", end=" ")
for j in range(8):
print(f"{board[i][j]} ", end=" ")
print(f"|{i+1}")
print(" +----------------------------+")
print(" a b c d e f g h")
def is_valid_move(board, start, end, player_turn):
piece = board[start[0]][start[1]]
target = board[end[0]][end[1]]
# Check if piece belongs to current player
if piece == ' ':
return False
if player_turn == 'white' and not piece.isupper():
return False
if player_turn == 'black' and not piece.islower():
return False
dx = end[0] - start[0]
dy = end[1] - start[1]
abs_dx, abs_dy = abs(dx), abs(dy)
# Check if target is not friendly
if target != ' ':
if (player_turn == 'white' and target.isupper()) or (player_turn == 'black' and target.islower()):
return False
# Determine piece type
lower_piece = piece.lower()
# Movement validation
if lower_piece == 'p': # Pawn
if player_turn == 'white':
if dx == 1 and dy == 0 and target == ' ':
return True
elif dx == 1 and abs(dy) == 1 and target != ' ':
return True
elif dx == 2 and dy == 0 and target == ' ' and start[0] == 1:
if board[start[0]+1][start[1]] == ' ':
return True
else: # Black
if dx == -1 and dy == 0 and target == ' ':
return True
elif dx == -1 and abs(dy) == 1 and target != ' ':
return True
elif dx == -2 and dy == 0 and target == ' ' and start[0] == 6:
if board[start[0]-1][start[1]] == ' ':
return True
return False
elif lower_piece == 'r': # Rook
if abs_dx > 0 and dy == 0:
step = 1 if dx > 0 else -1
for i in range(1, abs_dx):
if board[start[0] + i * step][start[1]] != ' ':
return False
return True
elif abs_dy > 0 and dx == 0:
step = 1 if dy > 0 else -1
for i in range(1, abs_dy):
if board[start[0]][start[1] + i * step] != ' ':
return False
return True
return False
elif lower_piece == 'n': # Knight
return (abs_dx == 2 and abs_dy == 1) or (abs_dx == 1 and abs_dy == 2)
elif lower_piece == 'b': # Bishop
if abs_dx == abs_dy:
x_step = 1 if dy > 0 else -1
y_step = 1 if dx > 0 else -1
for i in range(1, abs_dx):
if board[start[0] + i * y_step][start[1] + i * x_step] != ' ':
return False
return True
return False
elif lower_piece == 'q': # Queen
if abs_dx == abs_dy:
x_step = 1 if dy > 0 else -1
y_step = 1 if dx > 0 else -1
for i in range(1, abs_dx):
if board[start[0] + i * y_step][start[1] + i * x_step] != ' ':
return False
return True
elif dy == 0:
step = 1 if dx > 0 else -1
for i in range(1, abs_dx):
if board[start[0] + i * step][start[1]] != ' ':
return False
return True
elif dx == 0:
step = 1 if dy > 0 else -1
for i in range(1, abs_dy):
if board[start[0]][start[1] + i * step] != ' ':
return False
return True
return False
elif lower_piece == 'k': # King
return abs_dx <= 1 and abs_dy <= 1
return False
def make_move(board, start, end):
piece = board[start[0]][start[1]]
board[end[0]][end[1]] = piece
board[start[0]][start[1]] = ' '
def main():
board = [
['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']
]
current_player = 'white'
while True:
print_board(board)
print(f"{current_player.capitalize()}'s move. Enter move (e.g., e2 e4):")
move = input().strip().lower()
if move == 'quit':
print("Game ended.")
break
if len(move.split()) != 2:
print("Invalid format. Use 'from to' notation (e.g., e2 e4).")
continue
from_pos, to_pos = move.split()
start = parse_pos(from_pos)
end = parse_pos(to_pos)
if not start or not end:
print("Invalid position.")
continue
if start == end:
print("Start and end positions must be different.")
continue
if not is_valid_move(board, start, end, current_player):
print("Invalid move.")
continue
make_move(board, start, end)
current_player = 'black' if current_player == 'white' else 'white'
if __name__ == "__main__":
main()
You can extend the game by:
tkinter or pygame.This is a solid foundation for a terminal-based chess game. You can build upon it to add more features and realism!