Comment detail

マルバツゲーム (Nested Flatten)
srfi-1 の lset<= もどきを自作。

実行結果:
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))

Index

Feed

Other

Link

Pathtraq

loading...