# Help create "Snail Matrix" script

## Recommended Posts

Hello good afternoon, I need your help, I want to convert this code created in C to Lua, if someone can help me please.

```
#include <stdio.h>

//Variables globales
int nmatriz = 0;
void generarMatriz(int nmatriz);

//Funcion principal
int main(){
printf("Inserta el tamaño de la matriz\n");
scanf("%d",&nmatriz);
generarMatriz(nmatriz);
}

//Funcion que generara la matriz en caracol
void generarMatriz(nmatriz){
int i,j;
int countTotal = 1,tamanioTotal = nmatriz*nmatriz;
int fila = 0, check = nmatriz, band = 1;
int matrizCaracol[nmatriz][nmatriz];

//Llena matriz
while(countTotal<=tamanioTotal){
//Hacia la derecha
for(i=band-1; i < check; i++){
matrizCaracol[fila][i] = countTotal;
countTotal = countTotal++;
}
fila = nmatriz - band;
check = nmatriz - band;

//Hacia abajo
for(i=band; i <= check; i++){
matrizCaracol[i][fila] = countTotal;
countTotal = countTotal++;
}

//Hacia la izquierda
for(i=check-1; i >= (band-1) ; i--){
matrizCaracol[fila][i] = countTotal;
countTotal = countTotal++;
}

fila = band-1;

//Hacia arriba
for(i=check-1; i >= band ; i--){
matrizCaracol[i][fila] = countTotal;
countTotal = countTotal++;
}
band++;
fila = band-1;
}

//Imprimir matriz
for(i=0; i<nmatriz; i++){
for(j=0; j<nmatriz; j++){
printf("%d \t", matrizCaracol[i][j]);
}
printf("\n");
}
} ``` This is what the code has to do. sending to print on screen.

Edited by Narutimmy
• Scripting Moderators ```local sX, sY = guiGetScreenSize()
local unitW, unitH = 40, 40

local snakeSize = 0
local snakeTable = {}

function setupSnake(_snakeSize)
snakeSize = _snakeSize
for i = 0, _snakeSize-1 do
for j = 1, _snakeSize do
snakeTable[i*_snakeSize+j] = i*_snakeSize+j
end
end
end

function drawSnake()
local snakeW, snakeH = snakeSize*unitW, snakeSize*unitH
local snakeX, snakeY = (sX-snakeW)/2, (sY-snakeH)/2
local offsetX, offsetY = 0, 0
for i = 0, snakeSize-1 do
for j = 1, snakeSize do
dxDrawRectangle(snakeX+offsetX, snakeY+offsetY, unitW, unitH, tocolor(0, 0, 0, 170))
dxDrawText(snakeTable[i*snakeSize+j], snakeX+offsetX, snakeY+offsetY, snakeX+offsetX+unitW, snakeY+offsetY+unitH, tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center")
offsetX = offsetX + unitW
end
offsetY = offsetY + unitH
offsetX = 0
end
end

if not tonumber(_snakeSize) then
outputChatBox("You have to insert a number as the snakesize.")
else
setupSnake(_snakeSize)
end
end)```

Edited by NeXuS™

## Create an account

Register a new account