Slidding puzzle program

Hello guys the problem is my program is trying to implement the slidding puzzle algorythm but so far i come across a problem which is i can only get it to work once for what im looking for and i suspect the vector isnt changing at all.
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
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <MMSystem.h>
#include <vector>
#include <iomanip>
#pragma comment(lib, "winmm.lib")
#define UP 72
#define DOWN 80
#define RIGHT 77
#define LEFT 75
using namespace std;
void scramble(vector<vector<int>>);
void mexer(vector<vector<int>>,int tamanho);
bool possiblemoves(int emptyRow,int emptyCol,int tamanho,char input);
void printBoard(vector<vector<int>> items,int emptyRow,int emptyCol,int tamanho);

int main()
{
	int x=0,tamanho,i=0,j=0,contador=0,p=0;
	cout<<endl<<endl<<endl<<endl<<endl<<endl<<"\t\t\t-------Welcome to my game-------"<<endl<<endl<<endl<<endl<<"\t\t\t Press enter to continue...";
	while(_kbhit()==0)
	{
		system("color 20");
		Sleep(300);
		system("color 30");
		Sleep(300);
		system("color 40");
		Sleep(300);
		system("color 50");
		Sleep(300);
		system("color 60");
		Sleep(300);
		system("color 70");
		Sleep(300);
	}
	system("cls");
	system("color 70");
	PlaySound(TEXT("C:\\Users\\*user*\\Downloads\\foster.wav"), NULL, SND_ASYNC | SND_LOOP);
	
	
		system("color 71");
		cout<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<endl;
		cout<<char(219)<<" 1. "<<"Modo de treino "<<char(219) <<endl<<char(219)<<"                   "<<char(219)<<endl<<char(219);
		
	cout<<" 2. Modo competicao"<<char(219)<<endl;
	cout<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<char(219)<<endl;

	cout<<"Choose the mode you wanna play";
	cin>>x;

	system("cls");

	cout<<"Qual o tamanho do tabuleiro?"<<endl;
	cin>>tamanho;
	system("cls");
	cout<<endl;
	cout<<""<<setw(5);
	vector<vector<int>> items (tamanho, vector<int> (tamanho));
	while (j !=tamanho)
	{

		while(i < tamanho)
		{

			items[j][i]=contador;

			if (j==0 && i==0)
			{
			cout<<""<<setw(5);
			i++;
			contador++;
			}
			else
			{
			cout<<items[j][i]<<setw(5);
			i++;
			contador++;
			}
			}
		cout<<endl;
		i=0;
		j++;
	}


	while(p==0)
	{
		
		mexer(items,tamanho);


	}

	
 return 0;
}
void mexer(vector<vector<int>> matrix , int tamanho)
{	int emptyRow=0;
	int emptyCol=0;
	char input;
	bool parador=false;

	while (parador==false)
		
		

	{

		while(emptyRow<tamanho && parador==false)
		{

			if (matrix[emptyCol][emptyRow]==0)
			{
			
			parador=true;
			}
			else
			{
				
			emptyRow++;

			}
			}
		
		emptyRow=0;
		emptyCol++;

	}
	emptyCol=emptyCol-1;

	input=_getch();

	if (input==UP)
	{
		if(possiblemoves(emptyRow,emptyCol,tamanho,input))
		{
			matrix[emptyCol][emptyRow]=matrix[emptyCol - 1][emptyRow];
			matrix[emptyCol - 1][emptyRow]=0;
			emptyCol = emptyCol+1;

			system("cls");
			cout<<"up"<<endl;
			printBoard(matrix,emptyRow,emptyCol,tamanho);


		
		}

		else
			cout<<"not possible";


	}

	if (input==DOWN)
	{
		if(possiblemoves(emptyRow,emptyCol,tamanho,input))
		{
		matrix[emptyCol][emptyRow]=matrix[emptyCol + 1][emptyRow];
			matrix[emptyCol + 1][emptyRow]=0;
			emptyCol = emptyCol-1;
			system("cls");
			cout<<"down"<<endl;
			printBoard(matrix,emptyRow,emptyCol,tamanho);
			
		}

		else
			cout<<"not possible";


	}

	if (input==RIGHT)
	{
		if(possiblemoves(emptyRow,emptyCol,tamanho,input))
		{

			matrix[emptyCol][emptyRow]=matrix[emptyCol][emptyRow + 1];
			matrix[emptyCol][emptyRow + 1]=0;
			emptyRow = emptyRow+1;
			system("cls");
			cout<<"right"<<endl;
			printBoard(matrix,emptyRow,emptyCol,tamanho);
		
		}

		else
			cout<<"not possible";


	}

	if (input==LEFT)
	{
		if(possiblemoves(emptyRow,emptyCol,tamanho,input))
		{
			
		matrix[emptyRow][emptyCol]=matrix[emptyRow - 1][emptyCol];
			matrix[emptyRow - 1][emptyCol]=matrix[emptyRow][emptyCol];
		system("cls");
			cout<<"letf"<<endl;
			printBoard(matrix,emptyRow,emptyCol,tamanho);

		}

		else
			cout<<"not possible";


	}

}

bool possiblemoves(int emptyRow,int emptyCol,int tamanho,char input)
{
	if (input==UP)
	{
		if((emptyCol - 1)<0)
			return false;
		else
			return true;
	}
	
	else if(input==DOWN)
	{

		if((emptyCol + 1)>(tamanho-1))
			return false;
		else
			return true;

	}

	else if(input==RIGHT)
	{

		if((emptyRow + 1)>(tamanho-1))
			return false;
		else
			return true;

	}
	if (input==LEFT)
	{
		if((emptyCol - 1)<0)
			return false;
		else
			return true;
	}

	else
		return false;



}

void scramble(vector<vector<int>>)
{






}

void printBoard(vector<vector<int>> items,int emptyRow,int emptyCol,int tamanho)
{
	int j=0,i=0;


	cout<<""<<setw(5);
	while (j !=tamanho)
	{

		while(i < tamanho)
		{


			if (items[j][i]==0)
			{
			cout<<""<<setw(5);
			i++;
			}
			else
			{
			cout<<items[j][i]<<setw(5);
			i++;
			}
			}
		cout<<endl;
		i=0;
		j++;


	}



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