Advertisement
SciQuest_csp

MCTS-AI, TicTacToe

Mar 23rd, 2012
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. TicTacToe = class()
  2. -- pretty much this entire class is self documenting.
  3.  
  4.  function TicTacToe:init()
  5.      self.empty = -1
  6.      self.player = 0
  7.      self.ai = 1
  8.      self.turn = AIstarts
  9.      self.moves = 0
  10.      self.squares = {}
  11.      for i = 1, 9 do
  12.          table.insert(self.squares, i, self.empty)
  13.      end
  14.      self.gameWon = nil
  15.  end
  16.  
  17.  function TicTacToe:copy(orig)
  18.      for k,v in pairs(orig.squares) do
  19.          self.squares[k] = v
  20.      end
  21.      self.turn = orig.turn
  22.      self.moves = orig.moves
  23.      self.gameWon = orig.gameWon
  24.  end
  25.  
  26.  function TicTacToe:draw()
  27.      local size = math.min(WIDTH, HEIGHT)
  28.      size = size / 3
  29.      ellipseMode(CORNERS)
  30.      for i=1,9 do
  31.          local y = size*math.floor((i-1)/3)
  32.          local x = size*((i-1) % 3)
  33.          if self.squares[i] == self.ai then fill(blue)
  34.          elseif self.squares[i] == self.player then fill(red)
  35.          else fill(gray) end
  36.          ellipse(x,y, x+size, y+size)
  37.      end
  38.  end
  39.  
  40.  function TicTacToe:touched(t)
  41.      local n = 1
  42.      if (HEIGHT/3) < t.y then
  43.          if (HEIGHT*2/3) < t.y then
  44.              n = n + 6
  45.          else
  46.              n = n + 3
  47.          end
  48.      end
  49.      
  50.      if (WIDTH/3) < t.x then
  51.          if (WIDTH*2/3) < t.x then
  52.              n = n + 2
  53.          else
  54.              n = n + 1
  55.          end
  56.      end
  57.      
  58.      if self:validMove(n) then
  59.          self:move(n)
  60.      else
  61.          print("move invalid.")
  62.      end
  63.  end
  64.  
  65.  function TicTacToe:move(n)
  66.      self.moves = self.moves + 1
  67.      self.squares[n] = self.turn
  68.      self:updateWinner()
  69.      flipTurn(self)
  70.  end
  71.  
  72. -- we have no params.
  73.  function TicTacToe:saveParams() end
  74.  
  75. -- to increase efficiency i could pass it the most recent move,
  76. -- and only check what i need to.
  77. -- however, tictactoe's ai is not really performance limited.
  78.  function TicTacToe:updateWinner()
  79.      if self.gameWon ~= nil then return end
  80.      
  81.      if self:chain(1,5,9) or self:chain(3,5,7) then
  82.          self.gameWon = self.turn
  83.          return
  84.      end
  85.      
  86.      for i=0,2 do
  87.          if self:chain(i*3+1, i*3+2, i*3+3) or self:chain(i+1, i+4, i+7) then
  88.              self.gameWon = self.turn
  89.              return
  90.          end
  91.      end
  92.  end
  93.  
  94. -- just makes sure that the values corresponding to the indexes a, b, & c are the same
  95.  function TicTacToe:chain(a,b,c)
  96.      local val = self.squares[a]==self.squares[c] and self.squares[a]~=self.empty
  97.      return val and self.squares[a]==self.squares[b]
  98.  end
  99.  
  100.  function TicTacToe:gameOver()  
  101.      return self.gameWon
  102.  end
  103.  
  104.  function TicTacToe:randomMove(n)
  105.      myMove = math.random(n)
  106.      for k,v in pairs(self.squares) do
  107.          if v == self.empty then
  108.              myMove = myMove-1
  109.              if myMove == 0 then
  110.                  self:move(k)
  111.                  return
  112.              end
  113.          end
  114.      end
  115.  end
  116.  
  117.  function TicTacToe:possibleMovesInterval()
  118.      return vec2(1,9)
  119.  end
  120.  
  121.  function TicTacToe:validMove(n)
  122.      if self.squares[n] == self.empty then
  123.          return true
  124.      else
  125.          return false
  126.      end
  127.  end
  128.  
  129. -- no real heuristic. its tictactoe, just simulate to game completion!
  130.  function TicTacToe:positionStrength() return 0.5 end
  131.  
  132. -- this SHOULD be able to be implemented with just "return 9-moves"
  133. -- but that doesnt work for some reason. why?
  134.  function TicTacToe:numberOfMoves()
  135.      local sum = 0
  136.      for i=1,9 do
  137.          if self.squares[i] == self.empty then
  138.              sum = sum + 1
  139.          end
  140.      end
  141.      return sum
  142.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement