-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-Puzzle.cpp
194 lines (177 loc) · 4.36 KB
/
15-Puzzle.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
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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
void print_instruction();
void print_board_info();
void reset_board();
void draw_board();
void get_input();
void shift(char direction[5] /* "up", "down", "left", "right" */);
void swap(int row1, int col1, int row2, int col2);
void shuffle();
//////////////////////////////////////////////////////////////////////////
#define ALLOW_PRINT_BOARD_INFO false
#define BOARD_SIZE 4
#define NUMBER_OF_SHUFFLE 2500
//////////////////////////////////////////////////////////////////////////
int board[BOARD_SIZE][BOARD_SIZE];
int row_space, col_space;
bool isEnd = false;
int main(){
reset_board();
get_input();
while(!isEnd){
get_input();
}
return 0;
}
void print_instruction(){
printf("PRESS AN ARROW KEY TO SHIFT THE NUMBER.\n");
printf("PRESS 'S' TO SHUFFLE THE BOARD.\n");
printf("PRESS 'R' TO RESET.\n");
printf("PRESS 'Q' TO EXIT.\n");
printf("\n");
}
void print_board_info(){
if(ALLOW_PRINT_BOARD_INFO)
printf("Row space: %d\nCol space: %d\n",row_space, col_space);
}
void reset_board(){
system("cls");
print_instruction();
for(int row = 0; row < BOARD_SIZE; row++){
for(int col = 0; col < BOARD_SIZE; col++){
board[row][col] = (row * BOARD_SIZE) + (col + 1);
if(board[row][col] == pow(BOARD_SIZE, 2)){
row_space = row;
col_space = col;
board[row][col] = 0;
printf(" * ");
}
else
printf("%2d ", board[row][col]);
}
printf("\n");
}
printf("\n");
print_board_info();
}
void draw_board(){
system("cls");
print_instruction();
for(int row = 0; row < BOARD_SIZE; row++){
for(int col = 0; col < BOARD_SIZE; col++){
if(board[row][col] == 0){
printf(" * ");
}
else
printf("%2d ", board[row][col]);
}
printf("\n");
}
printf("\n");
print_board_info();
}
void get_input(){
char input = getch();
// This checks for Arrow key input.
// getch() function return two key codes for Arrow keys which are -32 and then 72, 80, 75, or 77.
// Thus, use getch() two times: one for checking for -32 and another for one of the four directions.
if (input == -32) {
input = getch();
switch(input) { // the real value
case 72:
// code for arrow up
shift("up");
break;
case 80:
// code for arrow down
shift("down");
break;
case 75:
// code for arrow left
shift("left");
break;
case 77:
// code for arrow right
shift("right");
break;
default: {
printf("INVALID INPUT\n");
return;
}
}
draw_board();
}
// Receive normal input
else if(input == 'q' || input == 'Q')
isEnd = true;
else if(input == 'r' || input == 'R')
reset_board();
else if(input == 's' || input == 'S')
shuffle();
}
void shift(char direction[5] /* up, down, left, right */){
direction = strlwr(direction);
if(direction == "up"){
if(row_space >= 0 && row_space < BOARD_SIZE-1){
swap(row_space, col_space, row_space + 1, col_space);
}
}
else if(direction == "down"){
if(row_space > 0 && row_space < BOARD_SIZE){
swap(row_space, col_space, row_space - 1, col_space);
}
}
else if(direction == "left"){
if(col_space >= 0 && col_space < BOARD_SIZE-1){
swap(row_space, col_space, row_space, col_space + 1);
}
}
else if(direction == "right"){
if(col_space > 0 && col_space < BOARD_SIZE){
swap(row_space, col_space, row_space, col_space - 1);
}
}
else {
printf("INVALID DIRECTION.\n");
return;
}
}
void swap(int row1, int col1, int row2, int col2){
int temp = board[row1][col1];
board[row1][col1] = board[row2][col2];
board[row2][col2] = temp;
if(board[row1][col1] == 0){
row_space = row1;
col_space = col1;
}
if(board[row2][col2] == 0){
row_space = row2;
col_space = col2;
}
}
void shuffle(){
// Use current time as seed for random generator
srand(time(0));
for(int i = 0; i < NUMBER_OF_SHUFFLE; i++){
switch(rand()%4) {
case 0:
shift("up");
break;
case 1:
shift("down");
break;
case 2:
shift("left");
break;
case 3:
shift("right");
break;
}
}
draw_board();
}