// TicTacToe Class located in ServerStorage
local Prototype = {}
Prototype._index = Prototype
Prototype.currentPlayerMark = 'x';
function Prototype.new()
local O = {}
O.currentPlayerMark = 'x'
O.board = {}
for i = 1,3 do
O.board[i] = {}
for j = 1,3 do
O.board[i][j] = '_'
end
end
setmetatable(O, Prototype)
return O
end
function Prototype.initializeBoard(self)
for i = 1,3 do
for j = 1,3 do
self.Board[i][j] = '_'
end
end
end
function Prototype.checkRowCol(c1, c2, c3)
return c1 ~= '_' and c1 == c2 and c2 == c3
end
function Prototype.checkRowsForWin(self)
for i = 1,3 do
if Prototype.checkRowCol(self.Board[i][1], self.Board[i][2], self.Board[i][3]) == true then
return true;
end
end
return false;
end
function Prototype.checkColForWin(self)
for i = 1,3 do
if Prototype.checkRowCol(self.Board[1][i], self.Board[2][i], self.Board[3][i]) == true then
return true;
end
end
return false;
end
function Prototype.checkDiagonalsForWin(self)
return Prototype.checkRowCol(self.board[1][1], self.board[2][2], self.board[3][3]) == true or Prototype.checkRowCol(self.board[1][3], self.board[2][2], self.board[3][1]) == true
end
function Prototype.checkForWin(self)
return self.checkRowsForWin() or self.checkColForWin() or self.checkDiagonalsForWin()
end
function Prototype.isBoardFull(self)
local isFull = true;
for i = 1,3 do
for j = 1,3 do
if self.board[i][j] == '_' then
isFull = false
end
end
end
return isFull;
end
function Prototype.changePlayer(self)
if self.currentPlayerMark == 'x' then
self.currentPlayerMark = 'o'
else
self.currentPlayerMark = 'x';
end
end
return Prototype
//Main Class located in ServerScriptService
local prototype = require(game.ServerStorage["TTT Class"])
local instance = prototype.new()
local function loopGame()
repeat
prototype.changePlayer(prototype)
until prototype.checkForWin() or prototype.isFull()
end
loopGame()
I’m trying to create a loop that iterates over the two-player each turn, and the loop will return once there is a win or if the board is full. I’m stuck. I’m thinking of another solution where the client sends a remote event to the server when they click a box, the server checks if the player is the current player in turn, and then proceed to change the matrix representing the board accordingly, which in turn changes the TicTacToe Gui. The server then changes the current player in turn so the other player can play.