-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
216 lines (185 loc) · 6.46 KB
/
Main.java
File metadata and controls
216 lines (185 loc) · 6.46 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Field myField = new Field(40,20,'.');
Character myCharacter = new Character(8,'X');
myField.makeField();
myField.addCharacter(myCharacter, 0, 0);
myField.renderField();
myField.moveCharacterAround();
}
}
class Field{
private int width;
private int length;
private char fieldChar;
private int fieldArea;
private char[] field;
private int characterStartingPosition;
private int[] characterSymbolPositions;
Character character;
Scanner myScanner;
public Field(int width, int length, char fieldChar){
this.width = width;
this.length = length;
this.fieldChar = fieldChar;
}
public void makeField(){
this.fieldArea = width * length;
this.field = new char[fieldArea];
for(int i = 0; i < fieldArea; i++) field[i] = fieldChar;
}
public void renderField(){
String stringField = "";
for(int i = 0; i < fieldArea; i++){
if (i%width == 0) stringField+="\n";
stringField+=field[i];
}
System.out.println(stringField);
}
public void addCharacter(Character character, int x, int y){
if (character.getSymbol() == this.fieldChar){
System.out.println("Error, the character is made up of the same character as the field.");
return;
}
this.character = character;
char characterSymbol = character.getSymbol();
int characterLength = character.getLength();
this.characterStartingPosition = x + y*this.width;
int startingPosition = this.characterStartingPosition;
this.characterSymbolPositions = new int [characterLength];
for(int i = 0; i < characterLength; i++){
this.field[startingPosition] = characterSymbol;
this.characterSymbolPositions[i] = startingPosition;
startingPosition+=this.width;
}
}
public char processUserInput(char direction){
char capitalizedDirection;
int asciiDirection = (int)direction;
if(asciiDirection >= 97) capitalizedDirection = (char)(asciiDirection-32);
else capitalizedDirection = direction;
return capitalizedDirection;
}
public void moveCharacter(char direction){
if(direction == 'W'){
this.newCharacterPositionsUp(characterSymbolPositions);
}
}
public void closeField(){
myScanner.close();
}
private int[] fieldBorder(){
//An array containing the index values that symbol the border of the field
return new int[2];
}
public char getUserInput(){
myScanner = new Scanner(System.in);
System.out.println("Select a direction: W (up), S (down), A (left), D (right): ");
char input = myScanner.nextLine().charAt(0);
return this.processUserInput(input);
}
public void moveCharacterAround(){
//Make it so that the previous character symbol has to move into the position of the current character symbol, calculate the new character symbol position and move each symbol from its previous position into the new one
// int[] corners = this.fieldCorners();
// boolean right = false;
// boolean left = false;
// boolean up = false;
// boolean down = false;
while(true){
char direction = this.getUserInput();
int lastPosition = this.characterSymbolPositions[this.characterSymbolPositions.length-1];
// int characterHead = this.characterSymbolPositions[0];
// if (characterHead == corners[0] || right){
// right = true;
// left = false;
// up = false;
// down = false;
// };
// if (characterHead == corners[1]){
// right = false;
// left = false;
// up = false;
// down = true;
// };
// if (characterHead == corners[2]){
// right = false;
// left = false;
// up = true;
// down = false;
// };
// if (characterHead == corners[3]){
// right = false;
// left = true;
// up = false;
// down = false;
// };
if (direction == 'D')this.characterSymbolPositions = this.newCharacterPositionsRight(this.characterSymbolPositions);
if (direction == 'S')this.characterSymbolPositions = this.newCharacterPositionsDown(this.characterSymbolPositions);
if (direction == 'W')this.characterSymbolPositions = this.newCharacterPositionsUp(this.characterSymbolPositions);
if (direction == 'A')this.characterSymbolPositions = this.newCharacterPositionsLeft(this.characterSymbolPositions);
for (int i = 0; i < this.characterSymbolPositions.length; i++){
this.field[this.characterSymbolPositions[i]] = this.character.getSymbol();
}
this.field[lastPosition] = this.fieldChar;
this.renderField();
}
}
private int[] fieldCorners(){
//Returns an array that is just the 4 corners of the field array, will be used to compare the position of the head at any given moment and prevent it from going "through" the board
int upperLeft = 0;
int upperRight = this.width-1;
int lowerLeft = upperLeft + (this.width*(this.length-1));
int lowerRight = upperRight + (this.width*(this.length-1));
return new int[]{upperLeft, upperRight, lowerLeft, lowerRight};
}
public int moveCharacterSymbolDown(int currentPosition){
return currentPosition + this.width;
}
public int[] newCharacterPositionsRight(int[] currentPositions){
int[] newPositions = new int [currentPositions.length];
for(int i = 0; i < currentPositions.length-1; i++){
if (i == 0) newPositions[i] = currentPositions[i]+1;
newPositions[i+1] = currentPositions[i];
}
return newPositions;
}
public int[] newCharacterPositionsDown(int[] currentPositions){
int[] newPositions = new int [currentPositions.length];
for(int i = 0; i < currentPositions.length-1; i++){
if (i == 0) newPositions[i] = currentPositions[i]+this.width;
newPositions[i+1] = currentPositions[i];
}
return newPositions;
}
public int[] newCharacterPositionsLeft(int[] currentPositions){
int[] newPositions = new int [currentPositions.length];
for(int i = 0; i < currentPositions.length-1; i++){
if (i == 0) newPositions[i] = currentPositions[i]-1;
newPositions[i+1] = currentPositions[i];
}
return newPositions;
}
public int[] newCharacterPositionsUp(int[] currentPositions){
int[] newPositions = new int [currentPositions.length];
for(int i = 0; i < currentPositions.length-1; i++){
if (i == 0) newPositions[i] = currentPositions[i]-this.width;
newPositions[i+1] = currentPositions[i];
}
return newPositions;
}
}
class Character{
private int length;
private char characterSymbol;
public Character(int length, char characterSymbol){
this.length = length;
this.characterSymbol = characterSymbol;
}
public char getSymbol(){
return this.characterSymbol;
}
public int getLength(){
return this.length;
}
}