~subsetpark/whist

e84fef5e4286f4c222e694f730b3b120dd4b220c — Zach Smith 8 months ago a19460f side-effects-as-events
Use add-decoration consistently
3 files changed, 38 insertions(+), 38 deletions(-)

M game/bid.janet
M game/deal.janet
M test/whist.janet
M game/bid.janet => game/bid.janet +4 -4
@@ 27,11 27,11 @@
	[events not-passed] (case last-bid
			      "pass"
			      # Handle passes. Mark the player as passed and remove them from the set.
			      [@[{:event "add_decoration" "name" "bid_action" "player" last-bidder "value" "passed"}]
			      [@[(events/add-decoration last-bidder "bid_action" "passed")]
			       (track-pass (meta "not_passed") last-bidder)]
			      # Handle a bid. Mark the most recent bidder.
			      [@[{:event "add_decoration" "name" "bid_action" "player" last-bidder "value" "declarer"}
				 {:event "add_decoration" "name" "bid" "player" last-bidder "value" (bids/to-text high-bid)}]
			      [@[(events/add-decoration last-bidder "bid_action" "declarer")
				 (events/add-decoration last-bidder "bid" (bids/to-text high-bid))]
			       (meta "not_passed")])
	meta @{"high_bid" {"player" high-bidder "bid" high-bid}}]
    # Remove "declarer" annotation from previous bidder.


@@ 51,5 51,5 @@
	:phase "bid"}
       (array/concat
	events
	{:event "add_decoration" "name" "bid_action" "player" next-bidder "value" "bidding"}
	(events/add-decoration next-bidder "bid_action" "bidding")
	(events/pick1 "bid" next-bidder (bids/available-bids high-bid)))])))

M game/deal.janet => game/deal.janet +1 -1
@@ 18,5 18,5 @@
      :meta (new-meta players)}
     (array/concat
      (all-draw players)
      {:event "add_decoration" "name" "bid_action" "player" bidder "value" "bidding"}
      (events/add-decoration bidder "bid_action" "bidding")
       (events/pick1 "bid" bidder (bids/available-bids)))]))

M test/whist.janet => test/whist.janet +33 -33
@@ 23,10 23,10 @@

(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)))
  (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


@@ 35,11 35,11 @@
				 "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 (= {"name" "bid" "count" 1 :event "prompt_select" "player" "East" "from" (bids/available-bids {"count" 3 "direction" "up"})} east_prompt))
  (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 (= {"name" "bid" "count" 1 :event "prompt_select" "player" "East" "from" (bids/available-bids {"count" 3 "direction" "up"})} east-prompt))
  (is (= {"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"]))))



@@ 50,12 50,12 @@
				      "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 (= {"name" "bid" "count" 1 :event "prompt_select" "player" "South" "from" (bids/available-bids {"count" 3 "suit" "no_trumps"})} south_prompt))
  (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 (= {"name" "bid" "count" 1 :event "prompt_select" "player" "South" "from" (bids/available-bids {"count" 3 "suit" "no_trumps"})} south-prompt))
  (is (= {"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"]))))



@@ 66,10 66,10 @@
				     "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))
  (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 (= {} (get-in state [:meta "high_bid"])))
  (is (deep= @{"East" true "South" true "West" true} (get-in state [:meta "not_passed"]))))



@@ 81,11 81,11 @@
					  "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)
  (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 (= {"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 (= {"name" "bid" "count" 1 :event "prompt_select" "player" "East" "from" bids/suit} east-prompt))
  (is (= {"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"])))


@@ 97,10 97,10 @@
				     :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))
  (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


@@ 114,10 114,10 @@
				  :meta {"suit" :null}}
			  :action {"player" "North" "name" "discard" "value" [C2]}}]
    (def [state events] (whist/next player-discarded))
    (def [north_score north_discard north_prompt] events)
    (is (= {:event "add_score" "value" 1 "player" "North"} north_score))
    (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))
    (def [north-score north-discard north-prompt] events)
    (is (= {:event "add_score" "value" 1 "player" "North"} north-score))
    (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))))

(def- players-with-full-hands


@@ 132,13 132,13 @@
		    :state {:phase "play" :meta {"suit" :null} :stacks {"trick" []}}
		    :action {"player" "North" "name" "play" "value" CJ}})
  (def [state events] (whist/next player-lead))
  (def [north_decoration east_prompt] events)
  (def [north-decoration east-prompt] events)
  (let [played-card (merge-into @{"player" "North"} CJ)]
    (is (deep= @{"suit" "clubs"} (state :meta)))
    (is (deep= @{"trick" [played-card]}  (state :stacks))))
  (is (= {"value" "played \xE2\x99\xA3J" :event "add_decoration" "player" "North" "name" "play_action"} north_decoration))
  (is (= {"value" "played \xE2\x99\xA3J" :event "add_decoration" "player" "North" "name" "play_action"} north-decoration))
  (is (deep= {"player" "East" :event "prompt_play" "count" 1 "to" "trick"
		       "from" [DA D3 D4 D5 D6 D7 D8 D9 DT DJ DQ DK]}
	     east_prompt)))
	     east-prompt)))

(run-tests!)