マルバツゲーム
Posted feedbacks - Arc
srfi-1 の lset<= もどきを自作。
実行結果:
arc> (nplay-ox 10000)
o win: 5835
x win: 2889
draw: 1276
nil
実行結果:
arc> (nplay-ox 10000)
o win: 5835
x win: 2889
draw: 1276
nil
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 | (def lset<= (s1 s2)
(if (is s1 nil) t
(mem (car s1) s2) (lset<= (cdr s1) s2)
nil))
(def check (lis)
(when (< (len lis) 3) nil)
(or (lset<= '(1 2 3) lis)
(lset<= '(1 4 7) lis)
(lset<= '(1 5 9) lis)
(lset<= '(2 5 8) lis)
(lset<= '(3 5 7) lis)
(lset<= '(3 6 9) lis)
(lset<= '(4 5 6) lis)
(lset<= '(7 8 9) lis)))
(def o-player (omark xmark pool)
(withs (picked (random-elt pool)
om (cons picked omark))
(if (check om) 'o
(is (cdr pool) nil) 'd
(x-player om xmark (rem picked pool)))))
(def x-player (omark xmark pool)
(withs (picked (random-elt pool)
xm (cons picked xmark))
(if (check xm) 'x
(o-player omark xm (rem picked pool)))))
(def nplay-ox (n)
(let wl (map (fn (_) (o-player nil nil (range 1 9))) (range 1 n))
(prn "o win: " (count 'o wl))
(prn "x win: " (count 'x wl))
(prn "draw: " (count 'd wl))
nil))
|


syat
#6190()
Rating4/4=1.00
マルバツゲームは3×3の格子に交互に○と×を書き込み、先に縦・横・斜めに記号をそろえたほうが勝ちというおなじみのゲームです。
「毎ターン乱数を使って手を決めるランダムプレイヤー同士を対戦させる」というのが今回のお題です。 1万回対戦させ、勝ち・負け・引き分けの数を表示してください。 そして先手が有利であることを確かめてください。
良い手を思考するプレイヤーについては別のお題にしようと思っています。 プレイヤーを簡単に差し換えることができる設計を目指してください。
[ reply ]