From 55caa3afbcc792fcc966707b7a56c1d21267d6d5 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Wed, 22 Dec 2021 12:14:07 -0500 Subject: [PATCH] save --- board/board.go | 2 +- board/display.go | 2 +- board/display_test.go | 2 +- game/game.go | 33 +++++++++++++++++++++++++++++++-- main.go | 6 +++++- squares/squares.go | 4 ++++ 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/board/board.go b/board/board.go index fdc58e7..c774706 100644 --- a/board/board.go +++ b/board/board.go @@ -8,7 +8,7 @@ type Board struct { // The board is represented as a 2D array of cells. // The first index is the row, the second is the column. Grid [8][8]piece.Piece - reversed bool + Reversed bool Turn piece.Color } diff --git a/board/display.go b/board/display.go index aba48c5..97f5fab 100644 --- a/board/display.go +++ b/board/display.go @@ -34,7 +34,7 @@ const ( func (b Board) String() string { var s string - if b.reversed { + if b.Reversed { ranks = reverse(ranks) } diff --git a/board/display_test.go b/board/display_test.go index 3e5bca1..d0ac13e 100644 --- a/board/display_test.go +++ b/board/display_test.go @@ -34,7 +34,7 @@ func TestDisplay(t *testing.T) { func TestDisplayReversed(t *testing.T) { b := New() - b.reversed = true + b.Reversed = true expected := ` ┌───┬───┬───┬───┬───┬───┬───┬───┐ 1 │ │ │ │ │ │ │ │ │ diff --git a/game/game.go b/game/game.go index 3b20802..b5f2a68 100644 --- a/game/game.go +++ b/game/game.go @@ -3,10 +3,14 @@ package game import ( tea "github.com/charmbracelet/bubbletea" "github.com/maaslalani/gambit/board" + "github.com/maaslalani/gambit/position" + . "github.com/maaslalani/gambit/squares" ) type model struct { - board board.Board + board board.Board + selected *position.Position + moves []board.Move } func Model() tea.Model { @@ -17,10 +21,35 @@ func Model() tea.Model { } func (m model) Init() tea.Cmd { return nil } -func (m model) View() string { return m.board.String() } +func (m model) View() string { return m.board.String() + m.selected.String() } + +const ( + marginLeft = 4 + marginTop = 2 + cellWidth = 4 + cellHeight = 2 +) func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case tea.MouseMsg: + col := (msg.X - marginLeft) / cellWidth + row := (msg.Y - marginTop) / cellHeight + + if !m.board.Reversed { + row = 7 - row + } + + if m.selected != nil { + m.selected = &position.Position{Row: row, Col: col} + } else { + from := m.selected + to := &position.Position{Row: row, Col: col} + m.moves = append(m.moves, board.Move{ + From: Square((*from).String()), + To: Square((*to).String()), + }) + } case tea.KeyMsg: switch msg.String() { case "ctrl+c", "q": diff --git a/main.go b/main.go index 1be695c..43cd13a 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,11 @@ import ( ) func main() { - p := tea.NewProgram(game.Model()) + p := tea.NewProgram( + game.Model(), + tea.WithAltScreen(), + tea.WithMouseCellMotion(), + ) err := p.Start() if err != nil { diff --git a/squares/squares.go b/squares/squares.go index b3a1a2c..0fd7c7c 100644 --- a/squares/squares.go +++ b/squares/squares.go @@ -68,3 +68,7 @@ const ( H7 = "H7" H8 = "H8" ) + +func (s Square) String() string { + return string(s) +}