-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminMoves.cpp
111 lines (91 loc) · 4.23 KB
/
minMoves.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
#include "minMoves.h"
char table[17][18] = { {'8', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'}, //18x17
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{'7', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{'6', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{'5', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{'4', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{'3', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{'2', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{'1', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '-', '+', '-', '+', '-', '+', '-', '|'},
{' ', '|', '1', '|', '2', '|', '3', '|', '4', '|', '5', '|', '6', '|', '7', '|', '8', '|'} };
Field* FindMinimum(int startPosition[2], int finalPosition[2]) {
int xOsa[] = {-2,-1,1,2,-2,-1,1,2};
int yOsa[] = {-1,-2,-2,-1,1,2,2,1};
vector<Field*> possiblePositions; // all the fields where the knight can go
possiblePositions.push_back(new Field(startPosition[0], startPosition[1],0,NULL));
bool visitedFields[9][9]; // fields where the knight has already been
for(int i=1;i<9;i++) { // we initialize all fields as unvisited
for(int j=1;j<9;j++) {
visitedFields[i][j] = false;
}
}
visitedFields[startPosition[0]][startPosition[1]] = true; // we mark the starting Field as visited
Field* current;
int x,y;
while(!possiblePositions.empty()) {
current = possiblePositions.front();
possiblePositions.erase(possiblePositions.begin());
if(current->xCoordinate == finalPosition[0] && current->yCoordinate == finalPosition[1]) { //we check if we have reached the target Field
return current;
possiblePositions.clear();
}
for(int i=0;i<8;i++) {
x = current->xCoordinate + xOsa[i];
y = current->yCoordinate + yOsa[i];
if(isInTable(x,y) && !visitedFields[x][y] ) {
visitedFields[x][y] = true;
possiblePositions.push_back(new Field(x,y,current->distance+1,current));
}
}
}
return NULL;
}
void PrintTable() {
for(int i=0;i<17;i++) {
for(int j=0;j<18;j++) {
cout << table[i][j];
}
cout << "\n";
}
}
bool isInTable(int x, int y) {
if(x<=8 && x>0 && y<=8 && y>0) return true;
return false;
}
void markField(int x, int y, char mark) {
switch(y) {
case 1:
y = 14;
break;
case 2:
y = 12;
break;
case 3:
y = 10;
break;
case 4:
y = 8;
break;
case 5:
y = 6;
break;
case 6:
y = 4;
break;
case 7:
y = 2;
break;
case 8:
y = 0;
break;
}
table[y][x*2] = mark;
}