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:
parent
b83a43107a
commit
fb198c38dc
4
board.go
4
board.go
@ -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
|
||||
|
9
main.go
9
main.go
@ -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"
|
||||
}
|
||||
|
5
piece.go
5
piece.go
@ -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])
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user