-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnewboard.erl
More file actions
244 lines (220 loc) · 9.46 KB
/
newboard.erl
File metadata and controls
244 lines (220 loc) · 9.46 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
%%% @author John Daily <jd@epep.us>
%%% @copyright (C) 2012, John Daily
%%% @doc
%%%
%%% @end
%%% Created : 6 Sep 2012 by John Daily <jd@epep.us>
-module(newboard).
-compile(export_all).
-include_lib("records.hrl").
-import(orddict, [ new/0, store/3, find/2 ]).
-import(newpiece).
-record(boardstate, { pieces, %% Compiler doesn't like :: orddict()
whiteking={5, 1} :: square(),
blackking={5, 8} :: square()}).
-type boardstate() :: #boardstate{}.
init() ->
RowSequence =
[ { pieces, [ { rook, rookmove, rookmove },
{ knight, knightmove, knightmove },
{ bishop, bishopmove, bishopmove },
{ queen, queenmove, queenmove },
{ king, kingmove, kingmove },
{ bishop, bishopmove, bishopmove },
{ knight, knightmove, knightmove },
{ rook, rookmove, rookmove }
]
},
{ pawns, [ { pawn, pawnmove, pawncap },
{ pawn, pawnmove, pawncap },
{ pawn, pawnmove, pawncap },
{ pawn, pawnmove, pawncap },
{ pawn, pawnmove, pawncap },
{ pawn, pawnmove, pawncap },
{ pawn, pawnmove, pawncap },
{ pawn, pawnmove, pawncap }
]
}
],
Board = initialize(new(), RowSequence),
MyState = #boardstate{pieces=Board},
spawn(?MODULE, board_loop, [ MyState ]).
board_loop(MyState) ->
receive
{ Pid, showboard } ->
io:format("~s~n", [matrix_to_text(MyState#boardstate.pieces)]),
Pid ! io_lib:format("~s~n", [matrix_to_text(MyState#boardstate.pieces)]),
board_loop(MyState);
{ Pid, move, Start, End } ->
case evaluate_legal_move(MyState, move, Start, End) of
{ true, Piece, undefined } ->
%% Don't forget to update history on the piece
NewDict =
store(Start, #piece{type=none},
store(End, Piece, MyState#boardstate.pieces)
),
%% Must also update whiteking/blackking if one of them moves
NewState = #boardstate{ whiteking=MyState#boardstate.whiteking,
blackking=MyState#boardstate.blackking,
pieces=NewDict },
IsCheckOrMate = check_or_mate(NewState),
Pid ! { move, true, IsCheckOrMate },
case IsCheckOrMate of
{ checkmate, WhoWins } ->
io:format("~s~n", [matrix_to_text(NewState#boardstate.pieces)]),
exit(WhoWins);
_ ->
nothing_to_do
end,
board_loop(NewState);
{ false, Reason } ->
Pid ! { false, Reason },
board_loop(MyState)
end;
{ Pid, examine, Square} ->
{ ok, Piece } = find(Square, MyState#boardstate.pieces),
Pid ! Piece,
board_loop(MyState)
end.
%% XXX populate later
check_or_mate(_State) ->
none.
%% If a move/capture would fail, returns { false, "reason" }
%% If there is no piece at the square, today would error out, should fix that
%% Castling is not yet supported
%% If a capture would succeed, returns { true, MovePiece, CapturePiece }
%% If a non-capture move would succeed, returns { true, MovePiece, undefined }
-spec evaluate_legal_move(boardstate(), movetype(), square(), square()) ->
{ 'true', piece(), piece()|'undefined' }|
{ 'false', string() }.
evaluate_legal_move(_State, castle, _Start, _End) ->
{ false, "Castling not yet supported" };
evaluate_legal_move(State, MoveType, Start, End) ->
{ ok, Piece } = find(Start, State#boardstate.pieces),
{ CanMove, Traverse } =
newpiece:testmove(#move{
piece=Piece,
start=Start,
target=End,
movetype=MoveType
}),
case MoveType of
move ->
%% Must make sure no piece at end as well as nothing in the path
evaluate_legal_aux(CanMove, move, Traverse ++ End, Start, End, Piece, State);
capture ->
%% Make sure there is a piece at the target before wasting our time
case find(End, State#boardstate.pieces) of
{ ok, #piece{type=none} } ->
{ false, io_lib:format("No piece at ~p to capture", [End]) };
_ ->
evaluate_legal_aux(CanMove, capture, Traverse, Start, End, Piece, State)
end;
_ ->
{ false, "Castle is not yet implemented" }
end.
-spec evaluate_legal_aux(boolean(), movetype(), list(square()),
square(), square(), piece(), boardstate()) ->
{ 'true', piece(), piece()|'undefined' }|
{ 'false', string() }.
%% First, illegal moves
evaluate_legal_aux(false, _, _, _, _, Piece, _) ->
{ false, io_lib:format("Illegal move for ~s", [Piece#piece.type]) };
%% No piece in the way, check for pins
evaluate_legal_aux(true, move, [], Start, _End, Piece, State) ->
case look_for_pins(Start, Piece#piece.team,
State#boardstate.whiteking, State#boardstate.blackking) of
{ true, PinningPiece, Where } ->
{ false, io_lib:format("Piece pinned by ~s at ~p",
[PinningPiece#piece.type,
Where]) };
false ->
{ true, Piece, undefined }
end;
%% Attempted capture, no piece in the way
evaluate_legal_aux(true, capture, [], Start, End, Piece, State) ->
case look_for_pins(Start, Piece#piece.team,
State#boardstate.whiteking, State#boardstate.blackking) of
{ true, PinningPiece, Where } ->
{ false, io_lib:format("Piece pinned by ~s at ~p",
[PinningPiece#piece.type,
Where]) };
false ->
{ ok, Captured } = find(End, State#boardstate.pieces),
{ true, Piece, Captured }
end;
%% Attempted move or capture, evaluate traversal squares for obstacles
evaluate_legal_aux(true, MoveType, [H | T], Start, End, Piece, State) ->
case find(H, State#boardstate.pieces) of
{ ok, #piece{type=none} } ->
evaluate_legal_aux(true, MoveType, T, Start, End, Piece, State);
{ ok, #piece{type=Obstacle} } ->
{ false, io_lib:format("Blocking piece (~s) at ~p", [Obstacle, H]) }
end.
%% XXX populate later
-spec look_for_pins(square(), side(), square(), square()) ->
{ 'true', piece(), square() } |
'false'.
look_for_pins(Square, white, _WhiteKing, _BlackKing) ->
false;
look_for_pins(_Square, black, _WhiteKing, _BlackKing) ->
false.
matrix_to_text(Matrix) ->
row_to_text(8, 1, Matrix, []).
%% X and Y are swapped here because we're focusing on rows. Made more
%% sense when I had a tuple of rows to navigate as my data structure.
row_to_text(0, _, _, Accum) ->
Accum ++ "\n";
row_to_text(Y, 9, Matrix, Accum) ->
row_to_text(Y - 1, 1, Matrix, ["\n\n|" | Accum]);
row_to_text(Y, X, Matrix, Accum) ->
row_to_text(Y, X + 1, Matrix, [ cell_to_text({X, Y}, Matrix) ++ "|" | Accum ]).
cell_to_text(Square, Matrix) ->
{ok, #piece{type=Type}} = find(Square, Matrix),
piece_to_shorthand(Type).
piece_to_shorthand(Piece) ->
case Piece of
none ->
" ";
pawn ->
" p ";
rook ->
" R ";
knight ->
" N ";
bishop ->
" B ";
queen ->
" Q ";
king ->
" K "
end.
initialize(Board, Proplist) ->
Pieces = proplists:get_value(pieces, Proplist),
Pawns = proplists:get_value(pawns, Proplist),
%% The Board dictionary gets revised with each call to initialize_row
Board1 = initialize_row(Board, Pieces, {1, 1}, white),
Board2 = initialize_row(Board1, Pawns, {1, 2}, white),
Board3 = initialize_blanks(Board2, 1, 3),
Board4 = initialize_row(Board3, Pawns, {1, 7}, black),
initialize_row(Board4, Pieces, {1, 8}, black).
initialize_blanks(Board, _, 7) ->
Board;
initialize_blanks(Board, 9, Y) ->
initialize_blanks(Board, 1, Y + 1);
initialize_blanks(Board, X, Y) ->
initialize_blanks(store({X, Y}, #piece{type=none}, Board), X + 1, Y).
initialize_row(Board, [], _, _) ->
Board;
initialize_row(Board, [{Piece, M, C}|T], {X, Y}, Team) ->
Move = proplists:get_value(M, newpiece:movefuns()),
Capture = proplists:get_value(C, newpiece:movefuns()),
initialize_row(store({X, Y}, #piece{type=Piece,
team=Team,
movefun=Move,
capturefun=Capture},
Board),
T, {X + 1, Y}, Team).
%% Pieces = [ { rook, [ { 1, 1 }, { 8, 1 }, { 1, 8 }, { 8, 8 } ] },
%% { bishop, [ { 2, 1 }, { 7, 1 }, { 2, 8 }, { 7, 8 } ] },
%% { knight, [ { 3, 1 }, { 6,