Advertisement
wwwRong

ox_game_julia

Dec 20th, 2019
2,307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.46 KB | None | 0 0
  1. #=
  2.   แปลงมาจาก OX.py ในหนังสือ Artificial Intelligence
  3.   with Machine Learning ของ รศ.ดร.ปริญญา สงวนสัตย์
  4. =#
  5. using Random
  6. O, X = [], []
  7. win = [[1,2,3],
  8.       [4,5,6],
  9.       [7,8,9],
  10.       [1,4,7],
  11.       [2,5,8],
  12.       [3,6,9],
  13.       [1,5,9],
  14.       [3,5,7]]
  15.  
  16. function randomChoice(args)
  17.   args[rand(1:length(args))]
  18. end
  19.  
  20. function checkWin(P)
  21.   for w in win
  22.     if all([in(v,P) for v in w])
  23.       return true
  24.     end
  25.   end
  26.   false
  27. end
  28.  
  29. function displayOX()
  30.   newMap = [in(v,O) ? "[O]" : (in(v,X) ? "[X]" : "[_]") for v in 1:9]
  31.   for i = 1:3:9
  32.     println(newMap[i]*newMap[i+1]*newMap[i+2])
  33.   end
  34. end
  35.  
  36. function ai()
  37.   validMove = [v for v in 1:9 if !in(v,vcat(O,X))]
  38.   for w in win
  39.     xl = Dict([(v,in(v,X)) for v in w])
  40.     if count(i->i==true,values(xl))==2 && any([in(v,validMove) for v in w])
  41.       return [v[1] for v in pairs(xl) if v[2]==false][1]
  42.     end
  43.   end
  44.   V = [-100,-100,-100,-100,-100,-100,-100,-100,-100]
  45.   for v in validMove
  46.     tempX = push!(copy(X),v)
  47.     V[v], criticalMove = evalOX(O,tempX)
  48.     if length(criticalMove) > 0
  49.       #if checkWin(tempX)
  50.       #  return v
  51.       #end
  52.       move = [i for i in criticalMove if in(i,validMove)]
  53.       return randomChoice(move)
  54.     end
  55.   end
  56.   maxV = maximum(V)
  57.   imaxV = [v[1] for v in pairs(V) if v[2] == maxV]
  58.   randomChoice(imaxV)
  59. end
  60.  
  61. function evalOX(o,x)
  62.   SO, SX, criticalMove = calSOX(o,x)
  63.   (1 + SX - SO), criticalMove
  64. end
  65.  
  66. function calSOX(o,x)
  67.   SO, SX = 0, 0
  68.   criticalMove = []
  69.   for w in win
  70.     cO = [in(v,o) for v in w]
  71.     cX = [in(v,x) for v in w]
  72.     if !any(cX)
  73.       #nO = length(cO[cO .== true])
  74.       nO = count(i->i==true,cO)
  75.       SO = SO + nO
  76.       if nO == 2
  77.         println("critical ", w)
  78.         criticalMove = copy(w)
  79.       end
  80.     end
  81.     if !any(cO)
  82.       SX = SX + count(i->i==true,cX)
  83.     end
  84.   end
  85.   SO, SX, criticalMove
  86. end
  87.  
  88. while true
  89.   print("Choose position [1-9]: ")
  90.   move = tryparse(Int64,readline())
  91.   println("")
  92.   while isnothing(move) || in(move,vcat(O,X)) || move > 9 || move < 1
  93.     print("Bad move: choose position [1-9]: ")
  94.     move = tryparse(Int64,readline())
  95.     println("")
  96.   end
  97.   push!(O,move)
  98.   displayOX()
  99.   if checkWin(O)
  100.     println("O win")
  101.     break
  102.   end
  103.   if length(O) + length(X) == 9
  104.     println("Draw")
  105.     break
  106.   end
  107.   push!(X,ai())
  108.   displayOX()
  109.   if checkWin(X)
  110.     println("X win")
  111.     break
  112.   end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement