Permalink
Browse files

New function to get the string representation of a move

  • Loading branch information...
1 parent 6d98e05 commit 593bcabdcc2e61f67d9e241c47dd80cc7ff488fe @fcardinaux committed Aug 15, 2011
Showing with 13 additions and 5 deletions.
  1. BIN beams/chessfold.beam
  2. +10 −3 erl/chessfold.erl
  3. +3 −2 erl/chessfold.hrl
View
Binary file not shown.
View
@@ -16,6 +16,7 @@
string_to_position/1,
position_to_string/1,
position_to_string_without_counters/1,
+ move_to_string/1,
all_possible_moves/1,
all_possible_moves_from/2,
position_after_move/1,
@@ -359,6 +360,12 @@ position_to_string_without_counters(
en_passant_square_to_string(SquareNumber) ->
square_to_string(SquareNumber).
+%% -----------------------------------------------------------------------------
+%% @doc Transform a move tuple into a string. This string can be used by javascript code.
+%% @spec move_to_string(#chessfold_move{}) -> string()
+move_to_string(Move) ->
+ square_to_string(chessfold:move_origin(Move)) ++ square_to_string(chessfold:move_target(Move)).
+
%% -----------------------------------------------------------------------------
%% @doc Get all possible moves from a specific position
%% @spec all_possible_moves(#chessfold_position{}) -> list(#chessfold_move{})
@@ -482,8 +489,8 @@ square_to_string(#chessfold_piece{square = SquareNumber}) ->
square_to_string(SquareNumber) when SquareNumber > ?TOP_RIGHT_CORNER -> throw({invalid_square_number, SquareNumber});
square_to_string(SquareNumber) when SquareNumber < ?BOTTOM_LEFT_CORNER -> throw({invalid_square_number, SquareNumber});
square_to_string(SquareNumber) ->
- RowValue = SquareNumber div ?ROW_SPAN,
- ColValue = SquareNumber rem ?ROW_SPAN,
+ RowValue = SquareNumber div ?ROW_SPAN, % 0x88 representation
+ ColValue = SquareNumber rem ?ROW_SPAN, % 0x88 representation
[ColValue + $a, RowValue + $1].
allowed_castling_value_to_string(AllowedCastling) ->
@@ -535,7 +542,7 @@ is_square_in_attack(Pieces, AttackingPieceColor, AttackedSquare) ->
AttackArrayKey = try AttackedSquare - AttackingSquare + 129 of % Not 128 because ?ATTACK_ARRAY keys start with 1
Value -> Value
catch
- Error:Reason -> ?NYI({AttackingSquare, AttackedSquare, Error, Reason})
+ Error:Reason -> ?debug("~p: ~p~n", [Error, {Reason, AttackingSquare, AttackedSquare, Error, Reason}])
end,
PiecesAbleToAttack = lists:nth(AttackArrayKey, ?ATTACK_ARRAY),
AttackingPieceType = Piece#chessfold_piece.type,
View
@@ -58,8 +58,9 @@
taken = false :: 'false' | #chessfold_piece{}}). % Not necessarily the same square as 'to' (en passant)
% Source: Programming Erlang, p 424
--define(NYI(X), (begin
- io:format("*** NYI ~p ~p ~p~n", [?MODULE, ?LINE, X])
+-define(debug(X, Y), (begin
+ Info = io_lib:format(X, Y),
+ io:format("~nDebug in module ~p, line ~p:~n~p~n", [?MODULE, ?LINE, Info])
end)).

0 comments on commit 593bcab

Please sign in to comment.