model.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. (define-module (model)
  2. #:export (<risk-rules>
  3. make-risk-rules
  4. get-att-dice set-att-dice
  5. get-def-dice set-def-dice
  6. get-eq-eyes-win set-eq-eyes-win
  7. <fight-situation>
  8. make-fight-situation
  9. get-att-wins
  10. get-att-rems
  11. get-def-wins
  12. get-def-rems))
  13. (use-modules
  14. ;; SRFI 1 for additional list procedures
  15. (srfi srfi-1)
  16. ;; for functional structs (not part of srfi-9 directly)
  17. (srfi srfi-9 gnu)
  18. (rnrs enums))
  19. (define-public *equal-eyes-winner*
  20. (make-enumeration
  21. '(att
  22. def
  23. both
  24. none)))
  25. (define-immutable-record-type <risk-rules>
  26. ;; define constructor
  27. (make-risk-rules att-dice def-dice eq-eyes-win)
  28. ;; define predicate
  29. risk-rules?
  30. ;; define accessors and functional setters
  31. (att-dice get-att-dice set-att-dice)
  32. (def-dice get-def-dice set-def-dice)
  33. (eq-eyes-win get-eq-eyes-win set-eq-eyes-win))
  34. (define-immutable-record-type <fight-situation>
  35. ;; define constructor
  36. (make-fight-situation att-wins att-rems def-wins def-rems)
  37. ;; define predicate
  38. fight-situation?
  39. ;; define accessors and functional setters
  40. (att-wins get-att-wins)
  41. (att-rems get-att-rems)
  42. (def-wins get-def-wins)
  43. (def-rems get-def-rems))
  44. (define-public make-init-fight-situation
  45. (λ (att-count def-count)
  46. "Create an initial fight situation, where no one yet has won any dice
  47. comparisons."
  48. (make-fight-situation 0 att-count 0 def-count)))
  49. (define-public fight-situation-less-than
  50. (λ (fs1 fs2)
  51. (cond
  52. [(< (get-att-wins fs1) (get-att-wins fs2)) #t]
  53. [(> (get-att-wins fs1) (get-att-wins fs2)) #f]
  54. [else ; =
  55. (cond
  56. [(< (get-att-rems fs1) (get-att-rems fs2)) #t]
  57. [(> (get-att-rems fs1) (get-att-rems fs2)) #f]
  58. [else ; =
  59. (cond
  60. [(< (get-def-wins fs1) (get-def-wins fs2)) #t]
  61. [(> (get-def-wins fs1) (get-def-wins fs2)) #f]
  62. [else ; =
  63. (cond
  64. [(< (get-def-rems fs1) (get-def-rems fs2)) #t]
  65. [(> (get-def-rems fs1) (get-def-rems fs2)) #f]
  66. [else ; =
  67. #f])])])])))
  68. (define-public list->fight-situation
  69. (λ (lst)
  70. (make-fight-situation (first lst)
  71. (second lst)
  72. (third lst)
  73. (fourth lst))))
  74. (define-public fight-sit-list-att-wins first)
  75. (define-public fight-sit-list-att-rems second)
  76. (define-public fight-sit-list-def-wins third)
  77. (define-public fight-sit-list-def-rems fourth)
  78. (define-public att-lost?
  79. (lambda* (fight-result #:key (rules *default-risk-rules*))
  80. ;; NOTE: FUTUE: Currently the rules do not contain anything, which would
  81. ;; make it necessary to look at them, when determining, whether the attacker
  82. ;; lost.
  83. (= (get-att-rems fight-result) 1)))
  84. (define-public def-lost?
  85. (lambda* (fight-result #:key (rules *default-risk-rules*))
  86. ;; NOTE: FUTUE: Currently the rules do not contain anything, which would
  87. ;; make it necessary to look at them, when determining, whether the defender
  88. ;; lost.
  89. (= (get-def-rems fight-result) 0)))
  90. (define-public fight-res-list-att-lost?
  91. (λ (fight-res-list)
  92. (= (fight-sit-list-att-rems fight-res-list) 1)))
  93. (define-public fight-res-list-att-win?
  94. (λ (fight-res-list)
  95. (> (fight-sit-list-att-rems fight-res-list) 1)))
  96. (define-public fight-res-list-def-lost?
  97. (λ (fight-res-list)
  98. (= (fight-sit-list-def-rems fight-res-list) 0)))
  99. (define-public fight-res-list-def-win?
  100. (λ (fight-res-list)
  101. (> (fight-sit-list-def-rems fight-res-list) 0)))
  102. (define-public fight-situation->hash-key
  103. (λ (fight-situation)
  104. (list (get-att-wins fight-situation)
  105. (get-att-rems fight-situation)
  106. (get-def-wins fight-situation)
  107. (get-def-rems fight-situation))))
  108. (define-public *default-risk-rules*
  109. (make-risk-rules 3 2 'def))
  110. (define-public attacker-lost?
  111. (lambda* (attackers-remaining #:key (rules *default-risk-rules*))
  112. "Tell whether the attacker lost a fight according to the rules."
  113. ;; Currently the rules model does not contain anything used to tell, whether
  114. ;; the attacker lost. It could contain something in the future, for example
  115. ;; the requirement, that the attacker must retain at least 2 units in the
  116. ;; source area.
  117. (< attackers-remaining 2)))
  118. (define-public defender-lost?
  119. (lambda* (defenders-remaining #:key (rules *default-risk-rules*))
  120. "Tell whether the defenders lost a fight according to the rules."
  121. ;; Currently the rules model does not contain anything used to tell, whether
  122. ;; the defender lost. It could contain something in the future, for example
  123. ;; the requirement, that the defender loses when only 1 unit is left.
  124. (= defenders-remaining 0)))