~subsetpark/whist

a85d88a3830a205b3210fba534d38e2b23239da0 — Zach Smith 6 months ago 1841866
Shrink through pattern matching
1 files changed, 19 insertions(+), 25 deletions(-)

M game/bid.janet
M game/bid.janet => game/bid.janet +19 -25
@@ 3,10 3,8 @@
(import events)

(defn- new-meta
  [state action]
  (let [meta (state :meta)
	{:bid previous-high-bid :player previous-high-bidder} (meta :high_bid)
	{:value last-bid :player last-bidder} action
  [{:meta meta} {:value last-bid :player last-bidder}]
  (let [{:bid previous-high-bid :player previous-high-bidder} (meta :high_bid)
	# Record the high bid (whether it's a new bid or the existing high bid).
	[high-bid high-bidder] (case last-bid
				 "pass" [previous-high-bid previous-high-bidder]


@@ 14,29 12,25 @@
    @{:high_bid @{:player high-bidder :bid high-bid}}))

(defn- initial-events
  [state action high-bid]
  (let [meta (state :meta)
	{:bid previous-high-bid :player previous-high-bidder} (meta :high_bid)
	{:value last-bid :player last-bidder} action]
    (case last-bid
      "pass" @[(events/add-decoration last-bidder "bid_action" "passed")]
      # Handle a bid. 
      (array/concat 
       (case previous-high-bidder
	 nil @[]
	 @[(events/clear-decoration previous-high-bidder "bid_action")])
       (events/add-decoration last-bidder "bid_action" "declarer")
       (events/add-decoration last-bidder "bid" (bids/to-text high-bid))))))
  [{:meta {:high_bid {:bid previous-high-bid :player previous-high-bidder}}}
   {:value last-bid :player last-bidder}
   current-high-bid]
  (case last-bid
    "pass" @[(events/add-decoration last-bidder "bid_action" "passed")]
    # Handle a bid. 
    (array/concat 
     (case previous-high-bidder
       nil @[]
       @[(events/clear-decoration previous-high-bidder "bid_action")])
     (events/add-decoration last-bidder "bid_action" "declarer")
     (events/add-decoration last-bidder "bid" (bids/to-text current-high-bid)))))

(defn- update-not-passed
  [state action]
  (let [meta (state :meta)
	{:value last-bid :player last-bidder} action
	not-passed (meta :not_passed)]
    (case last-bid
      # Handle a new pass. Mark the player as passed by removing them from the set.
      "pass" (put not-passed (keyword last-bidder) nil))
    not-passed))
  [{:meta {:not_passed not-passed}} {:value last-bid :player last-bidder}]
  (case last-bid
    # Handle a new pass. Mark the player as passed by removing them from the set.
    "pass" (put not-passed (keyword last-bidder) nil))
    not-passed)

(defn bid-phase
  ```