8 way puzzle

I'm trying to solve an 8 way puzzle that allows the user to input a state of the puzzle and it lists the steps, but my code keeps break in the prepend() member function. can anyone help, please?

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

#define DOWN 0
#define UP 1
#define LEFT 2
#define RIGHT 3


class puzzle {
private:
    struct state
    {
        int block[9];
        char* str;
        int pathcost;
        int valid;
        int totalcost;
        state* next;
    };
    char rep[4] = {'d','u','l','r'};
    int notvalid1[4] = { 6, 0, 0, 2 };
    int notvalid2[4] = { 7, 1, 3, 5 };
    int notvalid3[4] = { 8, 2, 6, 8 };
    int applyparam[4] = { +3, -3, -1, +1 };
    int goal_block[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8}; //8 indicates no tile
    const int maxdepth = 31;
    int block[9];
    state* top;
    
public:
    
    int h(int block[])
    {
        int manCounter = 0;
        
        for(int i=0; i<9; i++)
        {
            manCounter += abs((i/3) - (block[i]/3));
            manCounter += abs((i%3) - (block[i]%3));
        }
        return manCounter;
        
    }
    void prepend(state* newnode, state* oldnode, int op)
    {
        newnode->next = top;
        top = newnode;
        
        newnode->str = oldnode->str;
        newnode->str[oldnode->pathcost] = rep[op];
        newnode->str[oldnode->pathcost+1] = 0;
        
        newnode->pathcost = oldnode->pathcost+1;
        newnode->totalcost = newnode->pathcost + h(newnode->block);
        if (newnode->totalcost < oldnode->totalcost) newnode->totalcost = oldnode->totalcost;
    }
    int goal(int* block)
    {
        int* g_block = goal_block;
        
        for(int i=0; i<9; i++)
            if ((*(block++))!=(*(g_block++)))
                return 0;
        
        return 1;
    }
    int notonqueue(int block[])
    {
        int i;
        state* t = top;
        
        while (t != NULL)
        {
            for(i = 0; i < 9; i++) {
                if (t->block[i] != block[i])
                    break;
            }
            if (i==9)
                return 0;
            
            t = t->next;
        }
        return 1;
    }
    state* bestnodefromqueue()
    {
        state* t = top;
        int min_totalpathcost = 1000;
        state* to_return = NULL;
        
        while (t != NULL)
        {
            if (t->valid==1 && t->totalcost < min_totalpathcost)
            {
                min_totalpathcost = t->totalcost;
                to_return = t;
            }
            t = t->next;
        }
        
        if (to_return != NULL)
            to_return->valid = 0;
        
        return to_return;
    }
    void print_block(int* block)
    {
        for (int i = 0; i < 9; i++) {
            cout << to_char(block[i]);
        }
        cout << endl;
    }
    
    int apply (int* newstate, int* oldstate, int op)
    {
        int j;
        int blank;
        
        for (j=0; j<9; j++) {
            if (oldstate[j]==8)
            {
                blank=j;
                break;
            }
        }
        
        if (blank==notvalid1[op] || blank==notvalid2[op] || blank==notvalid3[op])
            return -1;
        
        for (j=0; j<9; j++)
            newstate[j] = oldstate[j];
        
        newstate[blank] = newstate[blank+applyparam[op]];
        newstate[blank+applyparam[op]] = 8;
        
        return 1;
    }
    state* newelement()
    {
        state* t = new state;
        if (t==NULL)
        {
            return NULL;
        }
        t->valid = 1;
        t->str = new char[maxdepth+1];
        if (t->str ==NULL){
            return NULL;
        }
        t->str[0] = 0;
        t->pathcost = t->totalcost = 0;
        t->next = NULL;
        return t;
    }
    char to_char(int i)
    {
        if (i>=0 &&i<=7) return i+'1';
        else if (i==8) return '0';
        else { printf("ERROR in Program!"); return -1; }
        
    }
    
    int op(char i)
    {
        switch (i)
        {
            case 'd': return 0;
            case 'u': return 1;
            case 'l': return 2;
            case 'r': return 3;
            default:
                cout << "ERROR!";
                return -1;
        }
    }
    void setStart() {
        int i = 0;
        string intstate;
        
        cout << "Enter the start state: ";
        cin >> intstate;
        
        while(i<9)
        {
            if (intstate[i] == '0') {
                block[i] = 8;
            }
            else if (intstate[i] >= '1' && intstate[i] <= '9') {
                block[i] = intstate[i] - '1';
            }
            else
            {
                cout << "Invalid Input" << endl;
            }
            i++;
        }
    }
    void setGoal() {
        int i = 0;
        string goalstate = "012345678";
        
        while (i < 9) {
            if (goalstate[i] == '0')
                goal_block[i] = 8;
            else if (goalstate[i] >= '1' && goalstate[i] <= '9')
                goal_block[i] = goalstate[i] - '1';
            i++;
        }
    }
    void successor() {
        
        top = newelement();
        
        for(int i = 0; i < 9; i++) {
            top->block[i] = block[i];
        }
        top->totalcost = h(block);
        
        state* newnode = newelement();
        
        while (1)
        {
            state* node = bestnodefromqueue();
            if (node == NULL) {
                cout << "If there is no solution." << endl;
                break;
                }
                else if (goal(node->block)) {
                    int block2[9];
                    for (int i = 0; i < node->pathcost; i++)
                    {
                        print_block(block);
                        apply(block2, block, op(node->str[i]));
                        for(int j=0; j<=8; j++)
                            block[j] = block2[j];
                    }
                    print_block(block);
                    break;
                    
                }
                
                if (node->totalcost > maxdepth) continue;
                
                for(int i=0; i<=3; i++) {
                    if (apply(newnode->block, node->block, i) == -1)
                        continue;
                    
                    if (notonqueue(newnode->block)) {
                        prepend(newnode, node, i);
                        newnode = newelement();
                        if (newnode==NULL) {
                            cout << "ERROR!! insufficient memory!!" << endl;
                        }
                    }
                }
                
        }

    }
    
};
Last edited on
Topic archived. No new replies allowed.