Skip to content

Commit 5d48440

Browse files
committed
Cosmetic adjustment of solution of several recursion tasks
1 parent c4c49d4 commit 5d48440

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

src/main/java/by/andd3dfx/recursion/EightQueens.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class EightQueens {
1515
@Data
1616
@AllArgsConstructor
1717
public static class Solution {
18+
1819
private boolean isFound;
1920
private boolean[][] cellsTaken;
2021

@@ -32,16 +33,16 @@ public int[][] prettyPrint() {
3233

3334
public Solution solve(int boardSize) {
3435
boolean[][] cellsTaken = new boolean[boardSize][boardSize];
35-
boolean isFound = checkSolution(boardSize, cellsTaken, 0);
36+
boolean isSolutionFound = checkSolution(boardSize, cellsTaken, 0);
3637

37-
return new Solution(isFound, cellsTaken);
38+
return new Solution(isSolutionFound, cellsTaken);
3839
}
3940

40-
private boolean checkSolution(int size, boolean[][] cellsTaken, int queensPositioned) {
41+
private boolean checkSolution(int size, boolean[][] cellsTaken, int positionedQueensAmount) {
4142
if (!isLegal(size, cellsTaken)) {
4243
return false;
4344
}
44-
if (queensPositioned == size) {
45+
if (positionedQueensAmount == size) {
4546
return true;
4647
}
4748

@@ -50,7 +51,7 @@ private boolean checkSolution(int size, boolean[][] cellsTaken, int queensPositi
5051
if (!cellsTaken[row][col]) {
5152
cellsTaken[row][col] = true;
5253

53-
if (checkSolution(size, cellsTaken, queensPositioned + 1)) {
54+
if (checkSolution(size, cellsTaken, positionedQueensAmount + 1)) {
5455
return true;
5556
} else {
5657
cellsTaken[row][col] = false;

src/main/java/by/andd3dfx/recursion/HanoiTowers.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class HanoiTowers {
1616
public static class Column {
1717
private final String name;
1818
@Getter
19-
private Deque<Integer> stack = new ArrayDeque<>();
19+
private final Deque<Integer> stack = new ArrayDeque<>();
2020
}
2121

2222
private final int height;
23-
private Column left;
24-
private Column middle;
25-
private Column right;
23+
private final Column left;
24+
private final Column middle;
25+
private final Column right;
2626

2727
public HanoiTowers(int height) {
2828
this.height = height;

src/main/java/by/andd3dfx/recursion/HorseWalk.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public String convertToCheckMateCoordinateSystem() {
2828
@Data
2929
@AllArgsConstructor
3030
public static class Solution {
31-
private final boolean isFound;
31+
private final boolean isSolutionFound;
3232
private final boolean[][] cellsTaken;
3333
private final Deque<Cell> log;
3434

3535
public String logToString() {
3636
var size = cellsTaken.length;
37-
if (!isFound || log.size() != size * size) {
38-
throw new IllegalStateException("Solution still not found!");
37+
if (!isSolutionFound || log.size() != size * size) {
38+
throw new IllegalStateException("The solution is still not found!");
3939
}
4040

4141
int[][] board = new int[size][size];
@@ -45,15 +45,14 @@ public String logToString() {
4545
board[cell.x][cell.y] = positionNumber;
4646
}
4747

48-
var result = "";
48+
StringBuilder sb = new StringBuilder();
4949
for (var x = 0; x < size; x++) {
50-
var line = "";
5150
for (var y = 0; y < size; y++) {
52-
line += board[x][y] + "\t";
51+
sb.append(board[x][y]).append("\t");
5352
}
54-
result += line.trim() + "\n";
53+
sb.append("\n");
5554
}
56-
return result;
55+
return sb.toString();
5756
}
5857
}
5958

@@ -64,13 +63,13 @@ public Solution solve(int boardSize) {
6463
Deque<Cell> log = new ArrayDeque<>();
6564
log.push(new Cell(0, 0));
6665

67-
boolean isFound = checkSolution(boardSize, cellsTaken, 1, 0, 0, log);
66+
boolean isSolutionFound = checkSolution(boardSize, cellsTaken, 1, 0, 0, log);
6867

69-
return new Solution(isFound, cellsTaken, log);
68+
return new Solution(isSolutionFound, cellsTaken, log);
7069
}
7170

72-
public boolean checkSolution(int size, boolean[][] cellsTaken, int cellsVisited, int currX, int currY, Deque<Cell> log) {
73-
if (cellsVisited == size * size) {
71+
public boolean checkSolution(int size, boolean[][] cellsTaken, int visitedCellsAmount, int currX, int currY, Deque<Cell> log) {
72+
if (visitedCellsAmount == size * size) {
7473
return true;
7574
}
7675

@@ -88,7 +87,8 @@ public boolean checkSolution(int size, boolean[][] cellsTaken, int cellsVisited,
8887

8988
cellsTaken[nextX][nextY] = true;
9089
log.push(new Cell(nextX, nextY));
91-
if (checkSolution(size, cellsTaken, cellsVisited + 1, nextX, nextY, log)) {
90+
91+
if (checkSolution(size, cellsTaken, visitedCellsAmount + 1, nextX, nextY, log)) {
9292
return true;
9393
} else {
9494
log.pop();

src/test/java/by/andd3dfx/recursion/HorseWalkTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public void cell() {
8888
private void commonSolve(int boardSize, boolean solutionExpected) {
8989
HorseWalk.Solution solution = horseWalk.solve(boardSize);
9090

91-
assertThat(solution.isFound()).isEqualTo(solutionExpected);
92-
if (solution.isFound()) {
91+
assertThat(solution.isSolutionFound()).isEqualTo(solutionExpected);
92+
if (solution.isSolutionFound()) {
9393
checkAreAllCellsVisited(boardSize, solution);
9494
System.out.println(solution.logToString());
9595
}

0 commit comments

Comments
 (0)