Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Why pointer assignment sets all parent pointers above it to the same thing C
- state current = state(state::goalState);
- current.setEqualTo(OPEN.front());
- void state::setEqualTo(const state x){
- this->parent = x.parent;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- this->board[i][j] = x.board[i][j];
- }
- }
- this->howWeGotHere = x.howWeGotHere;
- }
- list<state> OPEN;
- state current = state(state::goalState);
- current.setEqualTo(OPEN.front());
- list<state> children = current.generateChildren();
- OPEN.splice(OPEN.end(), children);
- //State Class
- state *parent = NULL;
- string howWeGotHere = "";
- int board[3][3];
- state::state(int x[3][3], state *parent) {
- this->parent = parent;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- this->board[i][j] = x[i][j];
- }
- }
- }
- state state::moveLeft() {
- int iIndex;
- int jIndex;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- if (board[i][j] == 0) {
- iIndex = i;
- jIndex=j;
- }
- }
- }
- state z = state(board, this);
- if (2 == jIndex) {
- return *this;
- } else {
- z.board[iIndex][jIndex] = z.board[iIndex][jIndex+1];
- z.board[iIndex][jIndex+1] = 0;
- z.howWeGotHere = "LEFT";
- }
- return z;
- }
- state state::moveRight() {
- int iIndex;
- int jIndex;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- if (board[i][j] == 0) {
- iIndex = i;
- jIndex=j;
- }
- }
- }
- state z = state(board, this);
- if (0 == jIndex) {
- return *this;
- } else {
- z.board[iIndex][jIndex] = z.board[iIndex][jIndex-1];
- z.board[iIndex][jIndex-1] = 0;
- z.howWeGotHere = "RIGHT";
- }
- return z;
- }
- state state::moveDown() {
- int iIndex;
- int jIndex;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- if (board[i][j] == 0) {
- iIndex = i;
- jIndex=j;
- }
- }
- }
- state z = state(board, this);
- if (0 == iIndex) {
- return *this;
- } else {
- z.board[iIndex][jIndex] = z.board[iIndex-1][jIndex];
- z.board[iIndex-1][jIndex] = 0;
- z.howWeGotHere = "DOWN";
- }
- return z;
- }
- state state::moveUp() {
- int iIndex;
- int jIndex;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- if (board[i][j] == 0) {
- iIndex = i;
- jIndex = j;
- }
- }
- }
- state z = state(board, this);
- if (2 == iIndex) {
- return *this;
- } else {
- z.board[iIndex][jIndex] = z.board[iIndex+1][jIndex];
- z.board[iIndex+1][jIndex] = 0;
- z.howWeGotHere = "UP";
- }
- return z;
- }
- void state::setEqualTo(const state x){
- this->parent = x.parent;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- this->board[i][j] = x.board[i][j];
- }
- }
- this->howWeGotHere = x.howWeGotHere;
- }
- list<state> state::generateChildren(){
- list<state> children;
- state temp(board);
- temp.setEqualTo(this->moveUp());
- if(*this == temp) {
- } else {
- children.push_front(temp);
- }
- temp.setEqualTo(this->moveDown());
- if(*this == temp) {
- } else {
- children.push_front(temp);
- }
- temp.setEqualTo(this->moveLeft());
- if(*this == temp) {
- } else {
- children.push_front(temp);
- }
- temp.setEqualTo(this->moveRight());
- if(*this == temp) {
- } else {
- children.push_front(temp);
- }
- return children;
- }
Add Comment
Please, Sign In to add comment