-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.c
162 lines (148 loc) · 3.43 KB
/
puzzle.c
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
#include "puzzsup.h"
int boardSize = 0;
struct position *ptrS = &space; //Pointer to space in board.
int main(){
setBoard(); // Make the board ready.
suffelBoard(); //Suffel the ready made board randomly.
int j = 1,i;
for(i=0;j;i++){
printScrn();
j = toMove();
if(gameCheck()){
printf("\n PUZZLE SOLVED! \n");
break;
}
}
return 0;
}
void suffelBoard(){
initRandom();
moveRandom();
return;
}
void setBoard(){
printf("Enter your squire puzzles size: N By N :\n");
printf("Minimum 2. Maximum is 5.\n");
fflush(stdin);
scanf("%d", &boardSize);
if(boardSize < 2 || boardSize > 5){
printf("Invalid option. Press any key.");
getchar();
return ;
}
makePuzzle(); // Make the puzzle board.
return;
}
void makePuzzle(){
char alp = 'A';
for(int i = 0;i < boardSize;i++){
for(int j = 0;j < boardSize;j++){
board[i][j] = alp++;
}
}
board[boardSize - 1][boardSize - 1] = ' ';
ptrS->x = boardSize - 1;
ptrS->y = boardSize - 1;
return;
}
void moveRandom(){
for(int i = 0;i < 100;i++){
int m = (rand() % (4)) + 1;
move(m);
}
return;
}
void initRandom(){
srand((unsigned)time(NULL));
return;
}
int gameCheck(){
char ch = 'A';
for(int i = 0, k = 1;i < boardSize;i++){
for(int j = 0;j < boardSize;j++){
k++;
if(board[i][j] == ch){
ch++;
if(k == (boardSize * boardSize)){
ch = ' ';
}
} else{
return 0;
}
}
}
printScrn();
return 1;
}
void move(int dir){
char temp = 0;
switch(dir){
case 2:
if((ptrS->y) - 1 < 0){ return; }
temp = board[(ptrS->y) - 1][ptrS->x];
board[(ptrS->y) - 1][ptrS->x] = board[ptrS->y][ptrS->x];
board[ptrS->y][ptrS->x] = temp;
ptrS->y -= 1;
break;
case 1:
if((ptrS->y) + 1 > boardSize - 1){ return; }
temp = board[ptrS->y + 1][ptrS->x];
board[(ptrS->y) + 1][ptrS->x] = board[ptrS->y][ptrS->x];
board[ptrS->y][ptrS->x] = temp;
ptrS->y += 1;
break;
case 4:
if((ptrS->x) - 1 < 0){ return; }
temp = board[ptrS->y][(ptrS->x) - 1];
board[ptrS->y][(ptrS->x) - 1] = board[ptrS->y][ptrS->x];
board[ptrS->y][ptrS->x] = temp;
ptrS->x -= 1;
break;
case 3:
if((ptrS->x) + 1 > boardSize - 1){ return; }
temp = board[ptrS->y][(ptrS->x) + 1];
board[ptrS->y][(ptrS->x) + 1] = board[ptrS->y][ptrS->x];
board[ptrS->y][ptrS->x] = temp;
ptrS->x += 1;
default:
break;
}
printScrn();
return;
}
int toMove(){
enableRawMode();
char ch;
ch = readkeys();
switch(ch){
case 'A':
move(FORWARD);
break;
case 'B':
move(BACKWARD);
break;
case 'D':
move(LEFT);
break;
case 'C':
move(RIGHT);
break;
case 'q':
return 0;
break;
}
return 1;
}
void printScrn(){
system("clear");
printf("Press q to exit\n");
printf("\n *************************\n");
for(int i = 0;i < boardSize;i++){
for(int j = 0;j < boardSize;j++){
printf(" %c ", board[i][j]);
}
printf("\n");
}
printf("\n *************************\n");
return;
}