-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
somoarn
authored and
somoarn
committed
Jun 30, 2020
0 parents
commit a9f64be
Showing
8 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Spiral | ||
|
||
It is a simple Java project to print a string that is given in rows and column just like a matrix. | ||
|
||
The matrix fill in process is controlled by a dictionary. | ||
|
||
**Frame spiral dictionary:** | ||
~~~~ | ||
a b c d e f | ||
t g | ||
s h | ||
r i | ||
q j | ||
p o n m l k | ||
~~~~ | ||
|
||
**Clock wise spiral dictionary:** | ||
~~~~ | ||
a b c d e f | ||
t u v w x g | ||
s 6 7 8 y h | ||
r 5 0 9 z i | ||
q 4 3 2 1 j | ||
p o n m l k | ||
~~~~ | ||
|
||
**Snake spiral dictionary:** | ||
~~~~ | ||
a b c d e f | ||
l k j i h g | ||
m n o p q r | ||
x w v u t s | ||
y z 1 2 3 4 | ||
0 9 8 7 6 5 | ||
~~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# project files | ||
*.class | ||
target/** | ||
|
||
# intellij idea specific | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea | ||
|
||
# eclipse specific | ||
**/.classpath | ||
**/.jupiter | ||
**/.project | ||
**/.settings/** | ||
|
||
# CVS | ||
**/.cvsignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.remal.spiral; | ||
|
||
import com.remal.spiral.dictionary.ClockWiseDictionary; | ||
import com.remal.spiral.dictionary.Dictionary; | ||
import com.remal.spiral.dictionary.FrameDictionary; | ||
import com.remal.spiral.dictionary.SnakeDictionary; | ||
import com.remal.spiral.spiral.Spiral; | ||
|
||
public class Test { | ||
private static final int MATRIX_SIZE = 6; | ||
|
||
public static void main(String[] args) { | ||
String sentence = "abcdefghijklmnopqrstuvwxyz1234567890"; | ||
String[][] spiral = new String[MATRIX_SIZE][MATRIX_SIZE]; | ||
|
||
System.out.println("frame spiral:"); | ||
Dictionary dictionary = new FrameDictionary(MATRIX_SIZE); | ||
String[] pattern = dictionary.createPattern(); | ||
Spiral.fill(sentence, pattern, spiral); | ||
Spiral.show(spiral); | ||
|
||
System.out.println("clock-wise spiral:"); | ||
dictionary = new ClockWiseDictionary(MATRIX_SIZE); | ||
pattern = dictionary.createPattern(); | ||
Spiral.fill(sentence, pattern, spiral); | ||
Spiral.show(spiral); | ||
|
||
System.out.println("snake spiral:"); | ||
dictionary = new SnakeDictionary(MATRIX_SIZE); | ||
pattern = dictionary.createPattern(); | ||
Spiral.fill(sentence, pattern, spiral); | ||
Spiral.show(spiral); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.remal.spiral.dictionary; | ||
|
||
public class ClockWiseDictionary extends FrameDictionary { | ||
|
||
public ClockWiseDictionary(int matrixSize) { | ||
super(matrixSize); | ||
} | ||
|
||
@Override | ||
public String[] createPattern() { | ||
while (dictionaryIndex < matrixSize * matrixSize) { | ||
pattern1(); | ||
pattern2(); | ||
} | ||
return dictionary; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.remal.spiral.dictionary; | ||
|
||
import java.util.Arrays; | ||
|
||
public abstract class Dictionary { | ||
|
||
protected int matrixSize; | ||
protected String[] dictionary; | ||
|
||
public Dictionary(int matrixSize) { | ||
this.matrixSize = matrixSize; | ||
dictionary = new String[matrixSize * matrixSize]; | ||
} | ||
|
||
public abstract String[] createPattern(); | ||
|
||
public void showPattern() { | ||
Arrays.stream(dictionary).forEach(System.out::println); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.remal.spiral.dictionary; | ||
|
||
public class FrameDictionary extends Dictionary { | ||
|
||
protected int dictionaryIndex = 0; | ||
protected int startX, endX; | ||
protected int startY, endY; | ||
|
||
public FrameDictionary(int matrixSize) { | ||
super(matrixSize); | ||
startX = -1; | ||
endX = matrixSize - 1; | ||
startY = 0; | ||
endY = matrixSize - 1; | ||
} | ||
|
||
@Override | ||
public String[] createPattern() { | ||
while (dictionaryIndex < matrixSize) { | ||
pattern1(); | ||
pattern2(); | ||
} | ||
return dictionary; | ||
} | ||
|
||
/** | ||
* pattern 1 | ||
* direction: left to right then top to bottom | ||
*/ | ||
protected void pattern1() { | ||
startX++; | ||
for (int i = startX; i <= endX; i++) { | ||
dictionary[dictionaryIndex] = i + ":" + startY; | ||
dictionaryIndex++; | ||
} | ||
|
||
startY++; | ||
for (int i = startY; i <= endY; i++) { | ||
dictionary[dictionaryIndex] = endX + ":" + i; | ||
dictionaryIndex++; | ||
} | ||
} | ||
|
||
/** | ||
* pattern 2 | ||
* direction: top to bottom then right to left | ||
*/ | ||
protected void pattern2() { | ||
endX--; | ||
for (int i = endX; i >= startX; i--) { | ||
dictionary[dictionaryIndex] = i + ":" + endY; | ||
dictionaryIndex++; | ||
} | ||
|
||
endY--; | ||
for (int i = endY; i >= startY; i--) { | ||
dictionary[dictionaryIndex] = startX + ":" + i; | ||
dictionaryIndex++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.remal.spiral.dictionary; | ||
|
||
public class SnakeDictionary extends Dictionary { | ||
|
||
private int dictionaryIndex = 0; | ||
private int startY = 0; | ||
|
||
public SnakeDictionary(int matrixSize) { | ||
super(matrixSize); | ||
} | ||
|
||
@Override | ||
public String[] createPattern() { | ||
while (dictionaryIndex < matrixSize * matrixSize) { | ||
pattern1(); | ||
if (dictionaryIndex < matrixSize * matrixSize) { | ||
pattern2(); | ||
} | ||
} | ||
|
||
return dictionary; | ||
} | ||
|
||
public void pattern1() { | ||
for (int i = 0; i < matrixSize; i++) { | ||
dictionary[dictionaryIndex] = i + ":" + startY; | ||
dictionaryIndex++; | ||
} | ||
startY++; | ||
} | ||
|
||
public void pattern2() { | ||
for (int i = matrixSize - 1; i >= 0; i--) { | ||
dictionary[dictionaryIndex] = i + ":" + startY; | ||
dictionaryIndex++; | ||
} | ||
startY++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.remal.spiral.spiral; | ||
|
||
public class Spiral { | ||
public static void fill(String sentence, String[] pattern, String[][] matrix) { | ||
int i = 0; | ||
for (String coordinates : pattern) { | ||
if (coordinates != null) { | ||
String[] parts = coordinates.split(":"); | ||
int x = Integer.parseInt(parts[1]); | ||
int y = Integer.parseInt(parts[0]); | ||
matrix[x][y] = (i < sentence.length()) ? sentence.substring(i, i + 1) : " "; | ||
i++; | ||
} | ||
} | ||
} | ||
|
||
public static void show(String[][] matrix) { | ||
for (String[] rows : matrix) { | ||
for (String row : rows) { | ||
System.out.print(row ==null ? " " : row + " "); | ||
} | ||
System.out.print("\n"); | ||
} | ||
} | ||
} |