Fix scope error + proper vertical/horizontal centering

- Remove leftover console.print that caused scope error
- Consolidate all board rendering into render_board function
- Add vertical centering based on terminal height
- Board now properly centers in any size terminal
This commit is contained in:
Greg Hendrickson
2026-01-27 18:55:43 +00:00
parent ed484e27a2
commit d5b187fdd5

View File

@@ -275,12 +275,11 @@ async def run_chess_game(process, session: TerminalSession, username: str, oppon
session.clear() session.clear()
# Simple text-based board that works reliably over SSH # Simple text-based board that works reliably over SSH
lines = [] board_lines = []
lines.append("") board_lines.append(" ♔ SHELLMATE CHESS ♔")
lines.append(" ♔ SHELLMATE CHESS ♔") board_lines.append("")
lines.append("") board_lines.append(" a b c d e f g h")
lines.append(" a b c d e f g h") board_lines.append(" +---+---+---+---+---+---+---+---+")
lines.append(" +---+---+---+---+---+---+---+---+")
for rank in range(7, -1, -1): for rank in range(7, -1, -1):
row = f" {rank + 1} |" row = f" {rank + 1} |"
@@ -297,46 +296,49 @@ async def run_chess_game(process, session: TerminalSession, username: str, oppon
row += " . |" if not is_light else " |" row += " . |" if not is_light else " |"
row += f" {rank + 1}" row += f" {rank + 1}"
lines.append(row) board_lines.append(row)
lines.append(" +---+---+---+---+---+---+---+---+") board_lines.append(" +---+---+---+---+---+---+---+---+")
lines.append(" a b c d e f g h") board_lines.append(" a b c d e f g h")
lines.append("")
# Center the board # Add status info
board_width = 42 board_lines.append("")
left_pad = max(0, (session.width - board_width) // 2)
pad = " " * left_pad
for line in lines:
session.write(pad + line + "\r\n")
# Use Rich only for the status panel (simpler)
# Simple status display
turn = "White ♔" if board.turn == chess.WHITE else "Black ♚" turn = "White ♔" if board.turn == chess.WHITE else "Black ♚"
session.write(pad + f" {turn} to move\r\n") board_lines.append(f" {turn} to move")
if board.is_check(): if board.is_check():
session.write(pad + " *** CHECK! ***\r\n") board_lines.append(" *** CHECK! ***")
if move_history: if move_history:
last_moves = move_history[-3:] last_moves = move_history[-3:]
session.write(pad + f" Moves: {' '.join(last_moves)}\r\n") board_lines.append(f" Moves: {' '.join(last_moves)}")
if status_msg: if status_msg:
session.write(pad + f" {status_msg}\r\n") board_lines.append(f" {status_msg}")
status_msg = "" status_msg = ""
session.write("\r\n") board_lines.append("")
session.write(pad + " Enter move (e.g. e2e4) | [q]uit | [r]esign\r\n") board_lines.append(" Enter move (e.g. e2e4) | [q]uit | [r]esign")
session.write(pad + " Move: ") board_lines.append(" Move: ")
session.show_cursor()
# Calculate centering
board_width = 42
total_height = len(board_lines)
# Horizontal centering
left_pad = max(0, (session.width - board_width) // 2)
pad = " " * left_pad
# Vertical centering
top_pad = max(0, (session.height - total_height) // 2)
# Output with centering
for _ in range(top_pad):
session.write("\r\n")
for line in board_lines:
session.write(pad + line + "\r\n")
# Show cursor for input
session.show_cursor()
console.print(Align.center("[cyan]Move: [/cyan]"), end="")
render_board() render_board()
move_buffer = "" move_buffer = ""