From ec63525eb6038a0b3497ca76cec85a72c0df28b1 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sat, 27 Nov 2021 15:25:58 -0500 Subject: [PATCH] refactor: position --- board.go | 18 +++++++++--------- main.go | 40 +++++++++++++++------------------------- piece.go | 11 +++++++++++ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/board.go b/board.go index 2085b22..6445b22 100644 --- a/board.go +++ b/board.go @@ -22,27 +22,27 @@ const ( ) type Board struct { - Pieces []*Piece Grid [dimensions][dimensions]*Piece flipped bool } -func (b *Board) Move(from, to position) { - p := b.Grid[from[0]-1][from[1]-1] +func (b *Board) Move(from, to string) { + fromPos, toPos := ToPosition(from), ToPosition(to) + p := b.Grid[fromPos[0]-1][fromPos[1]-1] if p == nil { return } - cp := b.Grid[to[0]-1][to[1]-1] + cp := b.Grid[toPos[0]-1][toPos[1]-1] if cp != nil { // TODO: Capture } - b.Grid[from[0]-1][from[1]-1] = nil - b.Grid[to[0]-1][to[1]-1] = p - p.Position = to + b.Grid[fromPos[0]-1][fromPos[1]-1] = nil + b.Grid[toPos[0]-1][toPos[1]-1] = p + p.Position = toPos } -func (b *Board) Draw() { - for _, piece := range b.Pieces { +func (b *Board) Init(pieces []*Piece) { + for _, piece := range pieces { p := piece b.Grid[piece.Position[0]-1][piece.Position[1]-1] = p } diff --git a/main.go b/main.go index f1c018d..9e9a460 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,14 @@ package main import ( "log" - "strconv" + "strings" tea "github.com/charmbracelet/bubbletea" ) func main() { m := model{} - m.Board.Pieces = append(m.Board.Pieces, WhitePieces()...) - m.Board.Pieces = append(m.Board.Pieces, BlackPieces()...) + m.Board.Init(append(WhitePieces(), BlackPieces()...)) p := tea.NewProgram(m, tea.WithMouseAllMotion()) if err := p.Start(); err != nil { log.Fatal(err) @@ -19,48 +18,39 @@ func main() { type model struct { Board Board - Move struct { - From position - To position - } + From string + To string } func (m model) Init() tea.Cmd { return nil } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - m.Board.Draw() - switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "ctrl+c", "q": return m, tea.Quit case "esc": - if m.Move.From[0] > 0 && m.Move.From[1] > 0 { - m.Board.Grid[m.Move.From[0]-1][m.Move.From[1]-1].Selected = false - } - m.Move.From, m.Move.To = position{}, position{} + m.From, m.To = "", "" case "1", "2", "3", "4", "5", "6", "7", "8": - i, _ := strconv.Atoi(msg.String()) + rank := msg.String() // `From` and `To` moves will be complete, perform move - if m.Move.To[1] > 0 { - m.Move.To[0] = i - m.Board.Grid[m.Move.From[0]-1][m.Move.From[1]-1].Selected = false - m.Board.Move(m.Move.From, m.Move.To) - m.Move.From, m.Move.To = position{}, position{} + if len(m.To) >= 1 { + m.To += rank + m.Board.Move(m.From, m.To) + m.From, m.To = "", "" } else { - m.Move.From[0] = i - m.Board.Grid[i-1][m.Move.From[1]-1].Selected = true + m.From += rank } return m, nil case "a", "b", "c", "d", "e", "f", "g", "h", "A", "B", "C", "D", "E", "F", "G", "H": - col := FileToColumn(msg.String()) + file := strings.ToUpper(msg.String()) // If we already have the `From` rank set, set the `To` column - if m.Move.From[0] > 0 { - m.Move.To[1] = col + if len(m.From) == 2 { + m.To = file } else { - m.Move.From[1] = col + m.From = file } return m, nil diff --git a/piece.go b/piece.go index 4958fd5..1643f40 100644 --- a/piece.go +++ b/piece.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strconv" ) type piece string @@ -36,6 +37,16 @@ var pieces = map[piece]string{ // e.g. [1, 4] (D1) type position [2]int +func ToPosition(square string) position { + if len(square) != 2 { + return position{0, 0} + } + + rank, _ := strconv.Atoi(string(square[1])) + file := FileToColumn(string(square[0])) + return position{rank, file} +} + func (p position) String() string { return p.File() + p.Rank() }