-
Notifications
You must be signed in to change notification settings - Fork 2
/
board.go
111 lines (99 loc) · 2.25 KB
/
board.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package chess_engine
import (
"strconv"
)
type Board []Piece
func NewBoard() Board {
board := make([]Piece, 64)
for i := 0; i < 64; i++ {
board[i] = NoPiece
}
return board
}
func (b Board) IsEmpty(pos Position) bool {
return b[pos] == NoPiece
}
func (b Board) IsColor(pos Position, color Color) bool {
return b[pos].Color() == color
}
func (b Board) IsOppositeColor(pos Position, color Color) bool {
return b[pos].Color() == color.Opposite()
}
func (b Board) IsOpposingPiece(pos Position, c Color) bool {
return b[pos] != NoPiece && b[pos].Color() != c
}
func (b Board) FindPieceOnLine(line []Position) Position {
for _, pos := range line {
if b[pos] == NoPiece {
continue
} else {
return pos
}
}
return NoPosition
}
func (b Board) CanCastle(a SquareControl, color Color, from, to Position) bool {
for p := from; p <= to; p++ {
if !b.IsEmpty(p) {
return false
} else if a.AttacksSquare(color.Opposite(), p) {
return false
}
}
return true
}
func (b Board) ApplyMove(from, to Position) Piece {
capture := b[to]
b[to] = b[from]
b[from] = NoPiece
return capture
}
func (b Board) Copy() Board {
result := make([]Piece, 64)
copy(result, b)
return result
}
func (b Board) HasClearLineTo(from, to Position) bool {
vector := NewMove(from, to).Vector().Normalize()
for _, pos := range vector.FollowVectorUntilEdgeOfBoard(to) {
if pos == from {
return true
} else if b[pos] == NoPiece {
continue
} else {
return false
}
}
return true
}
func (b Board) String() string {
result := " +-------------------------------+\n"
characters := map[Piece]string{
NoPiece: " ",
WhiteKing: "♔",
WhiteQueen: "♕",
WhiteRook: "♖",
WhiteBishop: "♗",
WhiteKnight: "♘",
WhitePawn: "♙",
BlackKing: "♚",
BlackQueen: "♛",
BlackRook: "♜",
BlackBishop: "♝",
BlackKnight: "♞",
BlackPawn: "♟",
}
for rank := 7; rank >= 0; rank-- {
result += " " + strconv.Itoa(rank+1) + " |"
for file := 0; file <= 7; file++ {
result += " " + characters[b[rank*8+file]] + " |"
}
result += "\n"
if rank != 0 {
result += " +-------------------------------+\n"
}
}
result += " +-------------------------------+\n"
result += " a b c d e f g h\n"
return result
}