Advertisement
wwwRong

ox_game2_moon

Apr 18th, 2021
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. O = {
  2.   false,false,false
  3.   false,false,false
  4.   false,false,false
  5. }
  6. X = {
  7.   false,false,false
  8.   false,false,false
  9.   false,false,false
  10. }
  11. win = {
  12.   {1,2,3}
  13.   {4,5,6}
  14.   {7,8,9}
  15.   {1,4,7}
  16.   {2,5,8}
  17.   {3,6,9}
  18.   {1,5,9}
  19.   {3,5,7}
  20. }
  21.  
  22. is = require "inspect"
  23. rnd = math.random
  24. max = math.max
  25.  
  26. floor = math.floor
  27.  
  28. add = table.insert
  29. unpak = table.unpack
  30. concat = table.concat
  31.  
  32. all = (player_move, win_case) ->
  33.   player_move[win_case[1]] and
  34.     player_move[win_case[2]] and
  35.       player_move[win_case[3]]
  36.  
  37. any = (player_move, win_case) ->
  38.   t = 0
  39.   t += (player_move[win_case[1]] and 1 or 0)
  40.   t += (player_move[win_case[2]] and 1 or 0)
  41.   t += (player_move[win_case[3]] and 1 or 0)
  42.   t
  43.  
  44. randomChoice = (args) -> args[rnd #args]
  45.  
  46. checkWin = (P) ->
  47.   for _, w in pairs win
  48.     if all P, w
  49.       return true
  50.   false
  51.  
  52. displayOX = ->
  53.   newMap = {}
  54.   for i = 1, 9, 3
  55.     newMap[i] = O[i] and "[O]" or (X[i] and "[X]" or "[_]")
  56.     newMap[i+1] = O[i+1] and "[O]" or (X[i+1] and "[X]" or "[_]")
  57.     newMap[i+2] = O[i+2] and "[O]" or (X[i+2] and "[X]" or "[_]")
  58.     print(newMap[i]..newMap[i+1]..newMap[i+2])
  59.  
  60. calSOX = (o, x) ->
  61.   SO, SX = 0, 0
  62.   criticalMove = {}
  63.   for _, w in ipairs win
  64.     cO = any o, w
  65.     cX = any x, w
  66.     if cX == 0
  67.       SO = SO + cO
  68.       if cO == 2
  69.         print "critical", "{".. concat(w,",").."}"
  70.         criticalMove = w
  71.     if cO == 0
  72.       SX = SX + cX
  73.   SO, SX, criticalMove
  74.  
  75. evalOX = (o, x) ->
  76.   SO, SX, criticalMove = calSOX o, x
  77.   (1 + SX - SO), criticalMove
  78.  
  79. ai = ->
  80.   validMove = {
  81.     true,true,true
  82.     true,true,true
  83.     true,true,true
  84.   }
  85.   validMove = [(not (O[i] or X[i]) and validMove[i]) for i = 1, 9]
  86.  
  87.   for _, w in ipairs win
  88.     cX = any X, w
  89.     if cX == 2
  90.       for _, v in ipairs w
  91.         return v if validMove[v]
  92.  
  93.   V = {
  94.     -100,-100,-100
  95.     -100,-100,-100
  96.     -100,-100,-100
  97.   }
  98.   for i, v in ipairs validMove
  99.     if v
  100.       tempX, criticalMove = {unpak X}, {}
  101.       tempX[i] = v
  102.       V[i], criticalMove = evalOX O, tempX
  103.       if #criticalMove > 0
  104.         for _, c in pairs criticalMove
  105.           if validMove[c]
  106.             return c
  107.  
  108.   maxV = max unpak V
  109.   imaxV = {}
  110.   for i, v in pairs V
  111.     if v == maxV then add imaxV, i
  112.   return randomChoice imaxV
  113.  
  114. chooseMove = (e)->
  115.   if e
  116.     io.write "Bad move: choose position [1-9]: "
  117.   else
  118.     io.write "Choose position [1-9]: "
  119.   move = tonumber io.read!
  120.   move = floor move if move
  121.   print ""
  122.   move
  123.  
  124. while true
  125.   move = chooseMove!
  126.   validMove = true
  127.   while (not move) or move > 9 or move < 1 or O[move] or X[move]
  128.     move = chooseMove true
  129.   O[move] = true
  130.   displayOX!
  131.   if checkWin O
  132.     print "O win"
  133.     break
  134.   d = true
  135.   for i = 1, 9 do d = (O[i] or X[i]) and d
  136.   if d
  137.     print "Draw"
  138.     break
  139.   X[ai!] = true
  140.   displayOX!
  141.   if checkWin X
  142.     print "X win"
  143.     break
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement