ForeverDev

Untitled

Oct 5th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. function getResult(board)
  2.     local tie = true
  3.     for _, node in ipairs(board) do
  4.         if node == " " then
  5.             tie = false
  6.             break
  7.         end
  8.     end
  9.     if tie then
  10.         return 3
  11.     end
  12.     return (
  13.         (board[1] == board[2] and board[1] == board[3] and board[1] ~= " ") and (board[1] == "X" and 1 or 2) or
  14.         (board[4] == board[5] and board[3] == board[6] and board[4] ~= " ") and (board[4] == "X" and 1 or 2) or
  15.         (board[7] == board[8] and board[7] == board[9] and board[7] ~= " ") and (board[7] == "X" and 1 or 2) or
  16.         (board[1] == board[4] and board[1] == board[7] and board[1] ~= " ") and (board[1] == "X" and 1 or 2) or
  17.         (board[2] == board[5] and board[2] == board[8] and board[2] ~= " ") and (board[2] == "X" and 1 or 2) or
  18.         (board[3] == board[6] and board[3] == board[9] and board[3] ~= " ") and (board[3] == "X" and 1 or 2) or
  19.         (board[1] == board[5] and board[1] == board[9] and board[1] ~= " ") and (board[1] == "X" and 1 or 2) or
  20.         (board[3] == board[5] and board[3] == board[7] and board[3] ~= " ") and (board[3] == "X" and 1 or 2) or
  21.         0
  22.     )
  23. end
  24.  
  25. function drawBoard(board)
  26.     print (
  27.         board[1] .. " | " .. board[2] .. " | " .. board[3] .. "\n" ..
  28.         board[4] .. " | " .. board[5] .. " | " .. board[6] .. "\n" ..
  29.         board[7] .. " | " .. board[8] .. " | " .. board[9]
  30.     )
  31. end
  32.  
  33. function removeElement(array, element)
  34.     for index, value in ipairs(array) do
  35.         if value == element then
  36.             table.remove(array, index)
  37.             return
  38.         end
  39.     end
  40. end
  41.  
  42. function makeEnemyMove(array)
  43.     local options = {}
  44.     for index, value in ipairs(array) do
  45.         if value == " " then
  46.             table.insert(options, index)
  47.         end
  48.     end
  49.     if #options > 0 then
  50.         array[options[math.random(#options)]] = "O"
  51.     end
  52.     return array
  53. end
  54.  
  55. while true do
  56.     local grid = {" ", " ", " ", " ", " ", " ", " ", " ", " "}
  57.     local result;
  58.     repeat
  59.         drawBoard(grid)
  60.         local choice = tonumber(io.read())
  61.         if grid[choice] == " " then
  62.             grid[choice] = "X"
  63.         end
  64.         grid = makeEnemyMove(grid)
  65.         result = getResult(grid)
  66.     until result > 0
  67.     drawBoard(grid)
  68.     print (
  69.         result == 1 and "You win!" or
  70.         result == 2 and "You lose!" or
  71.         "It's a tie!"
  72.     )
  73.     io.write("Press enter to play again.\n")
  74.     io.read()
  75. end
Advertisement
Add Comment
Please, Sign In to add comment