Repository navigation
Expand file tree
/
Copy pathgame15.cpp
More file actions
132 lines (126 loc) · 2.38 KB
/
Copy pathgame15.cpp
File metadata and controls
132 lines (126 loc) · 2.38 KB
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
#include<iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int field[4][4];
int empty_x, empty_y;
void create_field()
{
int i;
bool null_cell[15];
int cells[15];
int rand_num;
bool ok;
for (i = 0; i < 15; i++)
null_cell[i] = true;
for (i = 0; i < 15; i++)
{
ok = false;
while (!ok)
{
rand_num = rand() %15 +1;
if (null_cell[rand_num - 1])
{
null_cell[rand_num - 1] =false;
cells[i] = rand_num;
ok = true;
}
}
}
for (int i = 0; i < 15; i++)
{
field[i % 4][i / 4] = cells[i];
}
field[3][3] = 0;
empty_x = 3;
empty_y = 3;
}
void print_field()
{
system("cls");
for (int i = 0; i < 4; i++)
{
cout << "|---||---||---||---|" << endl;
for (int j = 0; j < 4; j++)
{
cout << "|" ;
cout.width(3);
if (field[i][j] != 0)
cout << field[i][j];
else
cout << " ";
cout << "|";
}
cout << endl << "|---||---||---||---|" << endl;
}
}
void move(int dir)
{
switch (dir)
{
case 1:
{
if (empty_x < 3)
{
field[empty_x][empty_y] = field[empty_x + 1][empty_y];
field[empty_x + 1][empty_y] = 0;
empty_x++;
}
} break;
case 2:
{
if (empty_y < 3)
{
field[empty_x][empty_y] = field[empty_x][empty_y + 1];
field[empty_x][empty_y + 1] = 0;
empty_y++;
}
} break;
case 3:
{
if (empty_x > 0)
{
field[empty_x][empty_y] = field[empty_x - 1][empty_y];
field[empty_x - 1][empty_y] = 0;
empty_x--;
}
} break;
case 4:
{
if (empty_y > 0)
{
field[empty_x][empty_y] = field[empty_x][empty_y - 1];
field[empty_x][empty_y - 1] = 0;
empty_y--;
}
} break;
}
}
bool is_correct()
{
for (int i = 0; i < 15; i++)
if (field[i % 4][i / 4] != i + 1)
return false;
return true;
}
int main ()
{
create_field();
print_field();
char key;
while (!is_correct())
{
key = getch();
switch((int)key) // çàâèñèìîñòè îò íàæàòîé êëàâèøè (îò åå öåëî÷èñëåííîãî êîäà) äâèãàåì êîñòÿøêè
{
case 56: move(1); break; //Íàæàòà êëàâèøà "Ââåðõ"
case 52: move(2); break; //Íàæàòà êëàâèøà "Âëåâî"
case 50: move(3); break; //Íàæàòà êëàâèøà "Âíèç"
case 54: move(4); break; //Íàæàòà êëàâèøà "Âïðàâî"
case 27: return 0; //Íàæàòà êëàâèøà "Escape"
}
print_field();
cout << (int)key <<endl;
}
return 0;
}