board.h /* * board.h * hw1pr1 * * Created by Da Xin on 6/3/12. * Copyright 2012. All rights reserved. * */ #include <iostream> #include <vector> using namespace std; class board{ int values[5][5], blankX, blankY, parent, index; public: board(int _values[5][5], int _blankX, int _blankY, int _index, int _parent); void printBoard(); bool checkUp(); bool checkDown(); bool checkLeft(); bool checkRight(); void createChild(vector<board> &_children, int &_index); void exchangeUp(); void exchangeDown(); void exchangeLeft(); void exchangeRight(); int nextLevel(vector<board> &_children, int &_index, board _goal, int &parentCount); void findSolution(vector<board> &_vector, int &_index, board _goal); bool compare(board _goal); int getParent(){return parent;}; void moveUp(){blankX--;}; void moveDown(){blankX++;}; void moveLeft(){blankY--;}; void moveRight(){blankY++;}; int getBlankX(){return blankX;}; int getBlankY(){return blankY;}; }; |
board.cpp /* * board.cpp * hw1pr1 * * Created by Da Xin on 6/3/12. * Copyright 2012. All rights reserved. * */ #include "board.h" board::board(int _values[5][5], int _blankX, int _blankY, int _index, int _parent){ for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { values[i][j]=_values[i][j]; } } blankX = _blankX; blankY = _blankY; index = _index; parent = _parent; } void board::printBoard(){ for (int i=1; i<4; i++) { for (int j=1; j<4; j++) { cout << values[i][j]<<' '; } cout << '\n'; } } bool board::checkUp(){ if (values[blankX-1][blankY]==-1) { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check up fails\n"; return false; }else { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check up works\n"; return true; } } bool board::checkDown(){ if (values[blankX+1][blankY]==-1) { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check down fails\n"; return false; }else { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check down works\n"; return true; } } bool board::checkLeft(){ if (values[blankX][blankY-1]==-1) { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check left fails\n"; return false; }else { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check left works\n"; return true; } } bool board::checkRight(){ if (values[blankX][blankY+1]==-1) { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check right fails\n"; return false; }else { cout << "blankX: " << blankX << " blankY: " << blankY << '\n'; cout << "check right works\n"; return true; } } void board::createChild(vector<board>& _children, int& _index){ _children.push_back(board(values, blankX, blankY, index, _index)); _index++; } void board::exchangeUp(){ values[blankX][blankY]=values[blankX-1][blankY]; values[blankX-1][blankY]=0; cout << "exchange up \n"; } void board::exchangeDown(){ values[blankX][blankY]=values[blankX+1][blankY]; values[blankX+1][blankY]=0; cout << "exchange down \n"; } void board::exchangeLeft(){ values[blankX][blankY]=values[blankX][blankY-1]; values[blankX][blankY-1]=0; cout << "exchange left\n"; } void board::exchangeRight(){ values[blankX][blankY]=values[blankX][blankY+1]; values[blankX][blankY+1]=0; cout << "exchange right\n"; } int board::nextLevel(vector<board> &_vector, int &_index, board _goal, int &parentCount){ if(_vector[parentCount].checkUp()){ createChild(_vector, _index); _vector[_index].exchangeUp(); _vector[_index].moveUp(); cout << _index << '\n'; } if(_vector[parentCount].checkDown()){ createChild(_vector, _index); _vector[_index].exchangeDown(); _vector[_index].moveDown(); cout << _index << '\n'; } if(_vector[parentCount].checkLeft()){ createChild(_vector, _index); _vector[_index].exchangeLeft(); _vector[_index].moveLeft(); cout << _index << '\n'; } if(_vector[parentCount].checkRight()){ createChild(_vector, _index); _vector[_index].exchangeRight(); _vector[_index].moveRight(); cout << _index << '\n'; } return _index; } bool board::compare(board _goal){ for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { if(values[i][j]!=_goal.values[i][j]){ return false; } } } return true; } |
main.cpp #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; cout << "Please enter the initial state with a number followed by a comma (1,2,3,4,5,6,7,8,9), 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 (1,2,3,4,5,6,7,8,9), 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); 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."; counter = i; } return 0; } |