Clojureで七並べ序章

;全自動モード (def AUTO_MODE false) ;プレイヤー人数 (def PLAYER_NUMB […]

;全自動モード (def AUTO_MODE false) ;プレイヤー人数 (def PLAYER_NUMB […]

;全自動モード
(def AUTO_MODE false)
;プレイヤー人数
(def PLAYER_NUMBER 4)
;パス回数
(def PASS_NUMBER 3)

;トランプカードクラス
(def TrumpCard-suitStrs ["▲" "▼" "◆" "■" "Jo" "JO"])
(def TrumpCard-powerStrs ["A" "2" "3" "4" "5" "6" "7" "8" "9" "10" "J" "Q" "K" "KR"])
(def TrumpCard-suits 4)
(def TrumpCard-powers 13)
(defrecord TrumpCard[name suit power])
(defn make-TrumpCard[suit power]
	(let [name (str (nth TrumpCard-suitStrs suit) (nth TrumpCard-powerStrs power))]
		(TrumpCard. name suit power)
	)
)

;トランプの束クラス
(defprotocol ITrumpDeck
	(-count[self])
	(-shuffle[self])
	(draw[self])
)
(defrecord TrumpDeck[__g __deck] ITrumpDeck
	(-count[self]
		(count @__deck)
	)
	(-shuffle[self]
		(swap! __deck shuffle)
	)
	(draw[self]
		(let [card (nth @__deck @__g)]
			(swap! __g inc)
			card
		)
	)
)
(defn make-TrumpDack[& {:keys [jokers] :or {jokers 0}}]
	(let [
		deck (vec(concat
			(apply concat (for[suit (range TrumpCard-suits)]
				(for[power (range TrumpCard-powers)]
					(make-TrumpCard suit power)
				)
		))
		(for[jokers (range jokers)]
			(make-TrumpCard (+ TrumpCard-suits jokers) TrumpCard-powers)
		)))
	]
		(TrumpDeck. (atom 0) (atom deck))
	)
)

;プレイヤークラス
(defprotocol IPlayer
	(sortDeck[self])
	(addCard[self card])
	(removeCard[self cardName])
	(existCard[self cardName])
	(gameOut[self])
)
(def MPlayer{
	:sortDeck (fn[self]
		(let [sortValue (fn[v] (+(*(:suit v) TrumpCard-powers) (:power v)))]
			(swap! (:deck self) #(sort-by sortValue %1))
		)
	)
	:addCard (fn[self card]
		(swap! (:deck self) #(cons card %1))
	)
	:removeCard (fn[self cardName]
		(swap! (:deck self) #(remove (fn[card] (= cardName (:name card))) %1))
	)
	:existCard (fn[self cardName]
		(keep-indexed (fn[index card] (if(= cardName (:name card)) index)) @(:deck self))
	)
	:gameOut (fn[self]
		(reset! (:isGameOut self) true)
	)
})
(defrecord Player[deck id name isGameOut])
(extend Player IPlayer MPlayer)
(defn make-Player[id name]
	(Player. (atom '()) id name (atom false))
)

;メイン処理
(loop[i 0](when(< i 100)
	(println)
(recur(inc i))))
(print "
/---------------------------------------/
/                 七並べ                /
/---------------------------------------/
")

(let [
	trp (make-TrumpDack)
	p (atom '())
	pid (atom 0)
]
	(-shuffle trp)

	(loop[i 0](when(< i PLAYER_NUMBER)
		(swap! p #(cons (make-Player @pid (str "Player " (inc @pid))) %1))
		(swap! pid inc)
	(recur(inc i))))
	(swap! p reverse)

	(loop[i 0](when(< i (-count trp))
		(addCard (nth @p (mod i PLAYER_NUMBER)) (draw trp))
	(recur(inc i))))
	(doseq[pl @p]
		(sortDeck pl)
		(printf "\n【%s】\n" (:name pl))
		(doseq[card @(:deck pl)]
			(print (:name card))
		)
	)
)