mirror of
https://github.com/maaslalani/gambit.git
synced 2024-11-24 08:22:12 +02:00
feat: move pieces
This commit is contained in:
parent
2e2cd7fac2
commit
f3825aa99d
33
board.go
33
board.go
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
@ -26,18 +27,36 @@ var faintStyle = lipgloss.NewStyle().Faint(true)
|
||||
|
||||
type Board struct {
|
||||
Players []Player
|
||||
Grid [dimensions][dimensions]string
|
||||
Grid [dimensions][dimensions]*Piece
|
||||
flipped bool
|
||||
}
|
||||
|
||||
func (b *Board) Move(from, to position) {
|
||||
p := b.Grid[from[0]-1][from[1]-1]
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
b.Grid[from[0]-1][from[1]-1] = nil
|
||||
b.Grid[to[0]-1][to[1]-1] = p
|
||||
p.Position = to
|
||||
}
|
||||
|
||||
func (b *Board) Draw() {
|
||||
for _, player := range b.Players {
|
||||
for _, piece := range player.Pieces {
|
||||
b.Grid[piece.Position[0]-1][piece.Position[1]-1] = piece.String()
|
||||
p := piece
|
||||
b.Grid[piece.Position[0]-1][piece.Position[1]-1] = p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func FileToColumn(file string) int {
|
||||
if len(file) < 1 {
|
||||
return 0
|
||||
}
|
||||
return int([]rune(strings.ToUpper(file))[0] - 64)
|
||||
}
|
||||
|
||||
func (b *Board) String() string {
|
||||
var s = header
|
||||
|
||||
@ -50,17 +69,17 @@ func (b *Board) String() string {
|
||||
}
|
||||
|
||||
for row, rank := range ranks {
|
||||
for col := firstCol; col < dimensions; col++ {
|
||||
if col == firstCol {
|
||||
for file := firstCol; file < dimensions; file++ {
|
||||
if file == firstCol {
|
||||
s += faintStyle.Render(fmt.Sprintf(" %d ", rank+1))
|
||||
}
|
||||
s += divider
|
||||
if b.Grid[rank][col] == "" {
|
||||
if b.Grid[rank][file] == nil {
|
||||
s += " "
|
||||
} else {
|
||||
s += b.Grid[rank][col]
|
||||
s += b.Grid[rank][file].String()
|
||||
}
|
||||
if col == lastCol {
|
||||
if file == lastCol {
|
||||
s += divider
|
||||
}
|
||||
}
|
||||
|
26
main.go
26
main.go
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
@ -20,6 +21,10 @@ func main() {
|
||||
|
||||
type model struct {
|
||||
Board Board
|
||||
Move struct {
|
||||
From position
|
||||
To position
|
||||
}
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd { return nil }
|
||||
@ -32,6 +37,25 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q", "esc":
|
||||
return m, tea.Quit
|
||||
case "1", "2", "3", "4", "5", "6", "7", "8":
|
||||
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
|
||||
}
|
||||
i, _ := strconv.Atoi(msg.String())
|
||||
m.Move.From[0] = i
|
||||
case "a", "b", "c", "d", "e", "f", "g", "h",
|
||||
"A", "B", "C", "D", "E", "F", "G", "H":
|
||||
if m.Move.From[0] > 0 {
|
||||
m.Move.To[1] = FileToColumn(msg.String())
|
||||
return m, nil
|
||||
}
|
||||
|
||||
m.Move.From[1] = FileToColumn(msg.String())
|
||||
return m, nil
|
||||
case "ctrl+f":
|
||||
m.Board.flipped = !m.Board.flipped
|
||||
return m, nil
|
||||
@ -42,5 +66,5 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
func (m model) View() string {
|
||||
return m.Board.String()
|
||||
return m.Board.String() + "\n\n" + m.Move.From.String() + " " + m.Move.To.String()
|
||||
}
|
||||
|
27
piece.go
27
piece.go
@ -39,20 +39,21 @@ var pieces = map[color]map[piece]string{
|
||||
},
|
||||
}
|
||||
|
||||
// array of [row, column]
|
||||
// e.g. [1, 4]
|
||||
// array of [row, column]
|
||||
// array of [rank, file]
|
||||
// e.g. [1, 4] (D1)
|
||||
type position [2]int
|
||||
|
||||
func (p position) String() string {
|
||||
return p.Row() + p.Col()
|
||||
return p.File() + p.Rank()
|
||||
}
|
||||
|
||||
func (p position) Row() string {
|
||||
return string(rune('A' - 1 + p[0]))
|
||||
func (p position) File() string {
|
||||
return string(rune('A' - 1 + p[1]))
|
||||
}
|
||||
|
||||
func (p position) Col() string {
|
||||
return fmt.Sprint(p[1])
|
||||
func (p position) Rank() string {
|
||||
return fmt.Sprint(p[0])
|
||||
}
|
||||
|
||||
type Piece struct {
|
||||
@ -66,16 +67,16 @@ func (p Piece) String() string {
|
||||
return pieces[p.Color][p.Type]
|
||||
}
|
||||
|
||||
func NewPiece(piece piece, position position, color color) Piece {
|
||||
return Piece{
|
||||
func NewPiece(piece piece, position position, color color) *Piece {
|
||||
return &Piece{
|
||||
Type: piece,
|
||||
Position: position,
|
||||
Color: color,
|
||||
}
|
||||
}
|
||||
|
||||
func InitialPieces(color color) []Piece {
|
||||
var pieces []Piece
|
||||
func InitialPieces(color color) []*Piece {
|
||||
var pieces []*Piece
|
||||
var (
|
||||
pawnRank int
|
||||
backRank int
|
||||
@ -98,10 +99,10 @@ func InitialPieces(color color) []Piece {
|
||||
return pieces
|
||||
}
|
||||
|
||||
func BlackPieces() []Piece {
|
||||
func BlackPieces() []*Piece {
|
||||
return InitialPieces(Black)
|
||||
}
|
||||
|
||||
func WhitePieces() []Piece {
|
||||
func WhitePieces() []*Piece {
|
||||
return InitialPieces(White)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user