Skip to content

Commit 42c1ad0

Browse files
committed
refactor CandyGame.java
1 parent 57f1d01 commit 42c1ad0

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

serialized-lob/src/main/java/com/iluwatar/slob/lob/Forest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@
3737
import org.w3c.dom.Document;
3838
import org.w3c.dom.Element;
3939
import org.w3c.dom.NodeList;
40-
4140
/**
4241
* Creates an object Forest which contains animals and plants as its constituents. Animals may eat
4342
* plants or other animals in the forest.
4443
*/
44+
45+
4546
@Data
4647
@NoArgsConstructor
4748
@AllArgsConstructor
4849
public class Forest implements Serializable {
49-
50+
public static final String HORIZONTAL_DIVIDER = "\n--------------------------\n";
5051
private String name;
5152
private Set<Animal> animals = new HashSet<>();
5253
private Set<Plant> plants = new HashSet<>();
@@ -105,16 +106,16 @@ public String toString() {
105106
sb.append("Forest Name = ").append(name).append("\n");
106107
sb.append("Animals found in the ").append(name).append(" Forest: \n");
107108
for (Animal animal : animals) {
108-
sb.append("\n--------------------------\n");
109+
sb.append(HORIZONTAL_DIVIDER);
109110
sb.append(animal.toString());
110-
sb.append("\n--------------------------\n");
111+
sb.append(HORIZONTAL_DIVIDER);
111112
}
112113
sb.append("\n");
113114
sb.append("Plants in the ").append(name).append(" Forest: \n");
114115
for (Plant plant : plants) {
115-
sb.append("\n--------------------------\n");
116+
sb.append(HORIZONTAL_DIVIDER);
116117
sb.append(plant.toString());
117-
sb.append("\n--------------------------\n");
118+
sb.append(HORIZONTAL_DIVIDER);
118119
}
119120
return sb.toString();
120121
}

type-object/src/main/java/com/iluwatar/typeobject/CandyGame.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.iluwatar.typeobject.Candy.Type;
2828
import java.util.ArrayList;
2929
import java.util.List;
30+
import org.slf4j.LoggerFactory;
31+
3032
import lombok.extern.slf4j.Slf4j;
3133

3234
/**
@@ -37,7 +39,6 @@
3739
@Slf4j
3840
@SuppressWarnings("java:S3776") //"Cognitive Complexity of methods should not be too high"
3941
public class CandyGame {
40-
4142
Cell[][] cells;
4243
CellPool pool;
4344
int totalPoints;
@@ -86,19 +87,28 @@ List<Cell> adjacentCells(int y, int x) {
8687
adjacent.add(this.cells[y][1]);
8788
}
8889
if (y == cells.length - 1) {
89-
adjacent.add(this.cells[cells.length - 2][x]);
90+
if (cells.length > 1) {
91+
adjacent.add(this.cells[cells.length - 2][x]);
92+
} else {
93+
LOGGER.info("Warning: Attempted to access a row in an array with insufficient rows.");
94+
}
9095
}
9196
if (x == cells.length - 1) {
92-
adjacent.add(this.cells[y][cells.length - 2]);
97+
if (cells.length > 1) {
98+
adjacent.add(this.cells[y][cells.length - 2]);
99+
} else {
100+
LOGGER.info("Warning: Attempted to access an out-of-bounds index.");
101+
}
93102
}
103+
94104
if (y > 0 && y < cells.length - 1) {
95105
adjacent.add(this.cells[y - 1][x]);
96106
adjacent.add(this.cells[y + 1][x]);
97107
}
98-
if (x > 0 && x < cells.length - 1) {
108+
if (y >= 0 && y < cells.length && x > 0 && x < cells[y].length - 1) {
99109
adjacent.add(this.cells[y][x - 1]);
100110
adjacent.add(this.cells[y][x + 1]);
101-
}
111+
}
102112
return adjacent;
103113
}
104114

0 commit comments

Comments
 (0)