mirror of
https://github.com/maaslalani/gambit.git
synced 2024-12-30 21:19:52 +02:00
feat: draw board
This commit is contained in:
parent
50bca39dbc
commit
980000e72b
23
board.go
23
board.go
@ -1,17 +1,30 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Board struct {
|
||||
Players []Player
|
||||
Grid [8][8]string
|
||||
}
|
||||
|
||||
func (b Board) String() string {
|
||||
var s string
|
||||
func (b *Board) Draw() {
|
||||
for _, player := range b.Players {
|
||||
for _, piece := range player.Pieces {
|
||||
s += fmt.Sprint(piece)
|
||||
b.Grid[piece.Position[0]-1][piece.Position[1]-1] = piece.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Board) String() string {
|
||||
var s string
|
||||
for row := 0; row < 8; row++ {
|
||||
for col := 0; col < 8; col++ {
|
||||
s += " "
|
||||
if b.Grid[row][col] == "" {
|
||||
s += " "
|
||||
} else {
|
||||
s += b.Grid[row][col]
|
||||
}
|
||||
}
|
||||
s += "\n"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -25,6 +25,8 @@ type model struct {
|
||||
func (m model) Init() tea.Cmd { return nil }
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.Board.Draw()
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
|
2
piece.go
2
piece.go
@ -92,7 +92,7 @@ func InitialPieces(color color) []Piece {
|
||||
}
|
||||
|
||||
for i, p := range []piece{Rook, Knight, Bishop, Queen, King, Knight, Bishop, Rook} {
|
||||
pieces = append(pieces, NewPiece(p, position{backRank, i}, color))
|
||||
pieces = append(pieces, NewPiece(p, position{backRank, i + 1}, color))
|
||||
}
|
||||
|
||||
return pieces
|
||||
|
Loading…
Reference in New Issue
Block a user