Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; แปลงมาจาก OX.py ในหนังสือ Artificial Intelligence
- ;; with Machine Learning ของ รศ.ดร.ปริญญา สงวนสัตย์
- (local M [false false false false false false false false false])
- (local win [[1 2 3]
- [4 5 6]
- [7 8 9]
- [1 4 7]
- [2 5 8]
- [3 6 9]
- [1 5 9]
- [3 5 7]])
- (local rnd math.random)
- (local max math.max)
- (local floor math.floor)
- (fn floor-n [x]
- (let [n (tonumber x)]
- (if n (floor n)
- 0)))
- ;ทำเรียกใช้ table function สะดวกขึ้น
- (local add table.insert)
- (local unpak table.unpack)
- (local concat table.concat)
- ;(local insp (require "inspect"))
- (fn all [p w]
- "ให้ p เป็น 'O' หรือ 'X'"
- (= (. M (. w 1)) (. M (. w 2)) (. M (. w 3)) p))
- (fn any [p w m]
- "ให้ p เป็น 'O' หรือ 'X'"
- (var t 0)
- (var move (or m M))
- (when (= p (. move (. w 1))) (set t (+ t 1)))
- (when (= p (. move (. w 2))) (set t (+ t 1)))
- (when (= p (. move (. w 3))) (set t (+ t 1)))
- t)
- (fn random-choice [args]
- (. args (rnd (length args))))
- ;(fn check-win [p]
- ; (var (result i) (values true 1))
- ; (while (<= i (length win))
- ; (set result (all p (. win i)))
- ; (if result
- ; (set i (+ (length win) 1))
- ; (set i (+ i 1))))
- ; result)
- ;(fn check-win [p]
- ; ":until in fennel 0.9+"
- ; (var result false)
- ; (each [_ w (ipairs win) :until result]
- ; (set result (all p w)))
- ; result)
- ;(fn check-win [p]
- ; "lua escape hatch"
- ; (each [_ w (ipairs win)])
- ; (when (all p w)
- ; (lua "return true")))
- ; false)
- (fn check-win [p w k r]
- "ตรวจผลชนะแบบ tail recursion"
- (var (key line) (next win k))
- (if (or (not w) (and key (not r)))
- (do
- (check-win p line key (all p line)))
- r))
- ;(fn check-draw []
- ; (var (draw i) (values true 1))
- ; (while (or draw (<= i 9))
- ; (set draw (and draw (. M i)))
- ; (set i (+ i 1)))
- ; draw)
- ;(fn check-draw []
- ; (and (. M 1)
- ; (. M 2)
- ; (. M 3)
- ; (. M 4)
- ; (. M 5)
- ; (. M 6)
- ; (. M 7)
- ; (. M 8)
- ; (. M 9)))
- ;(fn check-draw []
- ; ":until in fennel 0.9+"
- ; (var draw true)
- ; (each [_ v (ipairs M) :until (not draw)]
- ; (when (not v)
- ; (set draw v)))
- ; draw)
- ;(fn check-draw []
- ; "lua escape hatch"
- ; (each [_ v (ipairs M)]
- ; (when (not v)
- ; (lua "return false"))
- ; true)
- (fn check-draw [i r]
- "ตรวจสอบว่าเสมอแบบ tail recursion"
- (var n (or i 0))
- (var d (or (and (= r nil) true) r))
- (if (or (= i 9) (= r false))
- r
- (do
- (set n (+ n 1))
- (check-draw n (and d (. M n))))))
- (fn display []
- "แสดงผลตาราง"
- (for [i 1 9 3]
- (let [m1 (or (and (. M i) (.. "[" (. M i) "]")) "[_]")
- m2 (or (and (. M (+ i 1)) (.. "[" (. M (+ i 1)) "]")) "[_]")
- m3 (or (and (. M (+ i 2)) (.. "[" (. M (+ i 2)) "]")) "[_]")]
- (print (.. m1 m2 m3)))))
- (fn cal-sox [m]
- (var (so sx) (values 0 0))
- (var critical-move [])
- (each [_ w (pairs win)]
- (let [co (any "O" w m)
- cx (any "X" w m)]
- (when (= 0 cx)
- (set so (+ so co))
- (when (= 2 co)
- (print "critical" (.. "{" (concat w ",") "}"))
- (set critical-move w))
- (when (= 0 co)
- (set sx (+ sx cx))))))
- (values so sx critical-move))
- (fn eval-ox [m]
- (let [(so sx critical-move) (cal-sox m)]
- (values (- (+ 1 sx) so) critical-move)))
- (fn ai-method1 [move]
- (var (v i continue) (values nil 1 true))
- (while (and (= v nil) (<= i (length win)))
- (let [w (. win i)
- cx (any "X" w)]
- (when (= cx 2)
- (if (. move (. w 1)) (set v (. w 1))
- (. move (. w 2)) (set v (. w 2))
- (. move (. w 3)) (set v (. w 3)))))
- (set i (+ i 1)))
- v)
- (fn ai-method2 [move]
- (var V [-100 -100 -100 -100 -100 -100 -100 -100 -100])
- (var c nil)
- (var i 1)
- (while (and (= c nil) (<= i (length move)))
- (when (. move i)
- (var temp-x [(unpak M)])
- (tset temp-x i "X")
- (let [(Vi critical-move) (eval-ox temp-x)]
- (tset V i Vi)
- (when (> (length critical-move) 0)
- (var [c1 c2 c3] critical-move)
- (if (. move c1)
- (set c c1)
- (. move c2)
- (set c c2)
- (set c c3)))))
- (set i (+ i 1)))
- (if c c
- (do
- (var max-v (max (unpak V)))
- (var imax-v [])
- (each [i v (pairs V)]
- (when (= v max-v)
- (add imax-v i)))
- (random-choice imax-v))))
- (fn ai []
- (var valid-move [true true true true true true true true true])
- (var v nil)
- (for [i 1 9]
- (tset valid-move i (not (. M i))))
- (set v (ai-method1 valid-move))
- (when (= v nil)
- (set v (ai-method2 valid-move)))
- v)
- (fn main []
- (var continue true)
- (var move nil)
- (while continue
- (if (or (= move nil) (and (< 0 move 10) (not (. M move))))
- (io.write "Choose position [1-9]: ")
- (io.write "Bad move: choose position [1-9]: "))
- (set move (floor-n (io.read)))
- (print "")
- (when (and (< 0 move 10))
- (when (not (. M move))
- (tset M move "O")
- (display)
- (if (check-win "O")
- (do
- (print "O win")
- (set continue false))
- (not= false (check-draw))
- (do
- (print "Draw")
- (set continue false))
- (do
- (tset M (ai) "X")
- (display)
- (when (check-win "X")
- (print "X win")
- (set continue false))))
- (set move nil)))))
- (main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement