Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. let board = [|
  2. ('a', 1); ('b', 2); ('c', 3); ('d', 4);
  3. ('e', 5); ('f', 6); ('g', 7); ('h', 8)
  4. |];;
  5.  
  6. (*** 1 ***)
  7. let int_of_col = fun x ->
  8. match x with
  9. | 'a' -> 1
  10. | 'b' -> 2
  11. | 'c' -> 3
  12. | 'd' -> 4
  13. | 'e' -> 5
  14. | 'f' -> 6
  15. | 'g' -> 7
  16. | 'h' -> 8;;
  17.  
  18. (*** 2 ***)
  19. let attack = fun (k,b) (l,d) ->
  20. if int_of_col k == int_of_col l && b == d then
  21. false
  22. else if abs(int_of_col k - int_of_col l) * abs(b - d) == 0
  23. || abs(int_of_col k - int_of_col l) == abs(b - d)
  24. then
  25. true
  26. else
  27. false;;
  28.  
  29.  
  30. (*** 3 ***)
  31. let attacked = fun array ->
  32. let war = [|false; false; false; false; false; false; false; false|] in
  33. for i = 0 to (Array.length array)-1 do
  34. for j = 0 to (Array.length array)-1 do
  35. if attack (array.(i)) (array.(j)) then
  36. (war.(i) <- true;
  37. war.(j) <- true;);
  38. done
  39. done;
  40. war;;
  41.  
  42.  
  43. (*** 4 ***)
  44. let checkIfTrue = fun state ->
  45. if state then
  46. false
  47. else
  48. true;;
  49.  
  50. let safe = fun s ->
  51. Array.for_all checkIfTrue (attacked s);;
  52.  
  53. safe [|('a', 7); ('b', 2); ('d', 3);
  54. ('e', 6); ('f', 8); ('g', 5); ('h', 1)|];;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement