Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/main/java/de/cas/mse/exercise/diamond/Diamond.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@

public class Diamond {

public String print(int n) {
if (n <= 0 || n % 2 == 0) {
public String getDiamond(int diamondWidth) {
if (diamondWidth <= 0 || diamondWidth % 2 == 0) {
return null;
}
StringBuilder builder = new StringBuilder(new String(make(n, n)));
for (int i = n - 2; i > 0; i -= 2) {
char[] chars = make(n, i);
char[] centerLine = makeLine(diamondWidth, diamondWidth);
StringBuilder builder = new StringBuilder(new String(centerLine));

for (int oddLineIndex = diamondWidth - 2; oddLineIndex > 0; oddLineIndex -= 2) {
char[] chars = makeLine(diamondWidth, oddLineIndex);
builder.insert(0, chars);
builder.append(chars);
}
return builder.toString();
}

private char[] make(int i, int j) {
int amount = ((i - j) / 2);
char[] chars = new char[amount + j + 1];
if (amount > 0) {
Arrays.fill(chars, 0, amount, ' ');
}
Arrays.fill(chars, amount, amount + j, '*');
chars[chars.length - 1] = '\n';
private char[] makeLine(int diamondWidth, int stars) {
int spaces = ((diamondWidth - stars) / 2);
char[] chars = new char[spaces + stars + 1];

fillLine(chars, stars, spaces);
return chars;
}

private void fillLine(char[] chars, int stars, int spaces) {
Arrays.fill(chars, 0, spaces, ' ');
Arrays.fill(chars, spaces, spaces + stars, '*');
chars[chars.length - 1] = '\n';
}

}