1
0
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:
Maas Lalani 2021-11-26 22:52:33 -05:00
parent 50bca39dbc
commit 980000e72b
No known key found for this signature in database
GPG Key ID: F53774FA051C052A
3 changed files with 21 additions and 6 deletions

View File

@ -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
}

View File

@ -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() {

View File

@ -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