mirror of
https://github.com/maaslalani/gambit.git
synced 2024-11-28 08:38:36 +02:00
cleanup
This commit is contained in:
parent
20709692d9
commit
1593d5a867
@ -4,11 +4,18 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/maaslalani/gambit/board"
|
"github.com/maaslalani/gambit/board"
|
||||||
|
"github.com/maaslalani/gambit/position"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// cellHeight represents how many rows are in a cell
|
||||||
cellHeight = 2
|
cellHeight = 2
|
||||||
|
// cellWidth represents how many columns are in a cell
|
||||||
cellWidth = 4
|
cellWidth = 4
|
||||||
|
|
||||||
|
// marginLeft and marginTop represent how much to offset the
|
||||||
|
// chess board from the top left of the terminal to account for
|
||||||
|
// padding and rank labels
|
||||||
marginLeft = 3
|
marginLeft = 3
|
||||||
marginTop = 1
|
marginTop = 1
|
||||||
|
|
||||||
@ -16,10 +23,13 @@ const (
|
|||||||
Horizontal = "─"
|
Horizontal = "─"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Cell(x, y int) (int, int) {
|
// Cell returns the square that was clicked based on mouse
|
||||||
|
// coordinates adjusting for margins and cell dimensions.
|
||||||
|
func Cell(x, y int) string {
|
||||||
col := (x - marginLeft) / cellWidth
|
col := (x - marginLeft) / cellWidth
|
||||||
row := board.LastRow - (y-marginTop)/cellHeight
|
row := board.LastRow - (y-marginTop)/cellHeight
|
||||||
return col, row
|
return position.ToSquare(row, col)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// withMarginLeft returns a string with a prepended left margin
|
// withMarginLeft returns a string with a prepended left margin
|
||||||
@ -34,18 +44,22 @@ func Build(left, middle, right string) string {
|
|||||||
return withMarginLeft(border)
|
return withMarginLeft(border)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Top returns a built border with the top row
|
||||||
func Top() string {
|
func Top() string {
|
||||||
return Build("┌", "┬", "┐")
|
return Build("┌", "┬", "┐")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Middle returns a built border with the middle row
|
||||||
func Middle() string {
|
func Middle() string {
|
||||||
return Build("├", "┼", "┤")
|
return Build("├", "┼", "┤")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bottom returns a built border with the bottom row
|
||||||
func Bottom() string {
|
func Bottom() string {
|
||||||
return Build("└", "┴", "┘")
|
return Build("└", "┴", "┘")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BottomLabels returns the labels for the files
|
||||||
func BottomLabels() string {
|
func BottomLabels() string {
|
||||||
return withMarginLeft(" A B C D E F G H\n")
|
return withMarginLeft(" A B C D E F G H\n")
|
||||||
}
|
}
|
||||||
|
@ -84,11 +84,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
col, row := border.Cell(msg.X, msg.Y)
|
selected := border.Cell(msg.X, msg.Y)
|
||||||
|
|
||||||
if m.selected != "" {
|
if m.selected != "" {
|
||||||
from := m.selected
|
from := m.selected
|
||||||
to := fmt.Sprintf("%s", position.ToSquare(row, col))
|
to := selected
|
||||||
|
|
||||||
for _, move := range m.moves {
|
for _, move := range m.moves {
|
||||||
if move.String() == from+to {
|
if move.String() == from+to {
|
||||||
@ -113,7 +113,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m.selected = fmt.Sprintf("%s", position.ToSquare(row, col))
|
m.selected = selected
|
||||||
m.pieceMoves = []dt.Move{}
|
m.pieceMoves = []dt.Move{}
|
||||||
for _, move := range m.moves {
|
for _, move := range m.moves {
|
||||||
if strings.HasPrefix(move.String(), m.selected) {
|
if strings.HasPrefix(move.String(), m.selected) {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package pieces
|
package pieces
|
||||||
|
|
||||||
|
// Display maps pieces in FEN representation to their ASCII
|
||||||
|
// representation for the user.
|
||||||
var Display = map[string]string{
|
var Display = map[string]string{
|
||||||
"": " ",
|
"": " ",
|
||||||
"B": "♗",
|
"B": "♗",
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/maaslalani/gambit/board"
|
"github.com/maaslalani/gambit/board"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// colToFile returns the File given a column
|
||||||
func colToFile(col int) string {
|
func colToFile(col int) string {
|
||||||
if col < board.FirstCol {
|
if col < board.FirstCol {
|
||||||
col = board.FirstCol
|
col = board.FirstCol
|
||||||
@ -16,6 +17,7 @@ func colToFile(col int) string {
|
|||||||
return fmt.Sprintf("%c", col+'a')
|
return fmt.Sprintf("%c", col+'a')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rowToRank returns a Rank given a row
|
||||||
func rowToRank(row int) int {
|
func rowToRank(row int) int {
|
||||||
if row < board.FirstRow {
|
if row < board.FirstRow {
|
||||||
row = board.FirstRow
|
row = board.FirstRow
|
||||||
@ -25,6 +27,10 @@ func rowToRank(row int) int {
|
|||||||
return row + 1
|
return row + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToSquare returns the square position of a given row and
|
||||||
|
// column for display or checking legal moves.
|
||||||
|
//
|
||||||
|
// For example, ToSquare(0, 0) returns a1.
|
||||||
func ToSquare(row, col int) string {
|
func ToSquare(row, col int) string {
|
||||||
return colToFile(col) + strconv.Itoa(rowToRank(row))
|
return colToFile(col) + strconv.Itoa(rowToRank(row))
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/maaslalani/gambit/board"
|
"github.com/maaslalani/gambit/board"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// fileToCol returns column number for a given file rune
|
||||||
func fileToCol(file rune) int {
|
func fileToCol(file rune) int {
|
||||||
col := int(file - 'a')
|
col := int(file - 'a')
|
||||||
if col < board.FirstCol {
|
if col < board.FirstCol {
|
||||||
@ -16,6 +17,7 @@ func fileToCol(file rune) int {
|
|||||||
return col
|
return col
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rankToRow returns a row number for a given rank
|
||||||
func rankToRow(rank int) int {
|
func rankToRow(rank int) int {
|
||||||
row := rank - 1
|
row := rank - 1
|
||||||
if row < board.FirstRow {
|
if row < board.FirstRow {
|
||||||
@ -26,6 +28,11 @@ func rankToRow(rank int) int {
|
|||||||
return row
|
return row
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToPosition takes a square string and returns the
|
||||||
|
// corresponding row and column for compatibility with the grid
|
||||||
|
// (8x8 matrix).
|
||||||
|
//
|
||||||
|
// For example: "a1" returns (0, 0) and "h8" returns (7, 7).
|
||||||
func ToPosition(square string) (int, int) {
|
func ToPosition(square string) (int, int) {
|
||||||
col := fileToCol(rune(square[0]))
|
col := fileToCol(rune(square[0]))
|
||||||
row, _ := strconv.Atoi(string(square[1]))
|
row, _ := strconv.Atoi(string(square[1]))
|
||||||
|
@ -2,11 +2,11 @@ package style
|
|||||||
|
|
||||||
import . "github.com/charmbracelet/lipgloss"
|
import . "github.com/charmbracelet/lipgloss"
|
||||||
|
|
||||||
func foreground(color string) Style {
|
func fg(color string) Style {
|
||||||
return NewStyle().Foreground(Color(color))
|
return NewStyle().Foreground(Color(color))
|
||||||
}
|
}
|
||||||
|
|
||||||
var Cyan = foreground("5")
|
var Cyan = fg("5")
|
||||||
var Faint = foreground("8")
|
var Faint = fg("8")
|
||||||
var Red = foreground("1")
|
var Red = fg("1")
|
||||||
var Selected = foreground("6")
|
var Selected = fg("6")
|
||||||
|
Loading…
Reference in New Issue
Block a user