1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-12-30 21:19:52 +02:00

feat: selected piece movement

This commit is contained in:
Maas Lalani 2021-11-27 02:25:59 -05:00
parent b83a43107a
commit fb198c38dc
No known key found for this signature in database
GPG Key ID: F53774FA051C052A
4 changed files with 18 additions and 5 deletions

View File

@ -32,6 +32,10 @@ func (b *Board) Move(from, to position) {
if p == nil {
return
}
cp := b.Grid[to[0]-1][to[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

View File

@ -38,16 +38,21 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
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{}
case "1", "2", "3", "4", "5", "6", "7", "8":
i, _ := strconv.Atoi(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{}
} else {
m.Move.From[0] = i
m.Board.Grid[i-1][m.Move.From[1]-1].Selected = true
}
return m, nil
case "a", "b", "c", "d", "e", "f", "g", "h",
@ -71,7 +76,5 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m model) View() string {
return m.Board.String() + "\n\n" +
m.Move.From.String() + " " +
m.Move.To.String()
return m.Board.String() + "\n"
}

View File

@ -52,10 +52,13 @@ type Piece struct {
Type piece
Color color
Position position
Active bool
Selected bool
}
func (p Piece) String() string {
if p.Selected {
return activeStyle.Render(pieces[p.Type])
}
if p.Color == Black {
return faintStyle.Render(pieces[p.Type])
}

View File

@ -2,4 +2,7 @@ package main
import "github.com/charmbracelet/lipgloss"
var faintStyle = lipgloss.NewStyle().Faint(true)
var (
activeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#E8B4BC"))
faintStyle = lipgloss.NewStyle().Faint(true)
)