Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants
- local player1 = 1
- local player2 = 4
- -- Variables
- -- The playing board
- local gameBoard = {
- [0] = {[0] = 0, [1] = 0, [2] = 0 },
- [1] = {[0] = 0, [1] = 0, [2] = 0 },
- [2] = {[0] = 0, [1] = 0, [2] = 0 }
- }
- -- Table for variable game information
- local gameData = {
- cursorPos = {
- X = 1,
- Y = 1
- },
- enter = false,
- playerTurn = player1,
- winner = 0,
- turnCounter = 0
- }
- -- Enumeration for valid directional input
- local validKeys = { [200] = true, [203] = true, [205] = true, [208] = true }
- -- Table to store what the different keys do.
- -- dirname corresponds to the respective arrow key
- local keyboard = {
- [200] = { var = -1, axis = "Y", dirname = "Up" },
- [203] = { var = -1, axis = "X", dirname = "Left"},
- [205] = { var = 1, axis = "X", dirname = "Right"},
- [208] = { var = 1, axis = "Y", dirname = "Down"}
- }
- -- Function to make the cursor wrap around the playing field
- local function updateGameCursor(key)
- local keyTable = keyboard[key]
- gameData.cursorPos[keyTable.axis] = math.fmod(3 + gameData.cursorPos[keyTable.axis] + keyTable.var, 3)
- end
- -- Rendering functions
- local render = {}
- function render.cell(X, Y)
- -- Check if the current cell is the selected one
- -- and make it green if true
- if X == gameData.cursorPos.X and Y == gameData.cursorPos.Y then
- term.setTextColor(colours.green)
- end
- if gameBoard[Y][X] == 0 then
- io.write("[ ]")
- elseif gameBoard[Y][X] == 1 then
- io.write("[X]")
- elseif gameBoard[Y][X] == 4 then
- io.write("[O]")
- end
- term.setTextColor(colours.white)
- end
- function render.line(Y)
- for X = 0, #gameBoard[Y] do
- render.cell(X, Y)
- end
- end
- function render.board()
- term.clear()
- term.setCursorPos(1,1)
- for Y = 0, #gameBoard do
- render.line(Y)
- if Y == 0 then
- io.write("\t Player: ")
- if gameData.playerTurn == 1 then
- io.write("X")
- else
- io.write("O")
- end
- elseif Y == 1 then
- io.write("\t Turn number: "..gameData.turnCounter)
- end
- io.write("\n")
- end
- end
- setmetatable(render, { __call = function(t, ...) return render.board() end })
- -- Program loop
- while gameData.winner == 0 do
- -- Helper function
- local function selectedCell()
- return gameBoard[gameData.cursorPos.Y][gameData.cursorPos.X]
- end
- -- Render the screen.
- render()
- -- Check for input after rendering.
- local event, param1, param2, param3 = os.pullEventRaw()
- -- Parse input
- if event == "key" then
- if validKeys[param1] then
- updateGameCursor(param1)
- end
- if param1 == 28 then
- gameData.enter = true
- end
- end
- -- If enter has been pressed and the cell is empty
- -- change the content of the cell to the player value
- if gameData.enter and selectedCell() == 0 then
- gameBoard[gameData.cursorPos.Y][gameData.cursorPos.X] = gameData.playerTurn
- else
- gameData.enter = false
- end
- -- Check if somebody won
- local winConditions = {
- -- Horizontals
- [1] = gameBoard[0][0] + gameBoard[0][1] + gameBoard[0][2],
- [2] = gameBoard[1][0] + gameBoard[1][1] + gameBoard[1][2],
- [3] = gameBoard[2][0] + gameBoard[2][1] + gameBoard[2][2],
- -- Diagonals
- [4] = gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2],
- [5] = gameBoard[2][0] + gameBoard[1][1] + gameBoard[0][2],
- -- Verticals
- [6] = gameBoard[0][0] + gameBoard[1][0] + gameBoard[2][0],
- [7] = gameBoard[0][1] + gameBoard[1][1] + gameBoard[2][1],
- [8] = gameBoard[0][2] + gameBoard[1][2] + gameBoard[2][2]
- }
- for k, v in ipairs(winConditions) do
- if v == (gameData.playerTurn * 3) then
- gameData.winner = gameData.playerTurn
- end
- end
- -- Change the player turn if a cell has been selected
- if gameData.enter and gameData.winner == 0 then
- if gameData.playerTurn == player1 then
- gameData.playerTurn = player2
- else
- gameData.playerTurn = player1
- end
- gameData.turnCounter = gameData.turnCounter + 1
- end
- gameData.enter = false
- -- If the board is full and there is no winner, end the game
- if gameData.turnCounter == 9 and gameData.winner == 0 then
- break
- end
- end
- -- Render one last time to show the final outcome
- render()
- -- Print who the winner is
- if gameData.turnCounter == 9 then
- print("Tie")
- else
- print("Winner is player "..gameData.winner.." after "..gameData.turnCounter.." turns!")
- end
Advertisement
Add Comment
Please, Sign In to add comment