Guest User

Untitled

a guest
Oct 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. board =
  2. {
  3. {1,2,3},
  4. {4,5,6},
  5. {7,8,9},
  6. }
  7.  
  8.  
  9. function play(piece)
  10. local pos = tonumber(io.read())
  11. while not isFree(board, pos) do
  12. print("Posicao ocupada. Tente outra:")
  13. pos = tonumber(io.read())
  14. end
  15. local i, j = getIndices(pos)
  16. board[i][j] = piece
  17. end
  18.  
  19.  
  20. function isPiece(piece)
  21. return piece == "O" or piece == "X"
  22. end
  23.  
  24.  
  25. function isFree(board, pos)
  26. local i, j = getIndices(pos)
  27. return not isPiece(board[i][j])
  28. end
  29.  
  30.  
  31. function getIndices(pos)
  32. local i = math.floor((pos-1)/3) + 1
  33. local j = (pos-1) % 3 + 1
  34. return i, j
  35. end
  36.  
  37.  
  38. function win()
  39. return (board[1][1] == board[1][2] and board[1][2] == board[1][3]) or
  40. (board[2][1] == board[2][2] and board[2][2] == board[2][3]) or
  41. (board[3][1] == board[3][2] and board[3][2] == board[3][3]) or
  42. (board[1][1] == board[2][1] and board[2][1] == board[3][1]) or
  43. (board[1][2] == board[2][2] and board[2][2] == board[3][2]) or
  44. (board[1][3] == board[2][3] and board[2][3] == board[3][3]) or
  45. (board[1][1] == board[2][2] and board[2][2] == board[3][3]) or
  46. (board[1][3] == board[2][2] and board[2][2] == board[3][1])
  47. end
  48.  
  49.  
  50. function getFreePositions(board)
  51. local ret = {}
  52.  
  53. for pos = 1, 9 do
  54. if isFree(board, pos) then
  55. table.insert(ret, pos)
  56. end
  57. end
  58.  
  59. return ret
  60. end
  61.  
  62.  
  63. function cpuPlay(piece)
  64. local freePlaces = getFreePositions(board)
  65. local index = math.random(1, #freePlaces)
  66. local pos = freePlaces[index]
  67. print(pos)
  68. for i = 1,3 do
  69. for j = 1,3 do
  70. if pos == board[i][j] then
  71. board[i][j] = piece
  72. end
  73. end
  74. end
  75. end
  76.  
  77.  
  78. function getGameResult(board)
  79. local ret = "c" -- continue
  80. if win() then
  81. ret = "w" -- Hรก um ganhador
  82. elseif #getFreePositions(board) == 0 then
  83. ret = "v" -- Deu velha
  84. end
  85. return ret
  86. end
  87.  
  88.  
  89. function printGameResult(result)
  90. local results =
  91. {
  92. w = "Ganhou",
  93. v = "Deu velha",
  94. }
  95. print(results[result])
  96.  
  97. end
  98.  
  99.  
  100. function printBoard(board)
  101. for i, line in ipairs(board) do
  102. print(line[1] .. "|" .. line[2] .. "|" .. line[3])
  103. end
  104. print("")
  105. end
  106.  
  107.  
  108. function runGame()
  109. printBoard(board)
  110.  
  111. local gameResult = "c"
  112. while true do
  113. print("Sua vez:")
  114. play("X")
  115. printBoard(board)
  116. gameResult = getGameResult(board)
  117. if gameResult ~= "c" then
  118. break
  119. end
  120.  
  121. print("CPU:")
  122. cpuPlay("O")
  123. printBoard(board)
  124. gameResult = getGameResult(board)
  125. if gameResult ~= "c" then
  126. break
  127. end
  128. end
  129.  
  130. printGameResult(gameResult)
  131. end
  132.  
  133.  
  134. runGame()
Add Comment
Please, Sign In to add comment