I asked GPT-4 to make a basic chess program, it says that this won't handle castling, en passant, or detecting check/checkmate.
class ChessBoard:
def __init__(self):
self.board = [
["♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"],
["♟"] * 8,
[""] * 8,
[""] * 8,
[""] * 8,
[""] * 8,
["♙"] * 8,
["♖", "♘", "♗", "♕", "♔", "♗", "♘", "♖"],
]
def __str__(self):
rows = []
for i, row in enumerate(reversed(self.board)):
row_str = []
for j, cell in enumerate(row):
if cell == "":
cell = "□" if (i + j) % 2 == 0 else "■"
row_str.append(cell)
rows.append(" ".join(row_str))
board_str = "\n".join(rows)
return f" a b c d e f g h\n{board_str}"
def move_piece(self, start, end):
sx, sy = start
ex, ey = end
piece = self.board[sy][sx]
self.board[sy][sx] = ""
self.board[ey][ex] = piece
def is_valid_move(self, start, end):
sx, sy = start
ex, ey = end
piece = self.board[sy][sx]
if piece == "":
return False
if self.board[ey][ex] != "" and self.is_same_color(start, end):
return False
if piece in "♟♙":
return self.is_valid_pawn_move(start, end)
elif piece in "♜♖":
return self.is_valid_rook_move(start, end)
elif piece in "♞♘":
return self.is_valid_knight_move(start, end)
elif piece in "♝♗":
return self.is_valid_bishop_move(start, end)
elif piece in "♛♕":
return self.is_valid_queen_move(start, end)
elif piece in "♚♔":
return self.is_valid_king_move(start, end)
return False
def is_same_color(self, start, end):
sx, sy = start
ex, ey = end
return self.board[sy][sx].isupper() == self.board[ey][ex].isupper()
def is_valid_pawn_move(self, start, end):
sx, sy = start
ex, ey = end
dy = ey - sy
dx = abs(ex - sx)
if self.board[sy][sx].isupper():
dy *= -1
if dx == 0 and dy == 1 and self.board[ey][ex] == "":
return True
if dx == 1 and dy == 1 and self.board[ey][ex] != "" and not self.is_same_color(start, end):
return True
if dy == 2 and sy == 1 and self.board[ey][ex] == "" and self.board[sy + 1][sx] == "":
return True
if dy == -2 and sy == 6 and self.board[ey][ex] == "" and self.board[sy - 1][sx] == "":
return True
return False
def is_valid_rook_move(self, start, end):
sx, sy = start
ex, ey = end
if sx != ex and sy != ey:
return False
step_x = 0 if sx == ex else 1 if sx < ex else -1
step_y = 0 if sy == ey else 1 if sy < ey else -1
y, x = sy + step_y, sx + step_x
while y != ey or x != ex:
if self.board[y][x] != "":
return False
y += step_y
x += step_x
return True
def is_valid_knight_move(self, start, end):
sx, sy = start
ex, ey = end
dx, dy = abs(ex - sx), abs(ey - sy)
return (dx == 1 and dy == 2) or (dx == 2 and dy == 1)
def is_valid_bishop_move(self, start, end):
sx, sy = start
ex, ey = end
dx, dy = abs(ex - sx), abs(ey - sy)
if dx != dy:
return False
step_x = 1 if sx < ex else -1
step_y = 1 if sy < ey else -1
y, x = sy + step_y, sx + step_x
while y != ey and x != ex:
if self.board[y][x] != "":
return False
y += step_y
x += step_x
return True
def is_valid_queen_move(self, start, end):
return self.is_valid_rook_move(start, end) or self.is_valid_bishop_move(start, end)
def is_valid_king_move(self, start, end):
sx, sy = start
ex, ey = end
dx, dy = abs(ex - sx), abs(ey - sy)
return dx <= 1 and dy <= 1
def parse_move(move_str):
if len(move_str) != 4 or not move_str.isalnum():
raise ValueError("Invalid input")
start, end = move_str[:2], move_str[2:]
sx, sy = ord(start[0]) - ord("a"), int(start[1]) - 1
ex, ey = ord(end[0]) - ord("a"), int(end[1]) - 1
if not (0 <= sx < 8 and 0 <= sy < 8 and 0 <= ex < 8 and 0 <= ey < 8):
raise ValueError("Coordinates out of range")
return (sx, sy), (ex, ey)
def main():
board = ChessBoard()
player_turn = "White"
while True:
print(board)
print(f"{player_turn} to move")
move = input("Enter your move (e.g. e2e4): ")
try:
start, end = parse_move(move)
if board.is_valid_move(start, end):
board.move_piece(start, end)
player_turn = "Black" if player_turn == "White" else "White"
else:
print("Invalid move")
except ValueError as e:
print(e)
if __name__ == "__main__":
main()
EDIT: apparently old reddit is borked for multi-line code formatting, so I had to extra-indent every like by 4 spaces.
You wasted your time posting generated code nobody is ever going to look at, wasted some more indenting it, just to waste everybody's time as we are forced to scroll past the end of your lengthy post.
Do everyone a favour and replace the code block with a one-line summary of if you think it did a great job, or a terrible one.
8
u/thesuperzapper Apr 10 '23 edited Apr 10 '23
I asked GPT-4 to make a basic chess program, it says that this won't handle castling, en passant, or detecting check/checkmate.
EDIT: apparently old reddit is borked for multi-line code formatting, so I had to extra-indent every like by 4 spaces.