-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHUFF.CPP
167 lines (139 loc) · 2.54 KB
/
SHUFF.CPP
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
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void print();
void randomarray();
void check();
void move();
int as[5][5], size, arrwidth, shuffle_score=0, flagwin=0;
int i, j, t1, t2, points=0;
char vs=186, hs=205;
int mainshuffle()
{
clrscr();
flagwin=0;
points=0;
cout<<"enter size (2-5):";
cin>>arrwidth;
t1=t2=arrwidth-1;
randomarray();
while(1){
check();
if(flagwin)
break;
print();
move();
}
getch();
return (shuffle_score);
}
void randomarray()
{
randomize();
int i,k,j;
int elements; int b[25];
l:
elements= (arrwidth*arrwidth)-1;
for(i=1; i<=25; i++)
b[i-1]=i;
for(i=0; i<arrwidth; i++)
{
for(j=0; j<arrwidth; j++)
{
k=random(elements);
as[i][j]=b[k];
b[k]=b[elements-1];
elements--;
}
}
as[arrwidth-1][arrwidth-1]=0;
if(a[0][0]==1)
goto l;
}
void print()
{
clrscr();
cout<<endl<<endl<<" Shuffle game"<<endl<<endl<<endl<<endl;
cout<<endl<<' ';
int z=0;
while(z++<arrwidth)
cout<<hs<<hs<<hs<<hs<<' ';
cout<<endl;
for(i=0; i<arrwidth; i++)
{
cout<<vs<<' ';
for(j=0; j<arrwidth; j++)
{
if(as[i][j]==0)
cout<<' '<<' '<<' '<<vs<<' ';
else
{
if(as[i][j]/10==0)
cout<<as[i][j]<<' '<<' '<<vs<<' ';
else
cout<< as[i][j]<<' '<<vs<<' ';
}
}
cout<<endl<<' ';
z=0;
while(z++<arrwidth)
cout<<hs<<hs<<hs<<hs<<' ';
cout<<endl;
}
cout<<endl<<endl<<endl<<endl;
}
void move()
{
int c,flag=0;
cout<<"Enter number to move or 0 to exit:";
cin>>c;
points++;
cout<<"Number of moves:"<<points;
if(c==0)
exit(0);
for(i=0; i<arrwidth; i++)
{
for(j=0; j<arrwidth; j++)
if(as[i][j]==c)
{
as[t1][t2]=c;
as[i][j]=0;
t2=j; t1=i;
flag=1;
break;
}
if(flag)
break;
}
}
void check()
{
int flag=0, number=1;
for(i=0; i<arrwidth; i++)
{
for(j=0; j<arrwidth; j++)
{
if(as[i][j]==0)
as[i][j]=arrwidth*arrwidth;
if(as[i][j]!=number)
{
flag=1;
break;
}
number++;
}
if(flag)
break;
}
if(as[i][j]==arrwidth*arrwidth)
as[i][j]=0;
if(!flag)
{
clrscr();
cout<<endl<<endl<<endl<<endl<<"You have won!";
cout<<endl<<endl<<"Total Points: "<<100-points;
shuffle_score=100-points;
getch();
flagwin=1;
}
}