-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
219 lines (180 loc) · 5.07 KB
/
main.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
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
/*
* File: main.cpp
* Author: maltepagel
*/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;
#include "KnightPosition.h"
#include "PositionRepair.h"
#define ASK_USER 0
#define COL_MIN 5
#define COL_MAX 16
#define DEFAULT_COLS 8
void ask_user(void);
void show_solution(KnightPosition*, int, int);
char letters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'};
int cols;
int column;
int row;
void ask_user() {
int i;
char c;
cout << endl;
cout << "How many rows resp. columns (" << COL_MIN << " - " << COL_MAX << ")?" << endl;
scanf("%d", &i);
if (i < COL_MIN || i > COL_MAX) {
cout << "Invalid value! Taking " << DEFAULT_COLS << endl;
cols = DEFAULT_COLS;
} else
cols = i;
cout << "Start in which column ('A' to '" << (letters[cols - 1]) << "')?" << endl;
scanf("%s", &c);
i = (int) c - 97;
if (i >= 0 && i < cols)
column = i;
else {
i = (int) c - 65;
if (i >= 0 && i < cols)
column = i;
else {
column = rand() % cols;
cout << "Invalid value! Taking " << letters[column] << endl;
}
}
cout << "Start in which row (1 to " << cols << ")?" << endl;
scanf("%d", &i);
if (i > 0 && i <= cols)
row = i - 1;
else {
row = rand() % cols;
cout << "Invalid value! Taking " << (row + 1) << endl;
}
}
/**
* Preparing show,
* eventually cleaning memory
*/
void show_solution(KnightPosition * start_position, int delete_all, int closed_path) {
int i, j;
int ** show_collection = new int*[cols];
for (i = 0; i < cols; i++) {
show_collection[i] = new int[cols];
for (j = 0; j < cols; j++)
show_collection[i][j] = 0;
}
i = 0;
KnightPosition * position = start_position;
KnightPosition * position_to_delete;
do {
show_collection[cols - position->row - 1][position->column] = ++i;
position_to_delete = position;
if (position->next_position)
position = position->next_position;
if (delete_all)
delete(position_to_delete);
} while (position->next_position);
if (delete_all)
delete(position);
show_collection[cols - position->row - 1][position->column] = ++i;
/**
* Dont' blame me for not using 'cout' consequently -
* formatted output is much more readable
*/
if (closed_path)
for (i = 0; i < cols; i++)
cout << "******";
cout << endl;
for (i = 0; i < cols; i++) {
if (closed_path)
cout << "* ";
printf("%2d", (cols - i));
cout << " |";
for (j = 0; j < cols; j++)
printf("%5d", show_collection[i][j]);
if (closed_path)
cout << " *";
cout << endl;
}
if (closed_path)
cout << "* ";
cout << " ";
for (i = 0; i < cols; i++)
cout << "-----";
if (closed_path)
cout << "*";
cout << endl;
if (closed_path)
cout << "* ";
printf("%4s", " ");
for (i = 0; i < cols; i++)
printf("%5c", letters[i]);
if (closed_path)
cout << " *";
cout << endl;
if (closed_path) {
for (i = 0; i < cols; i++)
cout << "******";
cout << endl;
}
}
int main (int argc, char** argv) {
KnightPosition * start_position;
KnightPosition *** complete_collection;
int i, j;
time_t t;
time(&t);
srand((unsigned int) t);
if (ASK_USER)
ask_user();
else {
cols = DEFAULT_COLS;
column = rand() % cols;
row = rand() % cols;
}
/**
* Matrix of pointers needed for a moment
*/
complete_collection = new KnightPosition**[cols];
for (i = 0; i < cols; i++)
complete_collection[i] = new KnightPosition*[cols];
for (i = 0; i < cols; i++)
for (j = 0; j < cols; j++)
complete_collection[i][j] = NULL;
/**
* 'start_position' is - no one would ever guess this - the field where to start,
* temporarily stored at [column][row] in 'complete_collection'
*/
start_position = new KnightPosition(column, row, cols, complete_collection);
/**
* All fields should be initialized; matrix not needed any longer
*/
delete[](complete_collection);
/**
* Setting a knight on 'start_position' and invoking path search
*/
start_position->set_knight_and_start_search(NULL);
/**
* If finding closed path is impossible: show result;
* otherwise: search for closed path
*/
if (KnightPosition::failed || ((cols * cols) % 2 != 0))
show_solution(start_position, 1, 0);
else
start_position->search_last_position(start_position, &show_solution);
//show_solution(start_position, 1, 0);
/**
* The result should be visible also on windows systems
*/
#ifdef _WIN32
char c = 'n';
while (c != 'y') {
cout << "Quit application [y/n]?" << endl;
scanf("%s", &c);
}
#endif
return 0;
}