Jump to content

Help create "Snail Matrix" script


Narutimmy

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"); 
    } 
} 

I have already made several attempts but I get it, help please.

 

 

 

Resultado de imagen para matriz caracol

 

 

This is what the code has to do. sending to print on screen.

Edited by Narutimmy
Link to comment

_4QWpBhaQqeysb5gJGHZyw.png

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
	addEventHandler("onClientRender", root, drawSnake)
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

addCommandHandler("snake", function(_, _snakeSize)
	if not tonumber(_snakeSize) then
		outputChatBox("You have to insert a number as the snakesize.")
	else
		setupSnake(_snakeSize)
	end
end)

 

Edited by NeXuS™
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...