Create a roblox lua module containing a method that will create a matrix of a given size…
- User
Create a roblox lua module containing a method that will create a matrix of a given size consisting of false, and also with a method that will draw line of trues inside from x1 y1 to x2 y2, and a method to display this matrix where falses will be 0 and trues will be 1
- Assistant
Here's a simple Lua module that can create a matrix of false values, draw lines of true values, and display the matrix:
local Matrix = {} -- Function to create a matrix of false values function Matrix.create(size) local matrix = {} for i = 1, size do matrix[i] = {} for j = 1, size do matrix[i][j] = false end end return matrix end -- Function to draw a line of true values function Matrix.drawLine(matrix, x1, y1, x2, y2) local xdiff = x2 - x1 local ydiff = y2 - y1 local dx = 0 local dy = 0 if xdiff > ydiff then dx = 1 dy = 0 else dx = 0 dy = 1 end local x = x1 local y = y1 for i = 1, xdiff do matrix[x][y] = true x = x + dx y = y + dy end end -- Function to display the matrix function Matrix.display(matrix, size) local displayMatrix = {} for i = 1, size do local row = {} for j = 1, size do if matrix[i][j] then table.insert(row, 1) else table.insert(row, 0) end end table.insert(displayMatrix, row) end return displayMatrix end return MatrixTo use this module in your Roblox game, make sure you have a StarterGui, and then include the code above in the StarterGui's script. You can then use the functions from the Matrix module as follows:
local Matrix = require(StarterGui.Modules.Matrix) local matrixSize = 10 local matrix = Matrix.create(matrixSize) -- Draw a line from (1, 1) to (9, 9) Matrix.drawLine(matrix, 1, 1, 9, 9) -- Display the matrix local displayMatrix = Matrix.display(matrix, matrixSize) -- Print the displayed matrix for i, row in ipairs(displayMatrix) do for j, value in ipairs(row) do print(value) end endThis example creates a 10x10 matrix, draws a line from (1, 1) to (9, 9), and then displays and prints the matrix. Falses are represented by 0, and trues are represented by 1.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.