Advertisement
programcreator

TicTacToe

Oct 14th, 2015
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --[[
  2.   Tic Tac Toe with some real AI
  3.   by Wassil Janssen a.k.a. Creator
  4. ]]--
  5. if not AI then os.loadAPI("AI") end
  6. grid = {
  7.   {1,2,3},
  8.   {4,5,6},
  9.   {7,8,9}
  10. }
  11.  
  12. win = {
  13.   {1,2,3},
  14.   {4,5,6},
  15.   {7,8,9},
  16.   {1,4,7},
  17.   {2,5,8},
  18.   {3,6,9},
  19.   {1,5,9},
  20.   {3,5,7},
  21. }
  22. local myNet = 0
  23. function seewin(taken)
  24.   for i=1,8 do
  25.     local first = win[1]
  26.     if taken[win[i][1]] and taken[win[i][2]] == taken[win[i][3]] and taken[win[i][2]] == taken[win[i][1]] then
  27.       return true
  28.     end
  29.   end
  30.   return false
  31. end
  32.  
  33. if fs.exists("tic.net") then
  34.   file = fs.open("tic.net","r")
  35.   local buf = file.readAll()
  36.   file.close()
  37.   myNet = AI.unserialize(textutils.unserialize(buf))
  38. else
  39.   myNet = AI.Net({18,19,19,19,9})
  40. end
  41.  
  42. function game()
  43. taken = {}
  44. moves = 0
  45.  
  46. function draw()
  47.   term.setBackgroundColor(colors.black)
  48.   term.setTextColor(colors.white)
  49.   term.clear()
  50.   local count = 1
  51.   for i=1,3 do
  52.     for m=1,3 do
  53.       term.setCursorPos(m,i)
  54.       term.setTextColor(taken[count] == "X" and colors.green or taken[count] == "O" and colors.red or colors.white)
  55.       term.write(taken[count] or count)
  56.       count = count + 1
  57.     end
  58.   end
  59. end
  60.  
  61. while true do
  62.   local nInput = 0
  63.   repeat
  64.     draw()
  65.     term.setTextColor(colors.white)
  66.     term.setCursorPos(2,5)
  67.     term.write("Input your number, player 1:")
  68.     term.setCursorPos(31,5)
  69.     local sInput = read()
  70.     nInput = tonumber(sInput)
  71.   until #sInput == 1 and nInput and not taken[nInput]
  72.   taken[nInput] = "X"
  73.   moves = moves + 1
  74.   if seewin(taken) then return "X won" end
  75.   if moves == 9 then return "A tie" end
  76.  
  77.   local results = myNet.getResults()
  78.   local input = 1
  79.   local scale = 0
  80.   for i=1,9 do
  81.     if not taken[i] and results[i] > scale then
  82.       input = i
  83.     end
  84.   end
  85.   taken[input] = "O"
  86.  
  87.   moves = moves + 1
  88.  
  89.   local buffer = {}
  90.   for i=1,9 do
  91.     buffer[i] = taken[i] == "X" and 1 or 0
  92.     buffer[i+9] = taken[i] == "O" and 1 or 0
  93.   end
  94.   local buffer2 = {}
  95.   for i=1,9 do
  96.     buffer2[i] = i == nInput and 1 or 0
  97.   end
  98.   myNet.feedForward(buffer)
  99.   myNet.backProp(buffer2)
  100.   if seewin(taken) then return "O won" end
  101. end
  102. end
  103.  
  104. while true do
  105.   print(game())
  106.   read()
  107.   file = fs.open("tic.net","w")
  108.   file.write(textutils.serialize(myNet.serialize()))
  109.   file.close()
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement