RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"lets play tictactoe"
-- server script for a tic-tac-toe game local Players = game:GetService("Players") local Board = {} local CurrentPlayer = Players[1] local GameOver = false -- initialize the board for i = 1, 3 do Board[i] = {} for j = 1, 3 do Board[i][j] = " " end end -- function to check if a player has won local function CheckWin(player) -- check horizontal rows for i = 1, 3 do if Board[i][1] == player and Board[i][2] == player and Board[i][3] == player then return true end end -- check vertical columns for j = 1, 3 do if Board[1][j] == player and Board[2][j] == player and Board[3][j] == player then return true end end -- check diagonals if Board[1][1] == player and Board[2][2] == player and Board[3][3] == player then return true end if Board[1][3] == player and Board[2][2] == player and Board[3][1] == player then return true end return false end -- function to check if the board is full local function CheckTie() for i = 1, 3 do for j = 1, 3 do if Board[i][j] == " " then return false end end end return true end -- function to update the board and check for win/tie local function UpdateBoard(row, col, player) if Board[row][col] == " " then Board[row][col] = player if CheckWin(player) then GameOver = true print(player.Name .. " wins!") elseif CheckTie() then GameOver = true print("Tie game!") else CurrentPlayer = Players[(Players:GetRankInGroup(player.UserId) % #Players) + 1] print("It's " .. CurrentPlayer.Name .. "'s turn.") end end end -- function to handle player input local function HandleInput(player, row, col) if player == CurrentPlayer and not GameOver then UpdateBoard(row, col, player.Name) end end -- connect player input to HandleInput function for i = 1, 3 do for j = 1, 3 do local part = game.Workspace.Board:FindFirstChild(tostring(i) .. tostring(j)) part.Touched:Connect(function(otherPart) local player = game.Players:GetPlayerFromCharacter(otherPart.Parent) HandleInput(player, i, j) end) end end -- start the game print("It's " .. CurrentPlayer.Name .. "'s turn.")