From 3a772ea21b243e847f58c4a3608591289d5e05c1 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Tue, 16 Sep 2025 14:37:48 -0500 Subject: [PATCH] improve board rendering Signed-off-by: Jason Hall --- chess.go | 18 +++++++++--------- iac/chessh.tf | 2 +- main.go | 32 +++++++++++++++++--------------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/chess.go b/chess.go index cacf646..ea07824 100644 --- a/chess.go +++ b/chess.go @@ -96,7 +96,7 @@ func NewBoard() Board { {Pawn, White}, {Pawn, White}, {Pawn, White}, {Pawn, White}, } - for col := 0; col < 8; col++ { + for col := range 8 { for row := 2; row < 6; row++ { board[row][col] = Piece{Empty, White} } @@ -435,8 +435,8 @@ func (g *Game) updateGameState(from, to Position, piece Piece) { } func (g *Game) FindKing(color Color) Position { - for row := 0; row < 8; row++ { - for col := 0; col < 8; col++ { + for row := range 8 { + for col := range 8 { pos := Position{row, col} piece := g.Board.At(pos) if piece.Type == King && piece.Color == color { @@ -455,8 +455,8 @@ func (g *Game) IsInCheck(color Color) bool { enemyColor := 1 - color - for row := 0; row < 8; row++ { - for col := 0; col < 8; col++ { + for row := range 8 { + for col := range 8 { pos := Position{row, col} piece := g.Board.At(pos) if piece.Type != Empty && piece.Color == enemyColor { @@ -500,13 +500,13 @@ func (g *Game) IsCheckmate(color Color) bool { return false } - for row := 0; row < 8; row++ { - for col := 0; col < 8; col++ { + for row := range 8 { + for col := range 8 { from := Position{row, col} piece := g.Board.At(from) if piece.Type != Empty && piece.Color == color { - for toRow := 0; toRow < 8; toRow++ { - for toCol := 0; toCol < 8; toCol++ { + for toRow := range 8 { + for toCol := range 8 { to := Position{toRow, toCol} if g.isValidMoveIgnoringCheck(from, to) { originalPiece := g.Board.At(to) diff --git a/iac/chessh.tf b/iac/chessh.tf index 5ae133e..8dc01e6 100644 --- a/iac/chessh.tf +++ b/iac/chessh.tf @@ -94,7 +94,7 @@ resource "google_cloud_run_v2_service" "chessh" { scaling { min_instance_count = 0 - max_instance_count = 10 + max_instance_count = 1 } } diff --git a/main.go b/main.go index b918dc8..e42f5d5 100644 --- a/main.go +++ b/main.go @@ -331,7 +331,7 @@ func (m model) renderBoardWithInfo() string { if i < len(boardLines) { s.WriteString(boardLines[i]) } else { - s.WriteString(strings.Repeat(" ", 27)) // Board width padding + s.WriteString(strings.Repeat(" ", 26)) // Board width padding (8*3 + 2 for row numbers) } s.WriteString(" ") @@ -349,41 +349,43 @@ func (m model) renderBoardWithInfo() string { func (m model) getBoardLines() []string { var lines []string - lines = append(lines, " a b c d e f g h ") + lines = append(lines, " a b c d e f g h ") for row := 7; row >= 0; row-- { var line strings.Builder - line.WriteString(fmt.Sprintf("%d ", row+1)) + line.WriteString(fmt.Sprintf("%d", row+1)) - for col := 0; col < 8; col++ { + for col := range 8 { pos := Position{row, col} piece := m.game.Board.At(pos) cellChar := piece.String() if piece.Type == Empty { - if (row+col)%2 == 0 { - cellChar = "ยท" - } else { - cellChar = " " - } + cellChar = " " } + // Determine background color + var bgColor string if m.cursorRow == row && m.cursorCol == col { - line.WriteString(fmt.Sprintf("[%s]", cellChar)) + bgColor = "\033[41m" // Red background for cursor } else if m.selected != nil && m.selected.Row == row && m.selected.Col == col { - line.WriteString(fmt.Sprintf("<%s>", cellChar)) + bgColor = "\033[43m" // Yellow background for selected } else if slices.Contains(m.validMoves, pos) { - line.WriteString(fmt.Sprintf("*%s*", cellChar)) + bgColor = "\033[42m" // Green background for valid moves + } else if (row+col)%2 == 0 { + bgColor = "\033[47m" // White background for light squares } else { - line.WriteString(fmt.Sprintf(" %s ", cellChar)) + bgColor = "\033[40m" // Black background for dark squares } + + line.WriteString(fmt.Sprintf("%s %s \033[0m", bgColor, cellChar)) } - line.WriteString(fmt.Sprintf(" %d", row+1)) + line.WriteString(fmt.Sprintf("%d", row+1)) lines = append(lines, line.String()) } - lines = append(lines, " a b c d e f g h ") + lines = append(lines, " a b c d e f g h ") return lines }