-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.lua
executable file
·118 lines (78 loc) · 2.25 KB
/
Bot.lua
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
112
113
114
115
116
117
118
local array = require 'lib.array'
local inspect = require 'lib.inspect'
Bot = {}
Bot.attackedSquares = {}
function Bot:new()
local b = {}
self.__index = self
return setmetatable(b, self)
end
function Bot:afterMove()
end
function Bot:findAttackedSquares()
self.attackedSquares = {}
for i,p in ipairs(chessBoard.pieces) do
if p.color ~= chessBoard.hasTurn and p.tempDead == false then
if p.type ~= 'pawn' then
local moves = p:getPossibleMoves()
self.attackedSquares = array.concat(self.attackedSquares, moves)
elseif p.type == 'pawn' then
local attacks = p:getControlledSquares()
self.attackedSquares = array.concat(self.attackedSquares, attacks)
end
end
end
return array.uniq(self.attackedSquares)
end
function Bot:undieAll()
for i,p in ipairs(chessBoard.pieces) do
p.tempDead = false
end
end
function Bot:willItsKingBeCheckedAfterPieceMoves(piece_id, potential_square)
pieceInSquareIndex = chessBoard:pieceInSquare(potential_square)
local result = false
if pieceInSquareIndex ~= nil then
chessBoard.pieces[pieceInSquareIndex].tempDead = true
end
local original_square = chessBoard.pieces[piece_id].square
--changing one of the most important global objects temporarily
chessBoard.pieces[piece_id].square = potential_square
self.attackedSquares = {}
local hisMajesty = {}
for i,p in ipairs(chessBoard.pieces) do
if p.color == chessBoard.hasTurn and p.type == 'king' then
hisMajesty = p
end
end
local attackedSquares = self:findAttackedSquares()
if array.index_of(attackedSquares, hisMajesty.square) ~= -1 then
result = true
end
chessBoard.pieces[piece_id].square = original_square
self:undieAll()
return result
end
function Bot:evaluatePosition(pieces, board)
local evaluation = 0
if board.hasTurn == 'white' then
evaluation = evaluation + 0.3
else
evaluation = evaluation - 0.3
end
for i,p in ipairs(pieces) do
local column = p.column
if p.color == 'white' then
evaluation = evaluation + p.points
if p.type == 'pawn' and column > 4 then
evaluation = evaluation + (column - 4) * 0.1
end
else
evaluation = evaluation - p.points
if p.type == 'pawn' and column < 5 then
evaluation = evaluation + (column - 5) * 0.1
end
end
end
return evaluation
end