Guest User

TicTacToe by Brekkjern

a guest
Aug 9th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | None | 0 0
  1. -- Constants
  2. local player1 = 1
  3. local player2 = 4
  4.  
  5. -- Variables
  6.  
  7. -- The playing board
  8. local gameBoard = {
  9.   [0] = {[0] = 0, [1] = 0, [2] = 0 },
  10.   [1] = {[0] = 0, [1] = 0, [2] = 0 },
  11.   [2] = {[0] = 0, [1] = 0, [2] = 0 }
  12. }
  13.  
  14. -- Table for variable game information
  15. local gameData = {
  16.     cursorPos = {
  17.       X = 1,
  18.       Y = 1
  19.     },
  20.     enter = false,
  21.     playerTurn = player1,
  22.     winner = 0,
  23.     turnCounter = 0
  24. }
  25.  
  26. -- Enumeration for valid directional input
  27. local validKeys = { [200] = true, [203] = true, [205] = true, [208] = true }
  28.  
  29. -- Table to store what the different keys do.
  30. -- dirname corresponds to the respective arrow key
  31. local keyboard = {
  32.   [200] = { var = -1, axis = "Y", dirname = "Up" },
  33.   [203] = { var = -1, axis = "X", dirname = "Left"},
  34.   [205] = { var = 1, axis = "X", dirname = "Right"},
  35.   [208] = { var = 1, axis = "Y", dirname = "Down"}
  36. }
  37.  
  38. -- Function to make the cursor wrap around the playing field
  39. local function updateGameCursor(key)
  40.   local keyTable = keyboard[key]
  41.   gameData.cursorPos[keyTable.axis] = math.fmod(3 + gameData.cursorPos[keyTable.axis] + keyTable.var, 3)
  42. end
  43.  
  44. -- Rendering functions
  45. local render = {}
  46. function render.cell(X, Y)
  47.  
  48.   -- Check if the current cell is the selected one
  49.   -- and make it green if true
  50.   if X == gameData.cursorPos.X and Y == gameData.cursorPos.Y then
  51.     term.setTextColor(colours.green)
  52.   end
  53.  
  54.   if gameBoard[Y][X] == 0 then
  55.     io.write("[ ]")
  56.   elseif gameBoard[Y][X] == 1 then
  57.     io.write("[X]")
  58.   elseif gameBoard[Y][X] == 4 then
  59.     io.write("[O]")
  60.   end
  61.   term.setTextColor(colours.white)
  62. end
  63.  
  64. function render.line(Y)
  65.   for X = 0, #gameBoard[Y] do
  66.     render.cell(X, Y)
  67.   end
  68. end
  69.  
  70. function render.board()
  71.   term.clear()
  72.   term.setCursorPos(1,1)
  73.   for Y = 0, #gameBoard do
  74.     render.line(Y)
  75.  
  76.     if Y == 0 then
  77.       io.write("\t Player: ")
  78.       if gameData.playerTurn == 1 then
  79.         io.write("X")
  80.       else
  81.         io.write("O")
  82.       end
  83.     elseif Y == 1 then
  84.       io.write("\t Turn number: "..gameData.turnCounter)
  85.     end
  86.     io.write("\n")
  87.   end
  88. end
  89.  
  90. setmetatable(render, { __call = function(t, ...) return render.board() end })
  91.  
  92. -- Program loop
  93. while gameData.winner == 0 do
  94.  
  95.   -- Helper function
  96.   local function selectedCell()
  97.     return gameBoard[gameData.cursorPos.Y][gameData.cursorPos.X]
  98.   end
  99.  
  100.   -- Render the screen.
  101.   render()
  102.  
  103.   -- Check for input after rendering.
  104.   local event, param1, param2, param3 = os.pullEventRaw()
  105.  
  106.   -- Parse input
  107.   if event == "key" then
  108.     if validKeys[param1] then
  109.       updateGameCursor(param1)
  110.     end
  111.     if param1 == 28 then
  112.       gameData.enter = true
  113.     end
  114.   end
  115.  
  116.   -- If enter has been pressed and the cell is empty
  117.   -- change the content of the cell to the player value
  118.   if gameData.enter and selectedCell() == 0 then
  119.     gameBoard[gameData.cursorPos.Y][gameData.cursorPos.X] = gameData.playerTurn
  120.   else
  121.     gameData.enter = false
  122.   end
  123.  
  124.   -- Check if somebody won
  125.   local winConditions = {
  126.   -- Horizontals
  127.   [1] = gameBoard[0][0] + gameBoard[0][1] + gameBoard[0][2],
  128.   [2] = gameBoard[1][0] + gameBoard[1][1] + gameBoard[1][2],
  129.   [3] = gameBoard[2][0] + gameBoard[2][1] + gameBoard[2][2],
  130.  
  131.   -- Diagonals
  132.   [4] = gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2],
  133.   [5] = gameBoard[2][0] + gameBoard[1][1] + gameBoard[0][2],
  134.  
  135.   -- Verticals
  136.   [6] = gameBoard[0][0] + gameBoard[1][0] + gameBoard[2][0],
  137.   [7] = gameBoard[0][1] + gameBoard[1][1] + gameBoard[2][1],
  138.   [8] = gameBoard[0][2] + gameBoard[1][2] + gameBoard[2][2]
  139.   }
  140.  
  141.   for k, v in ipairs(winConditions) do
  142.     if v == (gameData.playerTurn * 3) then
  143.       gameData.winner = gameData.playerTurn
  144.     end
  145.   end
  146.  
  147.   -- Change the player turn if a cell has been selected
  148.   if gameData.enter and gameData.winner == 0 then
  149.     if gameData.playerTurn == player1 then
  150.       gameData.playerTurn = player2
  151.     else
  152.       gameData.playerTurn = player1
  153.     end
  154.     gameData.turnCounter = gameData.turnCounter + 1
  155.   end
  156.   gameData.enter = false
  157.  
  158.   -- If the board is full and there is no winner, end the game
  159.   if gameData.turnCounter == 9 and gameData.winner == 0 then
  160.     break
  161.   end
  162. end
  163.  
  164. -- Render one last time to show the final outcome
  165. render()
  166.  
  167. -- Print who the winner is
  168. if gameData.turnCounter == 9 then
  169.   print("Tie")
  170. else
  171.   print("Winner is player "..gameData.winner.." after "..gameData.turnCounter.." turns!")
  172. end
Advertisement
Add Comment
Please, Sign In to add comment