0% found this document useful (0 votes)
11 views3 pages

Echo Opo

This document contains code for generating a spiral matrix and printing it. The code defines a main method that calls other methods to generate a spiral matrix based on a size and print it.

Uploaded by

genekrupa2069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Echo Opo

This document contains code for generating a spiral matrix and printing it. The code defines a main method that calls other methods to generate a spiral matrix based on a size and print it.

Uploaded by

genekrupa2069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

public class Main {

public static final int ARRAY_LENGTH = 5;

public static void main(String[] args) {


int numberOfElements = (int) Math.pow(ARRAY_LENGTH, 2);
int[][] spiralArray = getSpiralArray(numberOfElements);
printIntegerArray(spiralArray);
}

public static int[][] getSpiralArray(int size) {


int squareSideSize = -1;
for (int i = 1; i * 2 < size; i++) {
if (Math.pow(i, 2) == size) {
squareSideSize = i;
}
}

if (squareSideSize == -1) throw new IllegalArgumentException("Size must be


a square of an integer");

int[][] matrix = new int[squareSideSize][squareSideSize];

int currentNumber = 1;
int top = 0;
int bottom = squareSideSize - 1;
int left = 0;
int right = squareSideSize - 1;

while (currentNumber <= size) {


for (int i = left; i <= right; i++) {
matrix[top][i] = currentNumber++;
}
top++;
for (int i = top; i <= bottom; i++) {
matrix[i][right] = currentNumber++;
}
right--;
for (int i = right; i >= left; i--) {
matrix[bottom][i] = currentNumber++;
}
bottom--;
for (int i = bottom; i >= top; i--) {
matrix[i][left] = currentNumber++;
}
left++;
}

return matrix;
}
function digIt()
while turtle.detect() do
turtle.dig()
os.sleep(0.5)
end
turtle.forward()
while turtle.detectUp() do
turtle.digUp()
end
end
local run = 0

term.write("Tunnel length: ")


run = read()

for i = 1, run do
digIt()
turtle.turnLeft()
digIt()
turtle.turnRight()
turtle.turnRight()
turtle.forward()
digIt()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end

-- Return to where we started


print("Returning to start...")
for i = 1, run do
turtle.back()
end

function placeTorch()
turtle.back()
turtle.turnLeft()
turtle.select(16)
turtle.place()
turtle.turnRight()
turtle.forward()
end

local run = 0
local j = 0
local k = 0

for i = 1, run do
k = i - 1
j = k % 7
if j == 1 then
placeTorch()
end
digIt()
end
public static void printIntegerArray(int[][] array) {
int maxElement = getArrayMaxElement(array);
int maxNumberLength = String.valueOf(maxElement).length();

for (int i = 0; i < array.length; i++) {


for (int j = 0; j < array[i].length; j++) {
System.out.printf("%" + maxNumberLength + "d ", array[i][j]);
}
System.out.println();
}
}
public static int getArrayMaxElement(int[][] array) {
int maxElement = 0;

for (int[] row : array) {


for (int element : row) {
maxElement = Math.max(maxElement, element);
}
}

return maxElement;
}
}

You might also like