1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-11-28 08:38:36 +02:00

refactor: piece style

This commit is contained in:
Maas Lalani 2021-11-27 02:08:40 -05:00
parent 722c21fad8
commit b83a43107a
No known key found for this signature in database
GPG Key ID: F53774FA051C052A
4 changed files with 32 additions and 31 deletions

View File

@ -3,8 +3,6 @@ package main
import (
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
)
const (
@ -23,8 +21,6 @@ const (
divider = " │ "
)
var faintStyle = lipgloss.NewStyle().Faint(true)
type Board struct {
Players []Player
Grid [dimensions][dimensions]*Piece

21
main.go
View File

@ -40,23 +40,26 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "esc":
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 {
i, _ := strconv.Atoi(msg.String())
m.Move.To[0] = i
m.Board.Move(m.Move.From, m.Move.To)
m.Move.From, m.Move.To = position{}, position{}
return m, nil
} else {
m.Move.From[0] = i
}
i, _ := strconv.Atoi(msg.String())
m.Move.From[0] = i
return m, nil
case "a", "b", "c", "d", "e", "f", "g", "h",
"A", "B", "C", "D", "E", "F", "G", "H":
col := FileToColumn(msg.String())
// If we already have the `From` rank set, set the `To` column
if m.Move.From[0] > 0 {
m.Move.To[1] = FileToColumn(msg.String())
return m, nil
m.Move.To[1] = col
} else {
m.Move.From[1] = col
}
m.Move.From[1] = FileToColumn(msg.String())
return m, nil
case "ctrl+f":
m.Board.flipped = !m.Board.flipped
@ -68,5 +71,7 @@ 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\n" +
m.Move.From.String() + " " +
m.Move.To.String()
}

View File

@ -1,6 +1,8 @@
package main
import "fmt"
import (
"fmt"
)
type piece string
@ -20,23 +22,13 @@ const (
Black color = "black"
)
var pieces = map[color]map[piece]string{
Black: {
Pawn: "♟",
Bishop: "♝",
Rook: "♜",
Knight: "♞",
Queen: "♛",
King: "♚",
},
White: {
Pawn: "♙",
Bishop: "♗",
Rook: "♖",
Knight: "♘",
Queen: "♕",
King: "♔",
},
var pieces = map[piece]string{
Pawn: "♟",
Bishop: "♝",
Rook: "♜",
Knight: "♞",
Queen: "♛",
King: "♚",
}
// array of [row, column]
@ -64,7 +56,10 @@ type Piece struct {
}
func (p Piece) String() string {
return pieces[p.Color][p.Type]
if p.Color == Black {
return faintStyle.Render(pieces[p.Type])
}
return pieces[p.Type]
}
func NewPiece(piece piece, position position, color color) *Piece {

5
style.go Normal file
View File

@ -0,0 +1,5 @@
package main
import "github.com/charmbracelet/lipgloss"
var faintStyle = lipgloss.NewStyle().Faint(true)