-
Notifications
You must be signed in to change notification settings - Fork 94
/
Logic.java
115 lines (106 loc) · 3.81 KB
/
Logic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package mooc.vandy.java4android.diamonds.logic;
import android.util.Log;
import mooc.vandy.java4android.diamonds.ui.OutputInterface;
/**
* This is where the logic of this App is centralized for this assignment.
* <p>
* The assignments are designed this way to simplify your early
* Android interactions. Designing the assignments this way allows
* you to first learn key 'Java' features without having to beforehand
* learn the complexities of Android.
*/
public class Logic
implements LogicInterface {
/**
* This is a String to be used in Logging (if/when you decide you
* need it for debugging).
*/
public static final String TAG = Logic.class.getName();
/**
* This is the variable that stores our OutputInterface instance.
* <p>
* This is how we will interact with the User Interface [MainActivity.java].
* <p>
* It is called 'out' because it is where we 'out-put' our
* results. (It is also the 'in-put' from where we get values
* from, but it only needs 1 name, and 'out' is good enough).
*/
private OutputInterface mOut;
/**
* This is the constructor of this class.
* <p>
* It assigns the passed in [MainActivity] instance (which
* implements [OutputInterface]) to 'out'.
*/
public Logic(OutputInterface out){
mOut = out;
}
/**
* This is the method that will (eventually) get called when the
* on-screen button labeled 'Process...' is pressed.
*/
public void process(int size) {
int height = size * 2 + 1;
int width = size * 2 + 2;
int accumulator = -(size+1);
for(int i=1;i<=height;i++){
accumulator++;
for (int j=1;j<=width;j++) {
if((i == 1 || i == height) && (j == 1 || j == width))
mOut.print("+");
else if((i == 1 || i == height) && !(j == 1 || j == width))
mOut.print("-");
else if(!(i == 1 || i == height) && (j == 1 || j == width))
mOut.print("|");
else {
drawDiamond(size, i, j, accumulator);
}
}
mOut.print("\n");
}
}
public void drawDiamond(int size, int i, int j, int accumulator){
int diamondRowThickness;
if (accumulator <= 0){
diamondRowThickness = i*2-2;
} else {
diamondRowThickness = (i-accumulator*2)*2-2;
}
int diamondMidpoint = size + 1;
int diamondBoundsLeft = diamondMidpoint - (diamondRowThickness/2-1);
int diamondBoundsRight = diamondMidpoint + (diamondRowThickness/2);
int frameTop = 1;
int frameBottom = size * 2 + 1;
if (j >= diamondBoundsLeft && j <= diamondBoundsRight) {
if (j == diamondBoundsLeft || j == diamondBoundsRight) {
if (i < diamondMidpoint && i > frameTop) {
if (j == diamondBoundsLeft) {
mOut.print("/");
} else {
mOut.print("\\");
}
} else if (i == diamondMidpoint) {
if (j == diamondBoundsLeft) {
mOut.print("<");
} else {
mOut.print(">");
}
} else if (i > diamondMidpoint && i < frameBottom) {
if (j == diamondBoundsLeft) {
mOut.print("\\");
} else {
mOut.print("/");
}
}
} else {
if (i % 2 == 0) {
mOut.print("=");
} else {
mOut.print("-");
}
}
} else {
mOut.print(" ");
}
}
}