mirror of
https://github.com/maaslalani/gambit.git
synced 2024-12-26 20:54:07 +02:00
moves
This commit is contained in:
parent
dbada55245
commit
3cf4ea492e
35
game/game.go
35
game/game.go
@ -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
37
moves/moves.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user