~subsetpark/whist

ref: b4197421dcbc4b6877e7b574a1fc6ee2802d1171 whist/test/whist.janet -rw-r--r-- 6.6 KiB
b4197421 — Zach Smith Replace add_score with info boxes 1 year, 1 month ago
                                                                                
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
(import testament :prefix "")

(import whist)
(import bids)
(import init)
(import cards)

(cards/intern-cards!)

(def- players ["North" "East" "South" "West"])

(def- player-state [{:team "north_south" :id "North"}
		    {:team "east_west" :id "East"}
		    {:team "north_south" :id "South"}
		    {:team "east_west" :id "West"}])

(defn all-not-passed [] @{:North true :East true :South true :West true}) 

(deftest init
  (is (= {:players player-state
	  :state {:phase "deal"}}
	 (init/init ;players))))

(deftest deal
  (def [_state events] (whist/next {:players player-state :state {:phase "deal"}}))
  (def [north-draw _east-draw _south-draw _west_draw north-decoration north-prompt] events)
  (is (= {:event "draw" :count 12 :player "North"} north-draw))
  (is (= {:value "bidding" :event "add_decoration" :player "North" :name "bid_action"} north-decoration))
  (is (= {:name "bid" :count 1 :event "prompt_select" :player "North" :from (bids/available-bids)} north-prompt)))

(deftest bidding
  (def first-bid {:players player-state
		  :state {:phase "bid"
			  :meta {:high_bid {}
				 :not_passed (all-not-passed)}}
		  :action {:player "North" :name "bid" :value {:count 3 :direction "up"}}})
  (def [state events] (whist/next first-bid))
  (def [north-action_decoration north-bid_decoration east-action_decoration east-prompt] events)
  (is (= {:value "declarer" :event "add_decoration" :player "North" :name "bid_action"} north-action_decoration))
  (is (= {:value "3 Uptown" :event "add_decoration" :player "North" :name "bid"} north-bid_decoration))
  (is (= {:value "bidding" :event "add_decoration" :player "East" :name "bid_action"} east-action_decoration))
  (is (deep= {:name "bid" :count 1 :event "prompt_select" :player "East" :from (bids/available-bids {:count 3 :direction "up"})} east-prompt))
  (is (deep= @{:bid {:count 3 :direction "up"} :player "North"} (get-in state [:meta :high_bid])))
  (is (deep= @{:North true :East true :South true :West true} (get-in state [:meta :not_passed]))))

(deftest overbid
  (def player-overbid {:players player-state
		       :state {:phase "bid"
			       :meta {:high_bid {:bid {:count 3 :direction "up"} :player "North"}
				      :not_passed (all-not-passed)}}
		       :action {:player "East" :value {:count 3 :suit "no_trumps"}}})
  (def [state events] (whist/next player-overbid))
  (def [east-action_decoration east-bid_decoration north-remove_decoration south-decoration south-prompt] events)
  (is (= {:value "declarer" :event "add_decoration" :player "East" :name "bid_action"} east-action_decoration))
  (is (= {:value "3 No-Trumps" :event "add_decoration" :player "East" :name "bid"} east-bid_decoration))
  (is (= {:event "remove_decoration" :player "North" :name "bid_action"}  north-remove_decoration))
  (is (= {:value "bidding" :event "add_decoration" :player "South" :name "bid_action"} south-decoration))
  (is (deep= {:name "bid" :count 1 :event "prompt_select" :player "South" :from (bids/available-bids {:count 3 :suit "no_trumps"})} south-prompt))
  (is (deep= @{:bid {:count 3 :suit "no_trumps"} :player "East"} (get-in state [:meta :high_bid])))
  (is (deep= @{:North true :East true :South true :West true} (get-in state [:meta :not_passed]))))

(deftest pass
  (def player-passed {:players player-state
		      :state {:phase "bid"
			      :meta {:high_bid {}
				     :not_passed (all-not-passed)}}
		      :action {:name "bid" :player "North" :value "pass"}})
  (def [state events] (whist/next player-passed))
  (def [north-decoration east-decoration east-prompt] events)
  (is (= {:value "passed" :event "add_decoration" :player "North" :name "bid_action"} north-decoration))
  (is (= {:value "bidding" :event "add_decoration" :player "East" :name "bid_action"} east-decoration))
  (is (= {:name "bid" :count 1 :event "prompt_select" :player "East" :from (bids/available-bids)} east-prompt))
  (is (deep= @{} (get-in state [:meta :high_bid])))
  (is (deep= @{:East true :South true :West true} (get-in state [:meta :not_passed]))))


(deftest last-pass
  (def last-player-passed {:players player-state
			   :state {:phase "bid"
				   :meta {:high_bid {:bid {:count 3 :direction "up"} :player "East"}
					  :not_passed @{:East true :North true}}}
			   :action {:name "bid" :player "North" :value "pass"}})
  (def [state events] (whist/next last-player-passed))
  (def [north-decoration east-prompt] events)
  # events for East is nil, because there are no side effects for that player this round. 
  (is (= {:value "passed" :event "add_decoration" :player "North" :name "bid_action"} north-decoration))

  (is (= {:name "bid" :count 1 :event "prompt_select" :player "East" :from bids/suit} east-prompt))
  (is (deep= @{:bid {:count 3 :direction "up"} :player "East"} (get-in state [:meta :high_bid])))
  # not_passed is gone because we are moved to the next phase.
  (is (= nil (get-in state [:meta :not_passed])))
  (is (= "discard" (in state :phase))))

(deftest discard-prompt
  (def player-completed-bid {:players player-state
			     :state {:phase "discard"
				     :meta {:high_bid {:bid {:count 3 :direction "up"} :player "East"}}}
			     :action {:player "North" :value {:suit "clubs"}}})
  (def [state events] (whist/next player-completed-bid))
  (def [north-decoration north-draw north-prompt] events)
  (is (= {:value "3 Uptown: Clubs" :event "add_decoration" :player "high_bid" :name "North"} north-decoration))
  (is (= {:event "draw" :count 6 :player "North"} north-draw))
  (is (= {:event "prompt_discard" :count 6 :player "North"} north-prompt))
  (is (deep= {:phase "begin_play" :meta {:bid @{:count 3 :direction "up" :suit "clubs"}}} state)))

(deftest begin-play
  (let [two-cards [CA C2]
	with-two [{:hand two-cards :score 0 :team 1 :meta {} :id "North"}
		  {:hand [] :score 0 :team 2 :meta {} :id "East"}
		  {:hand [] :score 0 :team 1 :meta {} :id "South"}
		  {:hand [] :score 0 :team 2 :meta {} :id "West"}]
	player-discarded {:players with-two
			  :state {:phase "begin_play"
				  :meta {:suit :null}}
			  :action {:player "North" :name "discard" :value [C2]}}]
    (def [state events] (whist/next player-discarded))
    (def [east-info south-info west-info north-info north-discard north-prompt] events)
    (is (= {:value 1 :name :North_tricks :event "add_info"} north-info))
    (is (= {:value 0 :name :East_tricks :event "add_info"} east-info))
    (is (= {:value 0 :name :South_tricks :event "add_info"} south-info))
    (is (= {:value 0 :name :West_tricks :event "add_info"} west-info))
    (is (deep= {:event "discard" :value [@{:rank 2 :suit "clubs"}] :player "North"} north-discard))
    (is (= {:count 1 :to "trick" :event "prompt_play" :player "North"} north-prompt))
    (is (deep= {:phase "play" :meta @{:suit :null}} state))))

(run-tests!)