Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.24 KB | None | 0 0
  1. %  main.erl
  2. -module(main).
  3. -compile([export_all]).
  4. %
  5.  
  6. % w = 119
  7. % b = 98
  8. % . = 46
  9.  
  10. deletePawn(Pos, Board) ->
  11.     {List1,End} = lists:split(Pos, Board),
  12.     Start = lists:droplast(List1),
  13.     Start++[46]++End.
  14.    
  15. movePawn(Src, Dest, Board) ->
  16.     Pawn = lists:nth(Src, Board),
  17.     TmpBoard = deletePawn(Src, Board),
  18.     {List1,End} = lists:split(Dest, TmpBoard),
  19.     Start = lists:droplast(List1),
  20.     Start++[Pawn]++End.
  21.  
  22. righMoveWhite(Src, Dest, Board) ->
  23.     Left = lists:nth(Src+7, Board),
  24.     Right = lists:nth(Src+9, Board),
  25.     LeftHit = lists:nth(Src+14, Board),
  26.     RightHit = lists:nth(Src+18, Board),
  27.    
  28.     if  Src > Dest -> false;
  29.         Src+7 == Dest;  Left == 46; (Dest div 8 - Src div 8) == 1 -> true;
  30.         Src+9 == Dest;  Right == 46; (Dest div 8 - Src div 8) == 1 -> true;
  31.         Src+14 == Dest; Left /= 46; LeftHit == 46; (Dest div 8 - Src div 8) == 2 -> true;
  32.         Src+18 == Dest; Right /= 46; RightHit == 46; (Dest div 8 - Src div 8) == 2 -> true;
  33.         true -> false
  34.     end.
  35.  
  36. righMoveBlack(Src, Dest, Board) ->
  37.     Left = lists:nth(Src-9, Board),
  38.     Right = lists:nth(Src-7, Board),
  39.     LeftHit = lists:nth(Src-18, Board),
  40.     RightHit = lists:nth(Src-14, Board),
  41.    
  42.     if  Src > Dest -> false;
  43.         Src-9 == Dest;  Left == 46; (Src div 8 - Dest div 8) == 1 -> true;
  44.         Src-7 == Dest;  Right == 46; (Src div 8 - Dest div 8) == 1 -> true;
  45.         Src-18 == Dest; Left /= 46; LeftHit == 46; (Src div 8 - Dest div 8) == 2 -> true;
  46.         Src-14 == Dest; Right /= 46; RightHit == 46; (Src div 8 - Dest div 8) == 2 -> true;
  47.         true -> false
  48.     end.
  49.  
  50. moveWhite(Src, Dest, Board) ->
  51.     RightMove = righMoveWhite(Src, Dest, Board),
  52.     Diff = Dest div 8 - Src div 8,
  53.     if  RightMove == true, Diff == 2 -> deletePawn(Dest, Board);
  54.         RightMove == true -> movePawn(Src, Dest, Board)
  55.     end.
  56.  
  57. moveBlack(Src, Dest, Board) ->
  58.     RightMove = righMoveBlack(Src, Dest, Board),
  59.     Diff = Src div 8 - Dest div 8,
  60.     if  RightMove == true, Diff == 2 -> deletePawn(Dest, Board);
  61.         RightMove == true -> movePawn(Src, Dest, Board)
  62.     end.
  63.  
  64. endGame(Board) ->
  65.     White = lists:member(119,Board),
  66.     Black = lists:member(98,Board),
  67.     if  White==true -> false;
  68.         Black==true -> false;
  69.         true -> true
  70.     end.
  71.  
  72. game() ->
  73.     BoardString = ".w.w.w.ww.w.w.w..w.w.w.w................b.b.b.b..b.b.b.bb.b.b.b.\n",
  74.     io:format(BoardString).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement