-- Compiled with roblox-ts v1.2.7 local TS = _G[script] local TicTacToeState = TS.import(script, script.Parent, "enum", "TicTacToeState").TicTacToeState local TicTacToeSymbol = TS.import(script, script.Parent, "enum", "TicTacToeSymbol").TicTacToeSymbol local TicTacToeCell = TS.import(script, script.Parent, "TicTacToeCell").TicTacToeCell --[[ * * The representation of a TicTacToe board. ]] local TicTacToeBoard do TicTacToeBoard = setmetatable({}, { __tostring = function() return "TicTacToeBoard" end, }) TicTacToeBoard.__index = TicTacToeBoard function TicTacToeBoard.new(...) local self = setmetatable({}, TicTacToeBoard) return self:constructor(...) or self end function TicTacToeBoard:constructor(board) local _result if board then local _arg0 = function(cell) return TicTacToeCell.new(cell:getRow(), cell:getCol(), cell:getSymbol()) end -- ▼ ReadonlyArray.map ▼ local _newValue = table.create(#board) for _k, _v in ipairs(board) do _newValue[_k] = _arg0(_v, _k - 1, board) end -- ▲ ReadonlyArray.map ▲ _result = _newValue else _result = TicTacToeBoard:empty() end self.board = _result end function TicTacToeBoard:getCell(row, col) local _board = self.board local _arg0 = function(cell) return cell:getRow() == row and cell:getCol() == col end -- ▼ ReadonlyArray.find ▼ local _result = nil for _i, _v in ipairs(_board) do if _arg0(_v, _i - 1, _board) == true then _result = _v break end end -- ▲ ReadonlyArray.find ▲ return _result end function TicTacToeBoard:getRow(row) local _board = self.board local _arg0 = function(cell) return cell:getRow() == row end -- ▼ ReadonlyArray.filter ▼ local _newValue = {} local _length = 0 for _k, _v in ipairs(_board) do if _arg0(_v, _k - 1, _board) == true then _length += 1 _newValue[_length] = _v end end -- ▲ ReadonlyArray.filter ▲ return _newValue end function TicTacToeBoard:getCol(col) local _board = self.board local _arg0 = function(cell) return cell:getCol() == col end -- ▼ ReadonlyArray.filter ▼ local _newValue = {} local _length = 0 for _k, _v in ipairs(_board) do if _arg0(_v, _k - 1, _board) == true then _length += 1 _newValue[_length] = _v end end -- ▲ ReadonlyArray.filter ▲ return _newValue end function TicTacToeBoard:getFirstDiagonal() local _board = self.board local _arg0 = function(cell) return cell:getRow() == cell:getCol() end -- ▼ ReadonlyArray.filter ▼ local _newValue = {} local _length = 0 for _k, _v in ipairs(_board) do if _arg0(_v, _k - 1, _board) == true then _length += 1 _newValue[_length] = _v end end -- ▲ ReadonlyArray.filter ▲ return _newValue end function TicTacToeBoard:getSecondDiagonal() local _board = self.board local _arg0 = function(cell) return cell:getRow() == 2 - cell:getCol() end -- ▼ ReadonlyArray.filter ▼ local _newValue = {} local _length = 0 for _k, _v in ipairs(_board) do if _arg0(_v, _k - 1, _board) == true then _length += 1 _newValue[_length] = _v end end -- ▲ ReadonlyArray.filter ▲ return _newValue end function TicTacToeBoard:setCell(cell) local _board = self.board local _arg0 = function(c) return c:getRow() == cell:getRow() and c:getCol() == cell:getCol() end -- ▼ ReadonlyArray.findIndex ▼ local _result = -1 for _i, _v in ipairs(_board) do if _arg0(_v, _i - 1, _board) == true then _result = _i - 1 break end end -- ▲ ReadonlyArray.findIndex ▲ local index = _result self.board[index + 1] = cell end function TicTacToeBoard:getCurrentState() if self:isEmpty() then return { TicTacToeState.IN_PROGRESS, nil } end local lines = { self:getRow(0), self:getRow(1), self:getRow(2), self:getCol(0), self:getCol(1), self:getCol(2), self:getFirstDiagonal(), self:getSecondDiagonal() } for _, cells in ipairs(lines) do local symbol = cells[1]:getSymbol() if symbol == TicTacToeSymbol.EMPTY then continue end local _arg0 = function(cell) return cell:getSymbol() == symbol end -- ▼ ReadonlyArray.every ▼ local _result = true for _k, _v in ipairs(cells) do if not _arg0(_v, _k - 1, cells) then _result = false break end end -- ▲ ReadonlyArray.every ▲ if not _result then continue end if symbol == TicTacToeSymbol.X then return { TicTacToeState.X_WON, cells } end if symbol == TicTacToeSymbol.O then return { TicTacToeState.O_WON, cells } end end if self:isFull() then return { TicTacToeState.DRAW, nil } end return { TicTacToeState.IN_PROGRESS, nil } end function TicTacToeBoard:getEmptyCells() local _board = self.board local _arg0 = function(cell) return cell:isEmpty() end -- ▼ ReadonlyArray.filter ▼ local _newValue = {} local _length = 0 for _k, _v in ipairs(_board) do if _arg0(_v, _k - 1, _board) == true then _length += 1 _newValue[_length] = _v end end -- ▲ ReadonlyArray.filter ▲ return _newValue end function TicTacToeBoard:isEmpty() local _board = self.board local _arg0 = function(cell) return cell:isEmpty() end -- ▼ ReadonlyArray.every ▼ local _result = true for _k, _v in ipairs(_board) do if not _arg0(_v, _k - 1, _board) then _result = false break end end -- ▲ ReadonlyArray.every ▲ return _result end function TicTacToeBoard:isFull() local _board = self.board local _arg0 = function(cell) return not cell:isEmpty() end -- ▼ ReadonlyArray.every ▼ local _result = true for _k, _v in ipairs(_board) do if not _arg0(_v, _k - 1, _board) then _result = false break end end -- ▲ ReadonlyArray.every ▲ return _result end function TicTacToeBoard:isValidMove(move) local _result = self:getCell(move:getRow(), move:getCol()) if _result ~= nil then _result = _result:isEmpty() end local _condition = _result if _condition == nil then _condition = false end return _condition end function TicTacToeBoard:getCells() return self.board end function TicTacToeBoard:empty() return { TicTacToeCell:empty(0, 0), TicTacToeCell:empty(0, 1), TicTacToeCell:empty(0, 2), TicTacToeCell:empty(1, 0), TicTacToeCell:empty(1, 1), TicTacToeCell:empty(1, 2), TicTacToeCell:empty(2, 0), TicTacToeCell:empty(2, 1), TicTacToeCell:empty(2, 2) } end end return { TicTacToeBoard = TicTacToeBoard, }