1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-11-24 08:22:12 +02:00
This commit is contained in:
Maas Lalani 2021-12-22 12:14:07 -05:00
parent 9a0d34e37d
commit 55caa3afbc
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
6 changed files with 43 additions and 6 deletions

View File

@ -8,7 +8,7 @@ type Board struct {
// The board is represented as a 2D array of cells.
// The first index is the row, the second is the column.
Grid [8][8]piece.Piece
reversed bool
Reversed bool
Turn piece.Color
}

View File

@ -34,7 +34,7 @@ const (
func (b Board) String() string {
var s string
if b.reversed {
if b.Reversed {
ranks = reverse(ranks)
}

View File

@ -34,7 +34,7 @@ func TestDisplay(t *testing.T) {
func TestDisplayReversed(t *testing.T) {
b := New()
b.reversed = true
b.Reversed = true
expected := `
1

View File

@ -3,10 +3,14 @@ package game
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/maaslalani/gambit/board"
"github.com/maaslalani/gambit/position"
. "github.com/maaslalani/gambit/squares"
)
type model struct {
board board.Board
board board.Board
selected *position.Position
moves []board.Move
}
func Model() tea.Model {
@ -17,10 +21,35 @@ func Model() tea.Model {
}
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) {
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:
switch msg.String() {
case "ctrl+c", "q":

View File

@ -8,7 +8,11 @@ import (
)
func main() {
p := tea.NewProgram(game.Model())
p := tea.NewProgram(
game.Model(),
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
err := p.Start()
if err != nil {

View File

@ -68,3 +68,7 @@ const (
H7 = "H7"
H8 = "H8"
)
func (s Square) String() string {
return string(s)
}