ptrelford

Reversi Kata

Apr 20th, 2012
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.67 KB | None | 0 0
  1. -module(reversi).
  2. -export([empty/0,update/2,open/0,find_legal_moves/2]).
  3.  
  4. empty() ->
  5.  Row=lists:duplicate(8,'.'),
  6.  lists:duplicate(8,Row).
  7.  
  8. index(XS) ->
  9.  [{I,X}||{I,X}<-lists:zip(lists:seq(1,length(XS)),XS)].
  10.  
  11. update(Board,Disks) ->
  12.  UpdateSquare=fun(ColI,RowI,OldValue) ->
  13.   Found=[Disk||{X,Y,Disk}<-Disks,X==ColI,Y==RowI],
  14.   case Found of [Disk] -> Disk; [] -> OldValue end
  15.  end,
  16.  [[UpdateSquare(ColI,RowI,Value)
  17.   ||{ColI,Value}<-index(Row)]
  18.   ||{RowI,Row}<-index(Board)].
  19.  
  20. open() ->
  21.  update(empty(),[{4,4,'W'},{5,4,'B'},{4,5,'B'},{5,5,'W'}]).
  22.  
  23. is_out_of_bounds(X,_) when X<1 orelse X>8 -> true;
  24. is_out_of_bounds(_,Y) when Y<1 orelse Y>8 -> true;
  25. is_out_of_bounds(_,_) -> false.
  26.  
  27. at(Board,X,Y) ->
  28.  Row=lists:nth(Y,Board),
  29.  lists:nth(X,Row).
  30.  
  31. find_legal_move(Board,{StartX,StartY},{DX,DY},Disk,Count) ->
  32.  X = StartX + DX, Y = StartY + DY,
  33.  Out=is_out_of_bounds(X,Y),
  34.  if
  35.   Out -> false;
  36.   not Out ->
  37.    Value=at(Board,X,Y),
  38.    case Value of
  39.     '.'  when Count > 0 -> {X,Y};
  40.     Disk -> find_legal_move(Board,{X,Y},{DX,DY},Disk,Count+1);
  41.     _    -> false
  42.    end
  43.  end.
  44.  
  45. find_legal_moves_at(Board,{X,Y}) ->
  46.  Disk=case at(Board,X,Y) of 'W' -> 'B'; 'B' -> 'W' end,
  47.  Vectors=
  48.   [{-1,-1},{0,-1},{ 1,-1},
  49.    {-1, 0},       { 1, 0},
  50.    {-1, 1},{0, 1},{ 1, 1}],
  51.  Results=[find_legal_move(Board,{X,Y},{DX,DY},Disk,0)||{DX,DY}<-Vectors],
  52.  lists:filter(fun(R)->if R==false -> false; true -> true end end,Results).
  53.  
  54. find_disks(Board, Disk) ->
  55.   lists:flatten([
  56.    [{ColI,RowI}||{ColI,Value}<-index(Row), Value==Disk]
  57.    ||{RowI,Row}<-index(Board)
  58.   ]).
  59.  
  60. find_legal_moves(Board,Disk) ->
  61.  [find_legal_moves_at(Board,Pos)||Pos<-find_disks(Board, Disk)].
Advertisement
Add Comment
Please, Sign In to add comment