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
|
#include <iostream>
#include <sstream>
#include <vector>
#include "board.h"
using namespace std;
int main (int argc, char * const argv[]) {
stringstream ss;
string initialState, goalState, test;
int tempStore, blankXstate, blankYstate;
vector<int> initialList;
vector<int> goalList;
int count = 0;
int count2 = 0;
int parentIndexCounter=0;
vector<board> tree;
int indexCount = 0;
int counter = 0;
vector<int> final;
cout << "Please enter the initial state with a number followed by a comma (0,1,2,3,4,5,6,7,8), no spaces.\n";
getline(cin, initialState);
ss << initialState;
while (ss >> tempStore) {
initialList.push_back(tempStore);
if(ss.peek() == ','){
ss.ignore();
}
}
ss.str("");
ss.clear();
cout << "Please enter the goal state with a number followed by a comma (0,1,2,3,4,5,6,7,8), no spaces.\n";
getline(cin, goalState);
ss << goalState;
while (ss >> tempStore) {
goalList.push_back(tempStore);
if(ss.peek() == ','){
ss.ignore();
}
}
int initialBoard[5][5];
int goalBoard[5][5];
for (int i=0; i<5; i++) {
if (i==0 || i==4) {
for (int j=0; j<5; j++) {
initialBoard[i][j] = -1;
}
}else {
initialBoard[i][0] = -1;
initialBoard[i][4] = -1;
}
}
for (int i=1; i<4; i++) {
for (int j=1; j<4; j++) {
initialBoard[i][j]=initialList[count];
if (initialBoard[i][j]==0) {
blankXstate = i;
blankYstate = j;
}
count++;
}
}
for (int i=0; i<5; i++) {
if (i==0 || i==4) {
for (int j=0; j<5; j++) {
goalBoard[i][j] = -1;
}
}else {
goalBoard[i][0] = -1;
goalBoard[i][4] = -1;
}
}
for (int i=1; i<4; i++) {
for (int j=1; j<4; j++) {
goalBoard[i][j]=goalList[count2];
count2++;
}
}
board firstCreatedBoard(initialBoard, blankXstate, blankYstate, 0, NULL);
board goalCreateBoard(goalBoard, NULL, NULL, NULL, NULL);
tree.push_back(firstCreatedBoard);
tree[0].setManhattan(goalCreateBoard);
vector <int> nodes;
for (int i=0; !(tree[i].compare(goalCreateBoard)); i++) {
tree[i].nextLevel(tree, indexCount, goalCreateBoard, parentIndexCounter);
parentIndexCounter++;
cout << "Generated " << parentIndexCounter - 1 << " states." << '\n';
counter = i + 1;
}
while (counter != -1) {
final.push_back(counter);
counter = tree[counter].getParent();
if (counter == 0) {
counter = -1;
}
}
final.push_back(0);
reverse(final.begin(),final.end());
for (int i = 0; i < final.size(); i++) {
cout << "Step " << i << '\n';
tree[final[i]].printBoard();
}
return 0;
}
|