1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-12-26 20:54:07 +02:00
This commit is contained in:
Maas Lalani 2021-12-24 19:08:48 -05:00
parent dbada55245
commit 3cf4ea492e
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
2 changed files with 40 additions and 32 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/maaslalani/gambit/board"
"github.com/maaslalani/gambit/border"
"github.com/maaslalani/gambit/fen"
"github.com/maaslalani/gambit/moves"
"github.com/maaslalani/gambit/pieces"
"github.com/maaslalani/gambit/position"
. "github.com/maaslalani/gambit/style"
@ -82,7 +83,7 @@ func (m model) View() string {
display = Cyan(display)
}
if isLegalMove(m.pieceMoves, position.ToSquare(rr, c)) {
if moves.IsLegal(m.pieceMoves, position.ToSquare(rr, c)) {
if cell == "" {
display = "."
}
@ -146,7 +147,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// After a mouse click, we must generate the legal moves for the selected
// piece, if there is a newly selected piece
m.pieceMoves = legalPieceMoves(m.moves, m.selected)
m.pieceMoves = moves.LegalSelected(m.moves, m.selected)
case tea.KeyMsg:
switch msg.String() {
@ -157,33 +158,3 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
// isLegalMove determines whether it is legal to move the the destination
// square given a piece's legal moves
func isLegalMove(legalMoves []dt.Move, destination string) bool {
for _, move := range legalMoves {
if strings.HasSuffix(move.String(), destination) {
return true
}
}
return false
}
// legalPieceMoves returns the legal moves for a given piece this is usually
// for the selected piece so that we know to which we can move. If there is no
// selected piece we return an empty array of moves.
func legalPieceMoves(moves []dt.Move, selected string) []dt.Move {
var legalMoves []dt.Move
if selected == "" {
return legalMoves
}
for _, move := range moves {
if strings.HasPrefix(move.String(), selected) {
legalMoves = append(legalMoves, move)
}
}
return legalMoves
}

37
moves/moves.go Normal file
View File

@ -0,0 +1,37 @@
package moves
import (
"strings"
dt "github.com/dylhunn/dragontoothmg"
)
// IsLegal determines whether it is legal to move the the destination
// square given a piece's legal moves
func IsLegal(legalMoves []dt.Move, destination string) bool {
for _, move := range legalMoves {
if strings.HasSuffix(move.String(), destination) {
return true
}
}
return false
}
// LegalSelected returns the legal moves for a given piece this is usually
// for the selected piece so that we know to which we can move. If there is no
// selected piece we return an empty array of moves.
func LegalSelected(moves []dt.Move, selected string) []dt.Move {
var legalMoves []dt.Move
if selected == "" {
return legalMoves
}
for _, move := range moves {
if strings.HasPrefix(move.String(), selected) {
legalMoves = append(legalMoves, move)
}
}
return legalMoves
}