Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TicTacToe = class()
- -- pretty much this entire class is self documenting.
- function TicTacToe:init()
- self.empty = -1
- self.player = 0
- self.ai = 1
- self.turn = AIstarts
- self.moves = 0
- self.squares = {}
- for i = 1, 9 do
- table.insert(self.squares, i, self.empty)
- end
- self.gameWon = nil
- end
- function TicTacToe:copy(orig)
- for k,v in pairs(orig.squares) do
- self.squares[k] = v
- end
- self.turn = orig.turn
- self.moves = orig.moves
- self.gameWon = orig.gameWon
- end
- function TicTacToe:draw()
- local size = math.min(WIDTH, HEIGHT)
- size = size / 3
- ellipseMode(CORNERS)
- for i=1,9 do
- local y = size*math.floor((i-1)/3)
- local x = size*((i-1) % 3)
- if self.squares[i] == self.ai then fill(blue)
- elseif self.squares[i] == self.player then fill(red)
- else fill(gray) end
- ellipse(x,y, x+size, y+size)
- end
- end
- function TicTacToe:touched(t)
- local n = 1
- if (HEIGHT/3) < t.y then
- if (HEIGHT*2/3) < t.y then
- n = n + 6
- else
- n = n + 3
- end
- end
- if (WIDTH/3) < t.x then
- if (WIDTH*2/3) < t.x then
- n = n + 2
- else
- n = n + 1
- end
- end
- if self:validMove(n) then
- self:move(n)
- else
- print("move invalid.")
- end
- end
- function TicTacToe:move(n)
- self.moves = self.moves + 1
- self.squares[n] = self.turn
- self:updateWinner()
- flipTurn(self)
- end
- -- we have no params.
- function TicTacToe:saveParams() end
- -- to increase efficiency i could pass it the most recent move,
- -- and only check what i need to.
- -- however, tictactoe's ai is not really performance limited.
- function TicTacToe:updateWinner()
- if self.gameWon ~= nil then return end
- if self:chain(1,5,9) or self:chain(3,5,7) then
- self.gameWon = self.turn
- return
- end
- for i=0,2 do
- if self:chain(i*3+1, i*3+2, i*3+3) or self:chain(i+1, i+4, i+7) then
- self.gameWon = self.turn
- return
- end
- end
- end
- -- just makes sure that the values corresponding to the indexes a, b, & c are the same
- function TicTacToe:chain(a,b,c)
- local val = self.squares[a]==self.squares[c] and self.squares[a]~=self.empty
- return val and self.squares[a]==self.squares[b]
- end
- function TicTacToe:gameOver()
- return self.gameWon
- end
- function TicTacToe:randomMove(n)
- myMove = math.random(n)
- for k,v in pairs(self.squares) do
- if v == self.empty then
- myMove = myMove-1
- if myMove == 0 then
- self:move(k)
- return
- end
- end
- end
- end
- function TicTacToe:possibleMovesInterval()
- return vec2(1,9)
- end
- function TicTacToe:validMove(n)
- if self.squares[n] == self.empty then
- return true
- else
- return false
- end
- end
- -- no real heuristic. its tictactoe, just simulate to game completion!
- function TicTacToe:positionStrength() return 0.5 end
- -- this SHOULD be able to be implemented with just "return 9-moves"
- -- but that doesnt work for some reason. why?
- function TicTacToe:numberOfMoves()
- local sum = 0
- for i=1,9 do
- if self.squares[i] == self.empty then
- sum = sum + 1
- end
- end
- return sum
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement