|
| 1 | +#include <iostream> |
| 2 | +#include <iomanip> |
| 3 | +#include <fstream> |
| 4 | +#include <cassert> |
| 5 | +#include <cstring> |
| 6 | +#include <cctype> |
| 7 | +#include "solitaire.h" |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +int main() { |
| 12 | + |
| 13 | + /* this section illustrates the use of the pre-supplied functions */ |
| 14 | + cout << "============== Pre-supplied functions ==================" << endl << endl; |
| 15 | + |
| 16 | + int height, width; |
| 17 | + char **puzzle = load_board("board.txt", height, width); |
| 18 | + assert(puzzle); |
| 19 | + |
| 20 | + cout << "Printing puzzle board:" << endl; |
| 21 | + /* prints the puzzle board with row and column numbers */ |
| 22 | + print_board(puzzle, height, width); |
| 23 | + cout << endl; |
| 24 | + |
| 25 | + cout << "====================== Question 1 ======================" << endl << endl; |
| 26 | + |
| 27 | + int height1, width1; |
| 28 | + char **board1 = load_board("board1.txt", height1, width1); |
| 29 | + assert(board1); |
| 30 | + print_board(board1, height, width); |
| 31 | + |
| 32 | + int height2, width2; |
| 33 | + char **board2 = load_board("board2.txt", height2, width2); |
| 34 | + assert(board2); |
| 35 | + print_board(board2, height, width); |
| 36 | + |
| 37 | + int height3, width3; |
| 38 | + char **board3 = load_board("board3.txt", height3, width3); |
| 39 | + assert(board3); |
| 40 | + print_board(board3, height, width); |
| 41 | + |
| 42 | + assert(height2 == height1 && width2 == width1); |
| 43 | + assert(height3 == height1 && width3 == width1); |
| 44 | + |
| 45 | + cout << endl; |
| 46 | + |
| 47 | + bool same = are_identical(board1, board2, height1, width1); |
| 48 | + cout << "Boards 1 and 2 are " << (same ? "" : "not ") << "identical." << endl << endl; |
| 49 | + |
| 50 | + same = are_identical(board2, board3, height1, width1); |
| 51 | + cout << "Boards 2 and 3 are " << (same ? "" : "not ") << "identical." << endl << endl; |
| 52 | + |
| 53 | + deallocate_2D_array(board1, height1); |
| 54 | + deallocate_2D_array(board2, height2); |
| 55 | + deallocate_2D_array(board3, height3); |
| 56 | + |
| 57 | + cout << "====================== Question 2 ======================" << endl << endl; |
| 58 | + |
| 59 | + cout << "Starting from:" << endl; |
| 60 | + print_board(puzzle, height, width); |
| 61 | + cout << endl; |
| 62 | + |
| 63 | + /* uncomment for simple test case |
| 64 | +
|
| 65 | + cout << "Move D1S is"; |
| 66 | + if (make_move(puzzle, "D1S", height, width)) { |
| 67 | + cout << " valid" << endl; |
| 68 | + print_board(puzzle, height, width); |
| 69 | + } else |
| 70 | + cout << " invalid" << endl; |
| 71 | + cout << endl; */ |
| 72 | + |
| 73 | + |
| 74 | + const char *moves[] = { "D1S", "D0S", "F2W", "D2N", "A2E", NULL }; |
| 75 | + |
| 76 | + for (int n=0; moves[n]; n++) { |
| 77 | + cout << "Move " << moves[n] << " is"; |
| 78 | + if (make_move(puzzle, moves[n], height, width)) { |
| 79 | + cout << " valid" << endl; |
| 80 | + print_board(puzzle, height, width); |
| 81 | + } else |
| 82 | + cout << " invalid" << endl; |
| 83 | + cout << endl; |
| 84 | + } |
| 85 | + |
| 86 | + deallocate_2D_array(puzzle, height); |
| 87 | + |
| 88 | + cout << "====================== Question 3 ======================" << endl << endl; |
| 89 | + |
| 90 | + /* uncomment for simple test case */ |
| 91 | + |
| 92 | + int h,w; |
| 93 | + char **first = load_board("target5.txt", h, w); |
| 94 | + assert(first); |
| 95 | + char **second = load_board("solution.txt", h, w); |
| 96 | + assert(second); |
| 97 | + char sequence[512]; |
| 98 | + bool result = find_solution(first, second, h, w, sequence); |
| 99 | + if (result) |
| 100 | + cout << "Success! Move sequence: " << sequence << endl; |
| 101 | + else |
| 102 | + cout << "No solution found" << endl; |
| 103 | + cout << endl; |
| 104 | + print_board(first, height, width); |
| 105 | + print_board(second, height, width); |
| 106 | + deallocate_2D_array(first, h); |
| 107 | + deallocate_2D_array(second, w); |
| 108 | + |
| 109 | + const char *filenames[] = { "board.txt", "target1.txt", "target2.txt", "target3.txt", |
| 110 | + "target4.txt", "target5.txt", "solution.txt", NULL }; |
| 111 | + |
| 112 | + char **start = load_board(filenames[0], height, width); |
| 113 | + assert(start); |
| 114 | + for (int n=1; filenames[n]; n++) { |
| 115 | + cout << "Attempting to reach target '" << filenames[n] << "'..." << endl; |
| 116 | + char **target = load_board(filenames[n], height, width); |
| 117 | + assert(target); |
| 118 | + |
| 119 | + char solution[1024]; |
| 120 | + bool success = find_solution(start, target, height, width, solution); |
| 121 | + |
| 122 | + if (success) { |
| 123 | + print_board(start, height, width); |
| 124 | + cout << "Target '" << filenames[n] << "' achieved!" << endl; |
| 125 | + cout << "Move sequence: " << solution << endl << endl; |
| 126 | + } else { |
| 127 | + cout << "No solution found" << endl; |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | + deallocate_2D_array(target, height); |
| 132 | + } |
| 133 | + deallocate_2D_array(start, height); |
| 134 | + |
| 135 | + cout << "======================= The End ========================" << endl << endl; |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
0 commit comments