-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
60 lines (53 loc) · 1.7 KB
/
move.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
package main
type Move struct {
StartRow int
StartCol int
EndRow int
EndCol int
PieceMoved string
PieceCaptured string
IsPawnPromotion bool
IsEnPassant bool
IsCastleMove bool
MoveId int
}
func NewMove(startSquare Square, endSquare Square, boardState BoardState, isEnPassant bool, isCastleMove bool) Move {
pieceMoved := boardState[startSquare.row][startSquare.col]
isPawnPromotion := (pieceMoved == "wp" && endSquare.row == 0) || (pieceMoved == "bp" && endSquare.row == 7)
return Move{
StartRow: startSquare.row,
StartCol: startSquare.col,
EndRow: endSquare.row,
EndCol: endSquare.col,
PieceMoved: pieceMoved,
PieceCaptured: boardState[endSquare.row][endSquare.col],
IsPawnPromotion: isPawnPromotion,
IsEnPassant: isEnPassant,
MoveId: startSquare.row * 1000 + startSquare.col * 100 + endSquare.row * 10 + endSquare.col,
IsCastleMove: isCastleMove,
}
}
func (m *Move) GetChessNotation() string {
return m.getSquareNotationFromIndexes(m.StartRow, m.StartCol) + " - " + m.getSquareNotationFromIndexes(m.EndRow, m.EndCol)
}
func (m *Move) getSquareNotationFromIndexes(row int, col int) string {
rowsToRank := map[int]string{
0: "8", 1: "7", 2: "6", 3: "5", 4: "4", 5: "3", 6: "2", 7: "1",
}
colsToFile := map[int]string{
0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", 7: "h",
}
return colsToFile[col] + rowsToRank[row]
}
func (m *Move) getSquareFromNotation(square string) Square {
rankToRows := map[string]int{
"8": 0, "7": 1, "6": 2, "5": 3, "4": 4, "3": 5, "2": 6, "1": 7,
}
fileToCols := map[string]int{
"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7,
}
return Square{
row: rankToRows[string(square[1])],
col: fileToCols[string(square[0])],
}
}