mirror of
https://github.com/maaslalani/gambit.git
synced 2024-11-24 08:22:12 +02:00
save
This commit is contained in:
parent
9a0d34e37d
commit
55caa3afbc
@ -8,7 +8,7 @@ type Board struct {
|
|||||||
// The board is represented as a 2D array of cells.
|
// The board is represented as a 2D array of cells.
|
||||||
// The first index is the row, the second is the column.
|
// The first index is the row, the second is the column.
|
||||||
Grid [8][8]piece.Piece
|
Grid [8][8]piece.Piece
|
||||||
reversed bool
|
Reversed bool
|
||||||
Turn piece.Color
|
Turn piece.Color
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ const (
|
|||||||
func (b Board) String() string {
|
func (b Board) String() string {
|
||||||
var s string
|
var s string
|
||||||
|
|
||||||
if b.reversed {
|
if b.Reversed {
|
||||||
ranks = reverse(ranks)
|
ranks = reverse(ranks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ func TestDisplay(t *testing.T) {
|
|||||||
|
|
||||||
func TestDisplayReversed(t *testing.T) {
|
func TestDisplayReversed(t *testing.T) {
|
||||||
b := New()
|
b := New()
|
||||||
b.reversed = true
|
b.Reversed = true
|
||||||
expected := `
|
expected := `
|
||||||
┌───┬───┬───┬───┬───┬───┬───┬───┐
|
┌───┬───┬───┬───┬───┬───┬───┬───┐
|
||||||
1 │ │ │ │ │ │ │ │ │
|
1 │ │ │ │ │ │ │ │ │
|
||||||
|
33
game/game.go
33
game/game.go
@ -3,10 +3,14 @@ package game
|
|||||||
import (
|
import (
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/maaslalani/gambit/board"
|
"github.com/maaslalani/gambit/board"
|
||||||
|
"github.com/maaslalani/gambit/position"
|
||||||
|
. "github.com/maaslalani/gambit/squares"
|
||||||
)
|
)
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
board board.Board
|
board board.Board
|
||||||
|
selected *position.Position
|
||||||
|
moves []board.Move
|
||||||
}
|
}
|
||||||
|
|
||||||
func Model() tea.Model {
|
func Model() tea.Model {
|
||||||
@ -17,10 +21,35 @@ func Model() tea.Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd { return nil }
|
func (m model) Init() tea.Cmd { return nil }
|
||||||
func (m model) View() string { return m.board.String() }
|
func (m model) View() string { return m.board.String() + m.selected.String() }
|
||||||
|
|
||||||
|
const (
|
||||||
|
marginLeft = 4
|
||||||
|
marginTop = 2
|
||||||
|
cellWidth = 4
|
||||||
|
cellHeight = 2
|
||||||
|
)
|
||||||
|
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
case tea.MouseMsg:
|
||||||
|
col := (msg.X - marginLeft) / cellWidth
|
||||||
|
row := (msg.Y - marginTop) / cellHeight
|
||||||
|
|
||||||
|
if !m.board.Reversed {
|
||||||
|
row = 7 - row
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.selected != nil {
|
||||||
|
m.selected = &position.Position{Row: row, Col: col}
|
||||||
|
} else {
|
||||||
|
from := m.selected
|
||||||
|
to := &position.Position{Row: row, Col: col}
|
||||||
|
m.moves = append(m.moves, board.Move{
|
||||||
|
From: Square((*from).String()),
|
||||||
|
To: Square((*to).String()),
|
||||||
|
})
|
||||||
|
}
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "q":
|
case "ctrl+c", "q":
|
||||||
|
6
main.go
6
main.go
@ -8,7 +8,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
p := tea.NewProgram(game.Model())
|
p := tea.NewProgram(
|
||||||
|
game.Model(),
|
||||||
|
tea.WithAltScreen(),
|
||||||
|
tea.WithMouseCellMotion(),
|
||||||
|
)
|
||||||
|
|
||||||
err := p.Start()
|
err := p.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -68,3 +68,7 @@ const (
|
|||||||
H7 = "H7"
|
H7 = "H7"
|
||||||
H8 = "H8"
|
H8 = "H8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s Square) String() string {
|
||||||
|
return string(s)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user