Skip to content
This repository has been archived by the owner on Feb 18, 2018. It is now read-only.

Commit

Permalink
It now shows the number of pieces left for each player
Browse files Browse the repository at this point in the history
  • Loading branch information
mads-hartmann committed Aug 20, 2011
1 parent 2000989 commit 831f355
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions resources/board.xmlt
Expand Up @@ -4,6 +4,7 @@
<li>Current player: <span id="color_of_current_player"></span></li>
<li>You're: <span id="color_of_player"></span></li>
<li>Name of the game: <span id="name_of_game"></span></li>
<li>Pieces: w:<span id="black_left"></span> b:<span id="white_left"></span></li>
</ul>
</div>
<table border="0" cellspacing="5" cellpadding="5">
Expand Down
25 changes: 23 additions & 2 deletions src/board.opa
Expand Up @@ -34,6 +34,7 @@ Board = {{
(
do iteri(board, [colorize(_,_,_,_), labelize(_,_,_,_)])
do place_pieces(board)
do update_counters(board)
void
)

Expand All @@ -54,7 +55,27 @@ Board = {{
void
)

update(board: board) = place_pieces(board)
update(board: board) =
(
do place_pieces(board)
do update_counters(board)
void
)

update_counters(board: board) =
(
(blacks,whites) = Map.To.val_list(board.chess_positions)
|> List.collect(Map.To.val_list(_),_)
|> List.fold(pos,acc -> (
(whites,blacks) = acc
match pos.piece with
| { some = ~{color kind}} -> if color == {white} then (whites+1,blacks) else (whites,blacks+1)
| {none} -> acc
), _, (0,0))
do Dom.set_text(#black_left, Int.to_string(blacks))
do Dom.set_text(#white_left, Int.to_string(whites))
void
)

/*
* Given a row, column, board it will return some with a chess position if there is a
Expand Down Expand Up @@ -158,7 +179,7 @@ Board = {{
Map.replace(posTo.number, (oldPos -> { oldPos with piece = posFrom.piece}), rows)
), chess_positions)
{ chess_positions = chess_positions2 current_color = next_color }
)
)

/*
Method for applying a list of functions on every td dom element in the board.
Expand Down

0 comments on commit 831f355

Please sign in to comment.