-
Notifications
You must be signed in to change notification settings - Fork 0
/
king.go
38 lines (31 loc) · 816 Bytes
/
king.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"math"
)
type King struct {
side Side
sprite *ebiten.Image
}
func NewKing(side Side) King {
img := getSprite(side, "King")
return King{
side: side,
sprite: img,
}
}
// Sprite implements Piece.
func (k *King) Sprite() *ebiten.Image {
return k.sprite
}
// IsValidMove implements Piece.
func (k *King) IsValidMove(board *Board, x1 int, y1 int, x2 int, y2 int) bool {
// Valid King move, if the piece moves from (X1, Y1) to (X2, Y2), the move is valid if and only if |X2-X1|<=1 and |Y2-Y1|<=1.
other := board.PieceAt(x2, y2)
return math.Abs(float64(x2-x1)) <= 1 && math.Abs(float64(y2-y1)) <= 1 && (other == nil || k.Side() != other.Side())
}
// Side implements Piece.
func (k *King) Side() Side {
return k.side
}
var _ Piece = &King{}