fix bug that prevented king from moving because server thought it wanted to castling (but it was not a castling move)
This commit is contained in:
parent
a55a46e866
commit
d40c757776
@ -296,3 +296,34 @@ func Test_Board_BlackCastlesRight_NoRook(t *testing.T) {
|
|||||||
|
|
||||||
assert.False(t, valid)
|
assert.False(t, valid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Board_fixBugThatPreventsCastling(t *testing.T) {
|
||||||
|
board := newBoard()
|
||||||
|
board.Init()
|
||||||
|
|
||||||
|
//white move
|
||||||
|
valid, violation := board.CheckAndPlay(&types.Move{StartSquare: types.Coordinate{Col: 5, Row: 2}, EndSquare: types.Coordinate{Col: 5, Row: 3}})
|
||||||
|
assert.True(t, valid)
|
||||||
|
assert.Empty(t, violation)
|
||||||
|
//black move
|
||||||
|
valid, violation = board.CheckAndPlay(&types.Move{StartSquare: types.Coordinate{Col: 5, Row: 7}, EndSquare: types.Coordinate{Col: 5, Row: 6}})
|
||||||
|
assert.True(t, valid)
|
||||||
|
assert.Empty(t, violation)
|
||||||
|
//white move
|
||||||
|
valid, violation = board.CheckAndPlay(&types.Move{StartSquare: types.Coordinate{Col: 4, Row: 2}, EndSquare: types.Coordinate{Col: 4, Row: 3}})
|
||||||
|
assert.True(t, valid)
|
||||||
|
assert.Empty(t, violation)
|
||||||
|
//black move
|
||||||
|
valid, violation = board.CheckAndPlay(&types.Move{StartSquare: types.Coordinate{Col: 6, Row: 7}, EndSquare: types.Coordinate{Col: 6, Row: 6}})
|
||||||
|
assert.True(t, valid)
|
||||||
|
assert.Empty(t, violation)
|
||||||
|
//queen moves to check the king
|
||||||
|
valid, violation = board.CheckAndPlay(&types.Move{StartSquare: types.Coordinate{Col: 4, Row: 1}, EndSquare: types.Coordinate{Col: 8, Row: 5}})
|
||||||
|
assert.True(t, valid)
|
||||||
|
assert.Empty(t, violation)
|
||||||
|
|
||||||
|
//this moves should be valid but it was not because of a bug
|
||||||
|
valid, violation = board.CheckAndPlay(&types.Move{StartSquare: types.Coordinate{Col: 5, Row: 8}, EndSquare: types.Coordinate{Col: 5, Row: 7}})
|
||||||
|
assert.True(t, valid)
|
||||||
|
assert.Empty(t, violation)
|
||||||
|
}
|
||||||
|
@ -30,6 +30,10 @@ func (k King) AfterMoveAction(board *Board, fromSquare types.Coordinate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k King) HandleCastling(board *Board, move types.Move) (bool, Violation) {
|
func (k King) HandleCastling(board *Board, move types.Move) (bool, Violation) {
|
||||||
|
if !k.isMoveCastlingMove(board, move) {
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
if k.hadMovedBefore(board) {
|
if k.hadMovedBefore(board) {
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
@ -66,6 +70,28 @@ const (
|
|||||||
CastlingLeft CastlingDirection = "left"
|
CastlingLeft CastlingDirection = "left"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (k King) isMoveCastlingMove(b *Board, move types.Move) bool {
|
||||||
|
var destinationSquareForKingRight types.Coordinate
|
||||||
|
var destinationSquareForKingLeft types.Coordinate
|
||||||
|
|
||||||
|
switch k.Color {
|
||||||
|
case types.White:
|
||||||
|
destinationSquareForKingRight = types.Coordinate{Col: 7, Row: 1}
|
||||||
|
destinationSquareForKingLeft = types.Coordinate{Col: 3, Row: 1}
|
||||||
|
|
||||||
|
case types.Black:
|
||||||
|
destinationSquareForKingRight = types.Coordinate{Col: 7, Row: 8}
|
||||||
|
destinationSquareForKingLeft = types.Coordinate{Col: 3, Row: 8}
|
||||||
|
}
|
||||||
|
|
||||||
|
if move.EndSquare == destinationSquareForKingRight ||
|
||||||
|
move.EndSquare == destinationSquareForKingLeft {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (k King) isCastlingAllowed(b *Board, move types.Move) (bool, CastlingDirection) {
|
func (k King) isCastlingAllowed(b *Board, move types.Move) (bool, CastlingDirection) {
|
||||||
var valid = false
|
var valid = false
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ var (
|
|||||||
SomethingWentWrong Violation = "something went wrong"
|
SomethingWentWrong Violation = "something went wrong"
|
||||||
CastlingThroughCheck Violation = "king would move through check"
|
CastlingThroughCheck Violation = "king would move through check"
|
||||||
CastlingWhileKingInCheck Violation = "king cannot castle while in check"
|
CastlingWhileKingInCheck Violation = "king cannot castle while in check"
|
||||||
|
CastlingKingMovedBefore Violation = "king cannot caslte because he moved before"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v Violation) String() string {
|
func (v Violation) String() string {
|
||||||
|
Loading…
Reference in New Issue
Block a user