-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPegXO.java
More file actions
146 lines (131 loc) · 4.77 KB
/
PegXO.java
File metadata and controls
146 lines (131 loc) · 4.77 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
/*
<COSC 2007>
<Rajin Santos Gajadhar>
<239479650>
<Assignment 1>
*/
import java.util.ArrayList;
import java.util.List;
public class PegXO {
private static final int SIZE = 7;
private static final char PEG = 'X';
private static final char EMPTY = 'O';
private char[][] board;
private List<String> moves;
// Constructor
public PegXO() {
this.board = new char[SIZE][SIZE];
this.moves = new ArrayList<>();
initializeBoard();
}
// Initialize the board with pegs and one empty space in the middle
private void initializeBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if ((i < 2 || i > 4) && (j < 2 || j > 4)) {
this.board[i][j] = ' ';
} else {
this.board[i][j] = PEG;
}
}
}
this.board[3][3] = EMPTY;
}
// Print the current state of the board
public void printBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.print(this.board[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
// The recursive backtracking algorithm
public boolean findSolution(int move) {
if (isSolved()) {
System.out.println("Final Board:");
printBoard();
return true;
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == PEG) {
// Check all four directions
if (canJump(i, j, i, j + 1, i, j + 2)) {
makeMove(i, j, i, j + 2);
moves.add("Move " + move + ": (" + i + "," + j + ") -> (" + i + "," + (j + 2) + ")");
if (findSolution(move + 1)) return true;
undoMove(i, j, i, j + 2);
moves.remove(moves.size() - 1);
}
if (canJump(i, j, i, j - 1, i, j - 2)) {
makeMove(i, j, i, j - 2);
moves.add("Move " + move + ": (" + i + "," + j + ") -> (" + i + "," + (j - 2) + ")");
if (findSolution(move + 1)) return true;
undoMove(i, j, i, j - 2);
moves.remove(moves.size() - 1);
}
if (canJump(i, j, i + 1, j, i + 2, j)) {
makeMove(i, j, i + 2, j);
moves.add("Move " + move + ": (" + i + "," + j + ") -> (" + (i + 2) + "," + j + ")");
if (findSolution(move + 1)) return true;
undoMove(i, j, i + 2, j);
moves.remove(moves.size() - 1);
}
if (canJump(i, j, i - 1, j, i - 2, j)) {
makeMove(i, j, i - 2, j);
moves.add("Move " + move + ": (" + i + "," + j + ") -> (" + (i - 2) + "," + j + ")");
if (findSolution(move + 1)) return true;
undoMove(i, j, i - 2, j);
moves.remove(moves.size() - 1);
}
}
}
}
return false;
}
private boolean isSolved() {
int count = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == PEG) {
count++;
}
}
}
return count == 1 && board[3][3] == PEG;
}
private boolean canJump(int fromX, int fromY, int overX, int overY, int toX, int toY) {
if (toX < 0 || toX >= SIZE || toY < 0 || toY >= SIZE || board[fromX][fromY] != PEG || board[overX][overY] != PEG || board[toX][toY] != EMPTY) {
return false;
}
return true;
}
private void makeMove(int fromX, int fromY, int toX, int toY) {
board[fromX][fromY] = EMPTY;
board[(fromX + toX) / 2][(fromY + toY) / 2] = EMPTY;
board[toX][toY] = PEG;
}
private void undoMove(int fromX, int fromY, int toX, int toY) {
board[fromX][fromY] = PEG;
board[(fromX + toX) / 2][(fromY + toY) / 2] = PEG;
board[toX][toY] = EMPTY;
}
public void printMoves() {
System.out.println("List of Moves:");
for (String move : moves) {
System.out.println(move);
}
}
public static void main(String[] args) {
PegXO game = new PegXO();
System.out.println("Initial Board:");
game.printBoard();
if (game.findSolution(1)) {
game.printMoves();
} else {
System.out.println("No solution found.");
}
}
}