15 puzzles code not working properly

Feb 6, 2017 at 3:02am
I don't know why when I prompt the user to enter the number to replace the '*', the entered number will replace the whole board.

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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int SIZE = 4;
const int BLANK_TILE = '*';
const int BOARD_SIZE = 16;

void displayBoard(int board[][SIZE]);
void shuffle(int board[][SIZE], int &blanki, int &blankj);
bool moveTile(int gameBoard[][SIZE], int &nextMove, int &blanki, int &blankj, int &i, int &j);
bool won(int gameBoard[][SIZE]);


int main() 
{
	
	int nextMove;
	int tempVal;
	int i,j;
	
    
   // TODO: create the game board

   int board [][SIZE]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
   cout << "This is the board before shuffling: "<<endl;
   displayBoard(board);
     
   // TODO: shuffle tiles on board
   int blanki, blankj;
  cout << "Now this is the board after we shuffled: "<<endl;
  shuffle(board, blanki, blankj);
  displayBoard(board);
  
  //locate the blank space
	for (int m = 0; m< SIZE; m++)
	{
	  for (int n=0; n<SIZE; n++)
	  {
		  if (board[m][n]==0)
		  {
			  blanki=m;
			  blankj=n;
		  }
	  }
	}
//TO DO: prompt the user to input the move   
  do
  {
	  if(moveTile(board, nextMove, blanki, blankj, i, j)==true)
	  {
		  tempVal=board[i][j];
		  board[i][j]=board[blanki][blankj];
		  board[blanki][blankj]=tempVal;
		  
		  displayBoard(board);
	  }
	  
	  else 
	  {
		  cout <<"Location out of bounds"<<endl;
		  
		  displayBoard(board);
		  
			cout << "\n";
	  }
// TODO: play the game until the user wins!
  }
  while (!won(board));
  
  cout <<"Congratulations, you finish the board"<<endl;
    
    return 0;
}

void displayBoard (int board[][SIZE])
{
	for (int i =0; i<SIZE; i++)
	{
		for (int j=0; j<4; j++)
		{
			if(board[i][j] == 0)
			{
				cout <<"*";
			}
			else
			{
			cout << board[i][j]<<" ";
			}
		}
        cout << endl;   
	}
}
void shuffle(int board[][SIZE], int &blanki, int &blankj)
{
	int temp;
	srand (time(0));
	
	for (int i =0; i<SIZE; i++)
	{
		for (int j=0; j< SIZE; j++)
		{
			int blanki = rand() % 4;
			int blankj =rand() % 4;
			
			temp = board[i][j];
			board[i][j] = board[blanki][blankj];
			board[blanki][blankj]=temp;
		}
	}
}
bool moveTile(int gameBoard[][SIZE], int &nextMove, int &blanki, int &blankj, int &i, int &j)
{	
	int tempVal;
	cout << "Input the number you want to swap with *: "<< endl;
	cin >> nextMove;
	
	for (int p=0; p<SIZE; p++)
	{
		for (int q=0; q<SIZE; q++)
		{
			if (gameBoard[p][q] = nextMove)
			{
				i=p;
				j=q;
			}
		}
	}
	
	if (i==0 && j==0)
	{
		if ((blanki==i && blankj==j+1)||(blanki==i+1 && blankj==j))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
					return true;
		}
		
		else 
			return false;
	}
		
	else if (i==0 && j==3)
	{
		if ((blanki==i && blankj==j-1)||(blanki==i+1 && blankj==j))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
		
				return true;
		}
		
		else 
			return false;
	}
	else if (i==3 && j==3)
	{
		if ((blanki==i-1 && blankj==j) || (blanki==i && blankj == j-1))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
		
			return true;
		}
		
		else 
			return false;
	}
	else if (i==3 && j==0)
	{
		if((blanki==i-1 && blankj==j) || (blanki==i && blankj==j+1))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
				return true;
		}
		
		else 
			return false;
	}
	
	else if ((i==0) && (j>0 && j<SIZE))
	{
		if((blanki ==i && blankj==j-1) || (blanki == i && blankj==j+1) || (blanki == i+1 && blankj==j))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
				return true;
		}
		
		else 
			return false;
	}
	
	else if ((i>0 && i<SIZE) && (j==0))
	{
		if((blanki== i-1 && blankj == j)|| (blanki==i+1 && blankj==j) || (blanki==i && blankj==j+1))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
				return true;
		}
		
		else 
			return false;
	}
	
	else if (( i>0&&i<SIZE) && (j==3))
	{
		if ((blanki==i-1 && blankj==j)||(blanki == i+1 && blankj==j)|| (blanki==i && blankj==j-1))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
				return true;
		}
		
		else 
			return false;
	}
	
	else if ((i=3)&&(j>0&&j<SIZE))
	{
		if((blanki==i&& blankj==j-1)|| (blanki==i&&blankj==j+1)|| (blanki==i-1&&blankj==j))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
				return true;
		}
		
		else 
			return false;
	}
	
	else if ((i>0&&i<3) && (j>0&&j<3))
	{
		if((blanki==i-1 &&blankj==j) || (blanki==i&&blankj==j+1) || (blanki==i&&blankj==j-1) || (blanki==i+1&&blankj==j))
		{
			tempVal=blanki;
			blanki=i;
			i=tempVal;
			
			tempVal=blankj;
			blankj=j;
			j=tempVal;
				return true;
		}
		
		else 
			return false;
	}

}	

	
bool won(int gameBoard[][SIZE])
{
	int count=1;
	for (int i=0; i<SIZE; i++)

		for (int j=0; j<SIZE; j++)
		{
			if (gameBoard[i][j]!=count)
				return false;
			count++;
		}
	
	return true;
}
Feb 6, 2017 at 3:06am
Line 124 :
if (gameBoard[p][q] = nextMove)

Should you use == instead of = ?
Feb 6, 2017 at 3:16am
Thank you I already fixed that but I still work in a very weird way. But thank you for pointing that out for me though!
Feb 6, 2017 at 3:19am
Compilation warnings, as per cpp.sh:

96:38: warning: unused parameter 'blanki' [-Wunused-parameter]
96:51: warning: unused parameter 'blankj' [-Wunused-parameter]
In function 'bool moveTile(int (*)[4], int&, int&, int&, int&, int&)':
124:34: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
294:1: warning: control reaches end of non-void function [-Wreturn-type]


Warnings are your friend: turn on as many compiler warning options as you can.

Line 96: these variables are shadowed by the ones on lines 105 and 106

Line 124: as Mantorr22 says.

Line 294: if you are going to have an else if chain, always put a final else - it catches bad input. You could at least put a return statement there. But you probably want to do more: it's some sort of error.

Always initialise your variables to something. Wait until you have a sensible value to assign to it, and only do 1 per line.

Good Luck !!
Topic archived. No new replies allowed.