-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdealer.rkt
1710 lines (1500 loc) · 73.8 KB
/
dealer.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket
;; ===================================================================================================
;; represent a dealer and its internal representation of a state
(require (only-in "next.rkt" dealer-next/c external-player/c)
(only-in "cards.rkt" card?)
(only-in "basics.rkt" natural? between))
(define dealer%/c
(class/c
[field players watering-hole cards]
[inner (complete-turn (->m any))]))
;; add the interface of the dealer for external uses (xmain)
(define dealer/c (and/c (object/c [run-game (->m any/c)]) dealer-next/c))
(define MIN-PLAYERS 3)
(define MAX-PLAYERS 8)
(define (create-dealer/c dealer/c)
(->* [(and/c [listof (list/c string? external-player/c)] (between MIN-PLAYERS MAX-PLAYERS))]
[(listof card?)]
dealer/c))
(provide
;; constants
MIN-PLAYERS
MAX-PLAYERS
create-dealer/c ;; DealerContract -> CreatorContract
dealer/c ;; DealerContract
(contract-out
[dealer% dealer%/c]
[create-dealer
;; expects a list of named (string) players that satisfy the external player interface
(create-dealer/c dealer/c)]))
;; ===================================================================================================
;; DEPENDENCIES
(require (only-in "player-internal.rkt" create-player CARDS-PER-BOARD CARD-PER-PLAYER)
(except-in "cards.rkt" card?)
(except-in "basics.rkt" natural? between)
(except-in "next.rkt" dealer-next/c external-player/c)
"board.rkt" "traits.rkt")
;; for debugging
(require "common.rkt")
(module+ test
(require rackunit
(only-in "player-external.rkt" create-external create-bad-choose no-fc over-growth)
(only-in (submod "player-internal.rkt" test) player)
(submod ".."))
(require (for-syntax syntax/parse))
(require (submod "common.rkt" test)))
;; ===================================================================================================
;; IMPLEMENTATION
(define (create-dealer players (cards all-cards))
(new dealer% [externals players][cards cards]))
(module+ test
;; create a dealer set for intermediate states
(define/contract (dealer #:cards [cards '()] #:players players #:watering [watering-hole 0])
(->i (#:players [players (and/c [listof any/c #;==player?] (between MIN-PLAYERS MAX-PLAYERS))])
(#:cards [cards [listof valid-card?]]
#:watering [watering-hole natural-number/c])
#:pre (cards players) (dealer-and-player-cards-are-disjoint-sets cards players)
[r any/c #;==dealer%])
(define s (new dealer% [cards cards]))
(set-fields! s players watering-hole))
; [Listof Card] [Listof InternalPlayer] -> Boolean
;; ensure that the players' and the dealer's cards are disjoint and that they form a set
(define (dealer-and-player-cards-are-disjoint-sets cards players)
[define player-cards (apply append (map (lambda (p) (get-field cards p)) players))]
[define dealer-cards (if (unsupplied-arg? cards) '() cards)]
[define cards-as-set (to-set (append dealer-cards player-cards))]
(and cards-as-set (subset-of-all-cards? cards-as-set))))
;; ---------------------------------------------------------------------------------------------------
(define dealer%
(class object%
(init-field
;; [Listof Card]
cards
[externals
;; [Listof [List String Player]]
'()])
(init-field
[internal-player create-player])
(super-new)
(field
[players
;; [Listof Player]]
;; the players in the order in which they will take the next turn
(for/list ((e externals) (name (in-naturals)))
(define id (format "~a#~a" (first e) (+ name 1)))
(internal-player id (second e)))]
[watering-hole
;; N : the number of food tokens left in the watering hole
0])
;; -----------------------------------------------------------------------------------------------
;; equality
(define/public (equal-to? other r)
(and (r (get-field cards other) cards)
(= (get-field watering-hole other) watering-hole)
(r (get-field players other) players)))
;; this is basically nonsense
(define/public (equal-hash-code-of hash-code)
(hash-code players))
;; this is basically nonsense
(define/public (equal-secondary-hash-code-of hash-code)
(hash-code players))
;; -----------------------------------------------------------------------------------------------
;; running a game
;; Results = [Listof [List N Name N]] ~~ rank, player's name, and score
;; -> Results
;; run turns until game is over, then determine results and pretty-print them
(define/public (run-game)
(let run-turns ()
(unless (over?)
(complete-turn)
(unless (empty? players)
(set! players (cyclic-rotate players))
(run-turns))))
(results))
;; -> Void
;; run a turn, allow derived classes to hook in a function at the top of the turn
(define/pubment (complete-turn)
(inner (void) complete-turn)
(boards-for-all)
(turn))
;; -> Void
;; EFFECT add one board to all players that don't have one
(define/public (boards-for-all)
(for ((p players))
(send p add-board-if-needed)))
;; -> Boolean
;; are there too few cards left to start a turn?
(define/private (over?)
(or (empty? players)
(< (length cards) (for/sum ((p players)) (send p how-many-cards-needed)))))
;; -> Results
;; determine the scores of the players and sort them in descending order (with id)
(define/public (results)
(define raw-scores
(for/list ((p players))
(list (get-field id p) (send p score))))
(for/list ((r (sort raw-scores >= #:key second)) (i (in-naturals)))
(cons (+ i 1) r)))
;; -> Void
;; EFFECT run a complete turn
;; ASSUME there are enough cards to launch a turn
(define/public (turn)
(unless (empty? players)
(step1)
(unless (empty? players)
(define action (step2-3))
(unless (empty? players)
(step4 action)
(reduce-population-to-food)
(move-food-to-bags)))))
;; -> Void
;; EFFECT distribute cards to players
;; ASSUME called only if (not (over?)) holds and (boards-for-all) has been run
(define/public (step1)
(for ((p players))
(give-cards-to p (send p how-many-cards-needed)))
(all-players-communicate-externally (lambda (p) (send p start watering-hole)) "start [~e]"))
;; -> Void
;; EFFECT move player's food on species boards to bags
(define/public (move-food-to-bags)
(for ((p players))
(send p move-food-to-bag)))
;; -> Void
;; EFFECT reduce population to food for all of all player's species
(define/private (reduce-population-to-food)
(for ((p players))
(send p reduce-population-to-food (lambda () (give-cards-to p EXTINCTION-CARDS)))))
;; -> [Listof Action4]
;; ask players to choose their actions
;; ASSUME (board-for-all) and (give-cards ...) replenished all players boards and cards
(define/private (step2-3)
(all-players-communicate-externally (lambda (p) (send p choose players)) "choose ~e"))
;; [Listof Action4] -> Void
;; execute step 4 of the Evolution turn
;; ASSUME: ;; (= (length players) (length card-actions))
;; the actions are specified in the same order as players
(define/public (step4 actions*)
(for ((p players) (per-player actions*))
(define added-food (send p apply-card-action per-player))
(fill-up-watering-hole added-food))
(feeding))
;; Card -> Void
(define/private (fill-up-watering-hole fc)
(set! watering-hole (max 0 (+ watering-hole (card-food-points fc)))))
;; -> Void
(define/public (feeding)
(act-on-all-from (first players) fertile? (lambda (p s) (send p population+1 s)))
(act-on-all-from (first players) long-neck? (lambda (p s) (feed-a-player-s-species p s)))
(act-on-all-from (first players) fat-tissue? (lambda (p s) (send p move-fat s)))
;; players* represents a cicular queue of the players that may still need food
(let feeding ([players* players])
(define some-player-can-eat
(ormap (lambda (p) (cons? (send p feedable (players-in-order p #:last #true)))) players*))
(when (and (> watering-hole 0) some-player-can-eat)
(feeding (feed1 players*)))))
;; {[Listof Player]} -> [Listof Player]
;; players* represents a cicular queue of the players that may still need food
;; ASSUME watering-hole is not empty, there is a feedable player
(define/public (feed1 (players* players))
(define current-player (first players*))
(define in-attackable-order (players-in-order current-player #:last #true))
(define choice (send current-player next watering-hole in-attackable-order))
(communicate-externally choice
(get-field id current-player)
"feed-next ~e"
(lambda (next) (send next interpret this players* in-attackable-order))
(lambda () (rest players*))))
;; -----------------------------------------------------------------------------------------------
;; the below is the API to NEXT for the interpret method
(define/public (give-cards-to player n0)
(define n (min (length cards) n0))
(send player take-cards (take cards n))
(set! cards (drop cards n)))
(define/public (store-fat current-player s n)
(define to-be-transfered (min n watering-hole))
(set! watering-hole (- watering-hole to-be-transfered))
(send current-player store-fat s to-be-transfered))
(define/public (feed-scavengers current-player)
(act-on-all-from current-player scavenger? (lambda (p s) (feed-a-player-s-species p s))))
;; generative recursion: the following three methods implement the rules of feeding a species
(define/public (feed-a-player-s-species player s)
;; TERMINATION
;; neighbor-of-s > s or (not neighbor-of-s)
;; so a recursive call from cooperate will be a larger species index
;; but there's only a finite number of species (with index) per player,
;; hence neighbor-of-s will eventually be #false & there will be no more recursion
(define-values (ok? foraging? neighbor-of-s) (feed-if-possible player s))
(when ok?
(when foraging?
(define-values (ok? _1 _2) (feed-if-possible player s))
(when ok?
(cooperate player neighbor-of-s)))
(cooperate player neighbor-of-s)))
;; Player [Maybe N] -> Void
;; hand s the right to eat if it points to a species
(define/private (cooperate player s)
(when s
(feed-a-player-s-species player s)))
;; Player N -> (values Boolean Boolean [Maybe N])
;; feed species s of player from this dealer's waterhing hole
;; if there is food and if s points to a species
(define/private (feed-if-possible player s)
(cond
;; scavenging, foraging, and cooperation must not overfeed a species
[(or (= watering-hole 0) (send player all-fed s)) (values #false #false #false)]
[else
(set! watering-hole (- watering-hole 1))
(define-values (foraging? neighbor-of-s) (send player feed1 s))
(values #true foraging? neighbor-of-s)]))
;; -----------------------------------------------------------------------------------------------
;; [Player -> X] FormatString[1] -> [Listof X]
;; apply all players to action and collect results
;; EFFECT validate action's result and throw players out that cheat
(define/private (all-players-communicate-externally action fmt)
(reverse
(for/fold ((result '())) ((p players))
(define a (action p))
(define i (get-field id p))
(communicate-externally a i fmt (lambda (choice) (cons choice result)) (lambda () result)))))
;; [Maybe X] Player FormatString [-> Y] [X -> Y] -> Y
;; run (thn choice) if choice holds, otherwise (els)
;; EFFECT: remove player p if choice isn't true
(define/private (communicate-externally choice id fmt thn els)
(cond
[choice (thn choice)]
[else
(log-info (string-append "dealer says: player ~a failed on " fmt) id choice)
(set! players (remf (lambda (p) (equal? (get-field id p) id)) players))
(els)]))
;; -----------------------------------------------------------------------------------------------
;; Player [Any -> Boolean] [Player N -> Void] -> Void
;; act on all player's species where species satisfies p?
(define/private (act-on-all-from current-player p? action)
(for ((p (players-in-order current-player)))
(send p find-all p? action)))
;; -----------------------------------------------------------------------------------------------
;; Player -> [Listof Player]
;; find current-player id in players, take rest and append front-end
;; the keyword argument #:last places the current player
(define/private (players-in-order current-player #:last (last? #false))
(define id (get-field id current-player))
(let loop ([players players][front-end '()])
(define fst (first players))
(if (equal? (get-field id fst) id)
(if last?
(append (rest players) (reverse front-end))
(append players (reverse front-end)))
(loop (rest players) (cons (first players) front-end)))))))
;; ===================================================================================================
;; the testing DSL
(module+ test
;; (check-scenario ...) describes the state of a dealer before and after (testing dealer) is called
;; it synthezies rackunit tests that check the expected changes as well as the frame conditions
(define-syntax (check-scenario stx)
(syntax-parse stx
[(check-feed1 #:doc msg
;; create players for this test case:
#:before
[p:id (~optional [#:cards card+] #:defaults ([card+ #''()])) sp ... ] ...
(~optional (~seq #:cards dealer-cards) #:defaults ([dealer-cards #''()]))
;; the food in the watering hole before (testing dealer)
#:pre pre:number
;; the food after a feed1 step
#:post post:number
;; those players that changed, all others are tested for differences:
;; _ means the species renains the same
;; - means the species went extinct
#:after
[q:id (~optional [#:cards cards] #:defaults ([cards #''()]))
(~optional [#:bag bag] #:defaults ([bag #'0]))
sq ... ] ...)
;;
;; creates code to:
;; -- define functions named p ... that create players from species sp ...,
;; (thawed if sp is just an id)
;; -- creates instances of these players and runs check-plain on them
;; -- compares those specified in ((q sq ...) ...) to make sure they changed as desired
;; -- makes sure the others remain the same (frame conditions)
;; ------------------------------------------------------------------------------------
(let* ([players (syntax->list #'((p sp ...) ...))]
[names (syntax->list #'(p ...))]
[ids (for/list ([_p names][i (in-naturals)]) (+ i 1))]
[modified (syntax->list #`((q bag cards sq ...) ...))]
[names-for-thunked-players (generate-temporaries names)])
(with-syntax ([(p-id ...) ids]
[(player-of ...) names-for-thunked-players]
[((tp ...) ...)
(for*/list ([s (syntax->list #'((sp ...) ...))])
(for/list ([t (syntax->list s)])
(id->app t)))])
#`(let ([player-of
(lambda ()
(player p-id #:cards card+ #:external create-external #:species (lazy tp ...)))]
...)
(log-info msg)
(let #,(for/list ([p names] [pre names-for-thunked-players])
#`[#,p (#,pre)])
(check-plain msg dealer-cards pre post p ...)
#,@(generate-checks players ids names-for-thunked-players modified #'msg)))))]))
;; make distinct species for external player and internal player
(define-syntax-rule (lazy s ...) (lambda () (list s ...)))
;; String N N Player *-> Void
;; (check-plain msg pre post player ... player) tests that a dealer w/o cards feeds a player
;; and that the feeding reduces the food tokens at the watering hole as expected
(define (check-plain msg cards pre post . players)
(define dealer0 (dealer #:players players #:cards cards #:watering pre))
(with-handlers ([exn:fail:contract? (lambda (x) (debug `("contract:" ,msg)) (raise x))])
(check-equal? (begin [(testing) dealer0] (get-field watering-hole dealer0)) post msg)))
(define set-add-test-in-hook (make-parameter #false))
;; PlayerSpec = #'[Id {Id | (Id Expr ...)}]
;; [Listof PlayerSpec] [Listof N] [Listof #'Id] [Listof PlayerSpec] -> Expr
(define-for-syntax (generate-checks players ids names-of-thunks changed-players msg)
(for/list ([p players] [id ids] [pre names-of-thunks])
(syntax-case p ()
[(p sp ...)
(let* ([ekwal? (lambda (p q) (free-identifier=? p (car (syntax->list q))))]
[the-q (member #'p changed-players ekwal?)])
(if (not the-q)
#`(check-equal? p (#,pre) (format "~a (frame condition ~a)" #,msg #,id))
(syntax-case (car the-q) ()
[(q bag cards sq ...)
(let ([sp-list (syntax->list (find #'q players))]
[sq-list (syntax->list #'(sq ...))])
#;
(unless (= (length sp-list) (length sq-list)) ;; doesn't work with cards ???
(displayln `(,sp-list ,sq-list))
(raise-syntax-error #f "unequal length"))
(define species #`(lazy #,@(expected-species sp-list sq-list)))
(define the-plr
#`(player #,id #:bag bag
#:external create-external
#:cards cards
#:species #,species))
#`(check-equal? q #,the-plr #,msg #;#,chk-msg))])))])))
;; [Listof Expr] [Listof (U - _ Expr)] -> [Listof Expr]
;; construct the expected list of species
(define-for-syntax (expected-species sp-list sq-list)
(let loop ([sp-list sp-list][sq-list sq-list])
(cond
[(null? sq-list) '()]
[(null? sp-list) (map id->app sq-list)]
[else
(syntax-parse (car sq-list) #:literals (_ -)
[_ (cons (id->app (car sp-list)) (loop (cdr sp-list) (cdr sq-list)))]
[- (loop (cdr sp-list) (cdr sq-list))]
[sq (cons (id->app #'sq) (loop (cdr sp-list) (cdr sq-list)))])])))
;; #'Id [Listof PlayerSpec] -> PlayerSpec
(define-for-syntax (find id name+species*)
(let/ec return
(for/and ((name+species name+species*))
(syntax-case name+species ()
[(p sp ...) (if (free-identifier=? #'p id) (return #'(sp ...)) #t)]))
(raise-syntax-error #f (string-append "not found: " (symbol->string (syntax-e id))))))
;; (U Id App) -> App
(define-for-syntax (id->app t)
(if (identifier? t) #`(#,t) t)))
;; ---------------------------------------------------------------------------------------------------
;; the actual tests:
(module+ test
;; -------------------------------------------------------------------------------------------------
;; testing the feed1 step
(testing (lambda (dealer0) (send dealer0 feed1)))
(log-info "testing feed1")
;; -------------------------------------------------------------------------------------------------
;; some species and players
(define (s-vegetarian-pop=1 f) (species #:body 3 #:food f #:population 1 #:traits '()))
(define (s-satisfied (with '()) (f 1)) (species #:body 3 #:food f #:population 1 #:traits with))
(define (s-hungry-carnivore (f 0))
(species #:body 3 #:food f #:population 1 #:traits `(,carnivore)))
(define (s-climbing g) (species #:body 3 #:food g #:population 1 #:traits `(,climbing)))
;; -------------------------------------------------------------------------------------------------
;; this test must fail but doesn't because the player is called even though there's no food
#;
(check-scenario #:doc "no food, no feeding happens"
#:before
[p1 (s-vegetarian-pop=1 0) s-satisfied s-hungry-carnivore]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 0
#:post 0
#:after)
(check-scenario #:doc "cannot attack anything but myself and won't"
#:before [p1 s-hungry-carnivore s-satisfied] [p2 (s-climbing 0)] [p3 ]
#:pre 1
#:post 1
#:after)
;; -------------------------------------------------------------------------------------------------
(define (species-with-fat-trait f)
(species #:fat-food f #:body 2 #:population 1 #:traits `(,fat-tissue)))
(check-scenario #:doc "fat tissue with sufficient food"
#:before
[p1 (species-with-fat-trait 0)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 2
#:post 0
#:after [p1 (species-with-fat-trait 2)])
(check-scenario #:doc "fat tissue with insufficient food"
#:before
[p1 (species-with-fat-trait 0)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 1
#:post 0
#:after [p1 (species-with-fat-trait 1)])
;; -------------------------------------------------------------------------------------------------
(check-scenario #:doc "feed the first hungry vegetarian"
#:before
[p1 (s-vegetarian-pop=1 0) s-satisfied s-hungry-carnivore]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 1
#:post 0
#:after [p1 (s-vegetarian-pop=1 1) _ _])
(check-scenario #:doc "feed the first hungry vegetarian again"
#:before
[p1 s-satisfied (s-vegetarian-pop=1 0) s-hungry-carnivore]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 1
#:post 0
#:after [p1 _ (s-vegetarian-pop=1 1) _])
(check-scenario #:doc "feed the one hungry carnivore and kill of a species"
#:before
[p1 s-satisfied (s-vegetarian-pop=1 1) s-hungry-carnivore]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 1
#:post 0
#:after
[p1 _ _ (s-hungry-carnivore 1)]
[p3 -])
(check-scenario #:doc "no hungry species, remove player & move on"
#:before
[p1 s-satisfied]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied (s-vegetarian-pop=1 1) s-hungry-carnivore]
#:pre 1
#:post 1
#:after)
;; -------------------------------------------------------------------------------------------------
(define (s-food f) (species #:food f #:population 1))
(define (s-cooperating-veg (f 0)) (species #:food f #:population 1 #:traits `(,cooperation)))
(check-scenario #:doc "feed cooperating vegetarian with hungry neighbor"
#:before
[p1 s-cooperating-veg (s-food 0)]
[p2 s-cooperating-veg (s-food 1)]
[p3 s-cooperating-veg (s-food 1)]
#:pre 2
#:post 0
#:after
[p1 (s-cooperating-veg 1) (s-food 1)])
;; -------------------------------------------------------------------------------------------------
(define (s-foraging-veg (f 0) (w '())) (species #:food f #:population 5 #:traits `(,foraging ,@w)))
(check-scenario #:doc "feed vegetarian with foraging trait 1 -> 0"
#:before
[p1 (s-foraging-veg)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 1
#:post 0
#:after
[p1 (s-foraging-veg 1)])
(check-scenario #:doc "feed vegetarian with foraging trait 2 -> 0"
#:before
[p1 (s-foraging-veg)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 2
#:post 0
#:after
[p1 (s-foraging-veg 2)])
(define (s+cooperating (f 0)) (s-foraging-veg f `(,cooperation)))
(define (s-vegegtarian-pop=3 (f 0)) (species #:food f #:population 3 #:traits `(,long-neck)))
(check-scenario #:doc "feed cooperating and foraging vegetarian 5 -> 1"
#:before
[p1 s+cooperating s-vegegtarian-pop=3]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 5
#:post 1
#:after
[p1 (s+cooperating 2) (s-vegegtarian-pop=3 2)])
(check-scenario #:doc "feed cooperating and foraging vegetarian 4 -> 0"
#:before
[p1 s+cooperating s-vegegtarian-pop=3]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 4
#:post 0
#:after
[p1 (s+cooperating 2) (s-vegegtarian-pop=3 2)])
(check-scenario #:doc "feed cooperating and foraging vegetarian 3 -> 0"
#:before
[p1 s+cooperating s-vegegtarian-pop=3]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 3
#:post 0
#:after
[p1 (s+cooperating 2) (s-vegegtarian-pop=3 1)])
;; -------------------------------------------------------------------------------------------------
;; s1: veg eats one, gets another one for foraging
;; s2: neighbor receives 2 for cooperating s1, takes 2 for foraging
;; s3: s2 tries to hand 4 to s3, but it can take only 3 because capacity runs out
(check-scenario #:doc "feed foraging vegetarian with cooperation chain 12 -> 3"
#:before
[p1 (s+cooperating) (s+cooperating) (s-vegegtarian-pop=3)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 12
#:post 3
#:after
[p1 (s+cooperating 2) (s+cooperating 4) (s-vegegtarian-pop=3 3)])
(check-scenario #:doc "feed foraging vegetarian with cooperation chain 10 -> 1"
#:before
[p1 (s+cooperating) (s+cooperating) (s-vegegtarian-pop=3)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 10
#:post 1
#:after
[p1 (s+cooperating 2) (s+cooperating 4) (s-vegegtarian-pop=3 3)])
(check-scenario #:doc "feed foraging vegetarian with cooperation chain 8 -> 0"
#:before
[p1 (s+cooperating) (s+cooperating) (s-vegegtarian-pop=3)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 8
#:post 0
#:after
[p1 (s+cooperating 2) (s+cooperating 4) (s-vegegtarian-pop=3 2)])
;; -------------------------------------------------------------------------------------------------
(define 2cards (take all-cards 2))
(define 2+cards (take (drop all-cards 2) 2))
(define 4cards (append 2cards 2+cards))
;; -------------------------------------------------------------------------------------------------
(check-scenario #:doc "attacker will attack attackee & kill the latter's only species"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-satisfied]
[by-stand (s-vegetarian-pop=1 0) (s-satisfied) (s-hungry-carnivore)]
#:cards 2cards
#:pre 1
#:post 0
#:after
[attacker (s-hungry-carnivore 1) (s-satisfied)]
[attackee (#:cards 2cards) -])
(define (s-horned) (species #:food 0 #:population 1 #:traits `(,horns)))
(check-scenario #:doc "attacker will attack attackee and commit suicide, no feeding"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-horned]
[by-stand ]
#:cards 4cards
#:pre 1
#:post 1
#:after
[attacker (#:cards 2+cards) - (s-satisfied)]
[attackee (#:cards 2cards) -])
(check-scenario #:doc "attacker will attack attackee and commit suicide, no feeding, no cards"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-horned]
[by-stand ]
#:cards 2cards
#:pre 1
#:post 1
#:after
[attacker - (s-satisfied)]
[attackee (#:cards 2cards) -])
(define (s-2satisfied (f 2))
(species #:food f #:population f))
(check-scenario #:doc "attackee's population is reduced below the food size"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-2satisfied]
[by-stand ]
#:cards 4cards
#:pre 1
#:post 0
#:after
[attacker (s-hungry-carnivore 1) s-satisfied]
[attackee (s-2satisfied 1)])
;; -------------------------------------------------------------------------------------------------
(define (s-scavenger-with p (f 0) (t '()))
(species #:body 0 #:food f #:population p #:traits `(,scavenger ,@t)))
(check-scenario #:doc "feed a scavenger"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-satisfied]
[by-stand (s-scavenger-with 1 0) s-hungry-carnivore]
#:cards 4cards
#:pre 3
#:post 1
#:after
[attacker (s-hungry-carnivore 1) (s-satisfied)]
[attackee (#:cards 2cards) -]
[by-stand (s-scavenger-with 1 1) (s-hungry-carnivore)])
(check-scenario #:doc "feed a scavenger 2 -> 0"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-satisfied]
[by-stand (s-scavenger-with 1 0) s-hungry-carnivore]
#:cards 4cards
#:pre 2
#:post 0
#:after
[attacker (s-hungry-carnivore 1) (s-satisfied)]
[attackee (#:cards 2cards) -]
[by-stand (s-scavenger-with 1 1) (s-hungry-carnivore)])
(check-scenario #:doc "feed a scavenger 1 -> 0"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-satisfied]
[by-stand (s-scavenger-with 1 0) s-hungry-carnivore]
#:cards 4cards
#:pre 1
#:post 0
#:after
[attacker (s-hungry-carnivore 1) (s-satisfied)]
[attackee (#:cards 2cards) -]
[by-stand (s-scavenger-with 1 0) (s-hungry-carnivore)])
;; -------------------------------------------------------------------------------------------------
(check-scenario #:doc "foraging scavenger 4 -> 1"
#:before
[attacker s-hungry-carnivore s-satisfied]
[by-standr s-satisfied]
[scavenger (s-scavenger-with 3 0 `(,foraging)) s-hungry-carnivore]
#:cards 4cards
#:pre 4
#:post 1
#:after
[attacker (s-hungry-carnivore 1) (s-satisfied)]
[scavenger (s-scavenger-with 2 2 `(,foraging)) s-hungry-carnivore])
(check-scenario #:doc "foraging scavenger 3 -> 0"
#:before
[attacker s-hungry-carnivore s-satisfied]
[by-standr s-satisfied]
[scavenger (s-scavenger-with 3 0 `(,foraging)) s-hungry-carnivore]
#:cards 4cards
#:pre 3
#:post 0
#:after
[attacker (s-hungry-carnivore 1) (s-satisfied)]
[scavenger (s-scavenger-with 2 2 `(,foraging)) s-hungry-carnivore])
(check-scenario #:doc "foraging scavenger 3 -> 0"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee s-satisfied]
[scavenger (s-scavenger-with 1 0 `(,foraging)) s-hungry-carnivore]
#:cards 4cards
#:pre 2
#:post 0
#:after
[attacker (s-hungry-carnivore 1) (s-satisfied)]
[attackee (#:cards 2cards) -]
[scavenger (s-scavenger-with 1 1 `(,foraging)) s-hungry-carnivore])
;; -------------------------------------------------------------------------------------------------
(check-scenario #:doc "foraging and cooperating scavenger 6 -> 2"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee (s-scavenger-with 4 0 `(,foraging ,cooperation)) s-hungry-carnivore]
[by-stand (s-vegetarian-pop=1 0) (s-satisfied) (s-hungry-carnivore)]
#:cards 2cards
#:pre 6
#:post 2
#:after
[attacker (s-hungry-carnivore 1) s-satisfied]
[attackee (s-scavenger-with 3 2 `(,foraging ,cooperation)) (s-hungry-carnivore 1)])
(check-scenario #:doc "foraging and cooperating scavenger 5 -> 1"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee (s-scavenger-with 4 0 `(,foraging ,cooperation)) s-hungry-carnivore]
[by-stand (s-vegetarian-pop=1 0) (s-satisfied) (s-hungry-carnivore)]
#:cards 2cards
#:pre 5
#:post 1
#:after
[attacker (s-hungry-carnivore 1) s-satisfied]
[attackee (s-scavenger-with 3 2 `(,foraging ,cooperation)) (s-hungry-carnivore 1)])
(check-scenario #:doc "foraging and cooperating scavenger 3 -> 0"
#:before
[attacker s-hungry-carnivore s-satisfied]
[attackee (s-scavenger-with 4 0 `(,foraging ,cooperation)) s-hungry-carnivore]
[by-stand (s-vegetarian-pop=1 0) (s-satisfied) (s-hungry-carnivore)]
#:cards 2cards
#:pre 3
#:post 0
#:after
[attacker (s-hungry-carnivore 1) s-satisfied]
[attackee (s-scavenger-with 3 2 `(,foraging ,cooperation)) s-hungry-carnivore])
;; -------------------------------------------------------------------------------------------------
(check-scenario #:doc "foraging and scavenging carnivore 6 -> 1"
#:before
[attacker (s-scavenger-with 4 0 `(,foraging ,carnivore)) s-satisfied]
[attackee s-satisfied]
[by-stand (s-scavenger-with 1 0) (s-hungry-carnivore)]
#:cards 2cards
#:pre 6
#:post 1
#:after
[attacker (s-scavenger-with 4 4 `(,foraging ,carnivore)) s-satisfied]
[attackee (#:cards 2cards) -]
[by-stand (s-scavenger-with 1 1) (s-hungry-carnivore)])
(check-scenario #:doc "foraging and scavenging carnivore 4 -> 0"
#:before
[attacker (s-scavenger-with 4 0 `(,foraging ,carnivore)) s-satisfied]
[attackee s-satisfied]
[by-stand (s-scavenger-with 1 0) (s-hungry-carnivore)]
#:cards 2cards
#:pre 4
#:post 0
#:after
[attacker (s-scavenger-with 4 4 `(,foraging ,carnivore)) s-satisfied]
[attackee (#:cards 2cards) -])
(check-scenario #:doc "foraging and scavenging carnivore 6 -> 2"
#:before
[attacker (s-scavenger-with 4 0 `(,foraging ,carnivore)) s-satisfied]
[attackee s-satisfied]
[by-stand s-satisfied]
#:cards 2cards
#:pre 6
#:post 2
#:after
[attacker (s-scavenger-with 4 4 `(,foraging ,carnivore)) s-satisfied]
[attackee (#:cards 2cards) -]
[by-stand s-satisfied])
(check-scenario #:doc "foraging, scavenging carnivore 3 0, not evough food to forage"
#:before
[attacker (s-scavenger-with 1 0 `(,foraging ,carnivore)) s-hungry-carnivore]
[attackee s-satisfied]
[by-stand (s-scavenger-with 1 0) (s-hungry-carnivore)]
#:cards 2cards
#:pre 3
#:post 0
#:after
[attacker (s-scavenger-with 1 1 `(,foraging ,carnivore)) (s-hungry-carnivore 1)]
[attackee (#:cards 2cards) -]
[by-stand (s-scavenger-with 1 1) (s-hungry-carnivore)])
;; -------------------------------------------------------------------------------------------------
(define (s-with (f 0) (p 1) (with '()))
(species #:food f #:population p #:traits with))
(check-scenario #:doc "foraging fails, cooperation should fail"
#:before
[p1 (s-with 1 2 `(,cooperation ,foraging)) (s-with 0 2)]
[p2 s-satisfied]
[p3 s-satisfied]
#:pre 3
#:post 1
#:after
[p1 (s-with 2 2 `(,cooperation ,foraging)) (s-with 1 2)])
;; -------------------------------------------------------------------------------------------------
(define (s-plain f) (species #:body 3 #:food f #:population 3))
(define (s-carnivore-with p with (f 0))
(species #:body 3 #:food f #:population p #:traits `(,carnivore ,@with)))
(check-scenario #:doc "allison's test"
#:before
[attacker (s-carnivore-with 5 `(,cooperation))
(s-carnivore-with 3 `(,cooperation))
(s-carnivore-with 3 `(,cooperation))
(s-carnivore-with 2 '())
(s-carnivore-with 2 '())]
[attackee (s-carnivore-with 4 `(,scavenger ,cooperation))
(s-plain 0)
(s-plain 0)]
[by-stander (s-plain 2) (s-plain 2)]
#:pre 15
#:post 9
#:after
[attacker
;; eats because it attacks, hands right to eat to neighbor on right (below)
(s-carnivore-with 5 `(,cooperation) 1)
;; eats because it received the rights, hands right to eat to neighbor
(s-carnivore-with 3 `(,cooperation) 1)
;; eats because it received the rights, hands right to eat to neighbor
(s-carnivore-with 3 `(,cooperation) 1)
;; eats because it received the rights
(s-carnivore-with 2 '() 1)
_] ;; last species remains same
[attackee
;; eats because it scavenges, hands right to eat to neighbor on right (below)
(s-carnivore-with 3 `(,scavenger ,cooperation) 1)
;; eats because it scavenges
(s-plain 1)
(s-plain 0)]))
(module+ test
;; -------------------------------------------------------------------------------------------------
;; testing the entire feeding step
(testing (lambda (dealer0) (send dealer0 feeding)))
(log-info "testing feeding")
;; QUESTION: is there a scenario where a player must skip eating during the feeding cycle
;; but eats later because something has changed that enables one of its species to attack now?
;; -------------------------------------------------------------------------------------------------
(check-scenario #:doc "FF no food, no feeding happens"
#:before
[p1 (s-vegetarian-pop=1 0) s-satisfied s-hungry-carnivore]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 0
#:post 0
#:after)
(check-scenario #:doc "FF cannot attack anything but myself and won't"
#:before [p1 s-hungry-carnivore s-satisfied] [p2 (s-climbing 0)] [p3 ]
#:pre 1
#:post 0
#:after [p2 (s-climbing 1)])
(define (s-fertile (p 1)) (species #:population p #:traits `(,fertile)))
(check-scenario #:doc "cannot attack because fertiles ate first"
#:before
[p1 s-hungry-carnivore s-satisfied]
[p2 (s-climbing 0) s-fertile]
[p3 s-fertile]
#:pre 0
#:post 0
#:after
[p2 (s-climbing 0) (s-fertile 2)]
[p3 (s-fertile 2)])
;; -------------------------------------------------------------------------------------------------
(check-scenario #:doc "FF fat tissue with sufficient food"
#:before
[p1 (species-with-fat-trait 0)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 2
#:post 0
#:after [p1 (species-with-fat-trait 2)])
(check-scenario #:doc "FF fat tissue with insufficient food"
#:before
[p1 (species-with-fat-trait 0)]
[p2 (s-vegetarian-pop=1 0) (s-climbing 0)]
[p3 s-satisfied]
#:pre 1
#:post 0