Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- red = color(255, 0, 0, 255)
- blue = color(0, 0, 255, 255)
- black = color(0, 0, 0, 255)
- white = color(255, 255, 255, 255)
- gray = color(127, 127, 127, 255)
- function setupParams()
- -- iparameter("Game", 0, 3)
- iparameter("AIonline", 0, 1)
- iparameter("AIstarts", 0, 1)
- parameter("Difficulty", 0, 4)
- iparameter("MaxSearchDepth", 0, 20)
- -- Game = readProjectData("game", 0)
- AIonline = readProjectData("aiOnline", 1)
- AIstarts = readProjectData("aiStarts", 0)
- Difficulty = readProjectData("difficulty", 2)
- MaxSearchDepth = readProjectData("maxSearchDepth", 6)
- end
- function saveParams()
- -- saveProjectData("game", Game)
- saveProjectData("aiOnline", AIonline)
- saveProjectData("aiStarts", AIstarts)
- saveProjectData("difficulty", Difficulty)
- saveProjectData("maxSearchDepth", MaxSearchDepth)
- end
- function setup()
- wait = true
- setupParams()
- strokeWidth(5)
- -- prevGame = 0
- board = Othello( 8,8 )
- sim = Othello( 8,8 )
- end
- function startGame()
- end
- function draw()
- --background(40, 40, 50)
- saveParams()
- -- print(board:positionStrength())
- board:draw()
- if board.turn == ai and AIonline == 1 then
- -- print("random moving")
- mcts()
- -- board:randomMove()
- end
- end
- function touched(t)
- if t.state == ENDED then
- print("move acknowledged. please wait...")
- board:touched(t)
- wait = true
- end
- end
- function mcts()
- wait = true
- local playouts = math.floor(10^Difficulty)
- possibleMoves = {}
- searchResults = {}
- for k,v in pairs(board.moves[board.turn+1]) do
- -- print(board.moves[self.turn+1][])
- table.insert(possibleMoves, v)
- table.insert(searchResults, 0)
- end
- local count = #possibleMoves
- print("number of possible moves is "..count)
- if count == 0 then fin = true return end
- -- local sim = ConnectFour(board.numberOfRows, board.numberOfColumns, board.turn, board.goal)
- for i = 1, count do
- local sum = 0
- for j = 1, playouts do
- sim:copy(board)
- -- print("moving "..i)
- print(possibleMoves[i])
- sim:move(possibleMoves[i].x, possibleMoves[j].y)
- -- print("---playout "..j)
- local playoutResult = playout(sim)
- -- print(playoutResult)
- sum = sum + playoutResult
- end
- sum = sum / playouts
- searchResults[i] = sum
- print("probability for move "..possibleMoves[i].." is "..sum)
- end
- -- we should want to maximize the search result
- bestResult = -99
- bestIndex = -99
- for i,r in pairs(searchResults) do
- -- print("best "..bestResult)
- -- print("r "..r)
- if bestResult < r then
- bestResult = r
- bestIndex = i
- end
- end
- print("bestResult is "..bestResult)
- print("bestMove is "..possibleMoves[bestIndex])
- if bestIndex == -99 then
- print("oops! mcts failed? (thats not good)")
- else
- board:move(possibleMoves[bestIndex])
- end
- end
- function playout(b)
- local result = b:gameOver()
- local searchDepth = 0
- -- print("playing out")
- while (result == nil) do
- local movesLeft = b:numberOfMoves()
- if movesLeft == 0 or MaxSearchDepth <= searchDepth and MaxSearchDepth ~= 0 then
- return b:positionStrength()
- end
- b:randomMove( movesLeft )
- searchDepth = searchDepth + 1
- result = b:gameOver()
- end
- return result
- end
- function flipTurn(b)
- if b.turn == b.player then
- b.turn = b.ai
- else
- b.turn = b.player
- end
- end
- function randBool()
- return math.random(2) == 1
- end
Advertisement
Add Comment
Please, Sign In to add comment