Skip to content

Commit

Permalink
chess: make to and from on Move private
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ff committed Nov 26, 2023
1 parent e32a1ef commit 14d0db6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
28 changes: 19 additions & 9 deletions internal/chess/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const (
)

type Move struct {
From Square
To Square
from Square
to Square

moveType MoveType
flags MoveFlag
Expand All @@ -55,15 +55,25 @@ var NullMove = NewMove(A1, A1, Null, NoMoveFlag)
// NewMove creates a new move with the given from and to.
func NewMove(from, to Square, moveType MoveType, flags MoveFlag) Move {
return Move{
From: from,
To: to,
from: from,
to: to,
moveType: moveType,
flags: flags,
promotionPiece: EmptyPiece,
capturePiece: EmptyPiece,
}
}

// From returns the square the piece is moving from.
func (m Move) From() Square {
return m.from
}

// To returns the square the piece is moving to.
func (m Move) To() Square {
return m.to
}

// WithPromotion sets that the move will result with the moving piece being promoted to the given piece.
func (m *Move) WithPromotion(piece Piece) {
m.flags |= PromotionMoveFlag
Expand All @@ -89,16 +99,16 @@ func (m Move) PromotionPiece() Piece {
// RankDifference calculates the difference in ranks between the from
// square and the to square.
func (m Move) RankDifference() int {
from := float64(m.From.Rank())
to := float64(m.To.Rank())
from := float64(m.from.Rank())
to := float64(m.to.Rank())
return int(math.Abs(from - to))
}

// FileDifference calculates the difference in files between the from
// square and the to square.
func (m Move) FileDifference() int {
from := float64(m.From.File())
to := float64(m.To.File())
from := float64(m.from.File())
to := float64(m.to.File())
return int(math.Abs(from - to))
}

Expand Down Expand Up @@ -130,7 +140,7 @@ func (m Move) String() string {
return "0000"
}

str := m.From.ToAlgebraic() + m.To.ToAlgebraic()
str := m.from.ToAlgebraic() + m.to.ToAlgebraic()

if m.promotionPiece != EmptyPiece {
character := string(m.promotionPiece.Character())
Expand Down
4 changes: 2 additions & 2 deletions internal/chess/movegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ func (p Position) isLegalMove(move Move) bool {
// check that the squares in between the king and rook are not attacked
if move.Type() == CastleMove {
direction := east
if move.To == C1 || move.To == C8 {
if move.To() == C1 || move.To() == C8 {
direction = west
}

difference := move.FileDifference()
for i := 0; i <= difference; i++ {
square := move.From + Square(difference*int(direction))
square := move.From() + Square(difference*int(direction))
if p.IsSquareAttackedBy(square, p.turn.OpposingSide()) {
return false
}
Expand Down
41 changes: 22 additions & 19 deletions internal/chess/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func (p *Position) clearPiece(square Square) {

// MakeMove applies the move to the current position.
func (p *Position) MakeMove(move Move) error {
movingPiece, err := p.GetPieceAt(move.From)
movingPiece, err := p.GetPieceAt(move.From())
if err != nil {
return fmt.Errorf("%w: %w", ErrInvalidMove, err)
}
Expand All @@ -573,6 +573,9 @@ func (p *Position) MakeMove(move Move) error {
return fmt.Errorf("%w: trying to capture piece of same color with %s", ErrInvalidMove, move)
}

to := move.To()
from := move.From()

copy := p.Copy()

p.enPassant = -1 // clear en passant square, this will be set later if needed
Expand All @@ -584,15 +587,15 @@ func (p *Position) MakeMove(move Move) error {
opposingSide := p.turn.OpposingSide()

// check to see if a pawn is on a valid square for en passant
westPawn, _ := p.GetPieceAt(move.To + Square(west))
eastPawn, _ := p.GetPieceAt(move.To + Square(east))
if (westPawn.Type() == Pawn && westPawn.Color() == opposingSide && move.To.File() != 1) || (eastPawn.Type() == Pawn && eastPawn.Color() == opposingSide && move.To.File() != 8) {
p.enPassant = move.To + Square(pawnDirection(opposingSide))
westPawn, _ := p.GetPieceAt(to + Square(west))
eastPawn, _ := p.GetPieceAt(to + Square(east))
if (westPawn.Type() == Pawn && westPawn.Color() == opposingSide && to.File() != 1) || (eastPawn.Type() == Pawn && eastPawn.Color() == opposingSide && to.File() != 8) {
p.enPassant = to + Square(pawnDirection(opposingSide))
}
}

if movingPiece.Type() == Rook {
switch move.From {
switch from {
case A1:
p.castlingRights &= ^WhiteCastleQueenside
break
Expand All @@ -617,10 +620,10 @@ func (p *Position) MakeMove(move Move) error {
}

if move.Captures() {
p.clearPiece(move.To)
p.clearPiece(to)

if capturePiece.Type() == Rook {
switch move.To {
switch to {
case A1:
p.castlingRights &= ^WhiteCastleQueenside
break
Expand All @@ -637,23 +640,23 @@ func (p *Position) MakeMove(move Move) error {
}
}

p.clearPiece(move.From)
p.setPiece(move.To, movingPiece)
p.clearPiece(from)
p.setPiece(to, movingPiece)

if move.IsPromotion() {
p.clearPiece(move.To) // remove the original piece
p.clearPiece(to) // remove the original piece

// place the newly promoted piece
p.setPiece(move.To, move.PromotionPiece())
p.setPiece(to, move.PromotionPiece())
}
break
case CastleMove:
// move the king to it's new square
p.clearPiece(move.From)
p.setPiece(move.To, movingPiece)
p.clearPiece(from)
p.setPiece(to, movingPiece)

// move the rook to it's new square
switch move.To {
switch to {
case C1:
rook, _ := p.GetPieceAt(A1)
p.clearPiece(A1)
Expand Down Expand Up @@ -684,11 +687,11 @@ func (p *Position) MakeMove(move Move) error {
break
case EnPassantMove:
// move the pawn to it's new square
p.clearPiece(move.From)
p.setPiece(move.To, movingPiece)
p.clearPiece(from)
p.setPiece(to, movingPiece)

// remove the captured pawn
captureSquare := move.To + Square(pawnDirection(p.turn.OpposingSide()))
captureSquare := to + Square(pawnDirection(p.turn.OpposingSide()))
p.clearPiece(captureSquare)
break
}
Expand Down Expand Up @@ -810,7 +813,7 @@ func (p *Position) updateAttackers() {

moves := append(ourAttacks, theirAttacks...)
for _, move := range moves {
p.attackersBB[move.To].SetBit(uint64(move.From))
p.attackersBB[move.To()].SetBit(uint64(move.From()))
}
}

Expand Down

0 comments on commit 14d0db6

Please sign in to comment.