-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·173 lines (155 loc) · 4.4 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include "person.h"
Person addperson(int ID, int wantsID, int wantsID2, int avoidID, int holding);
enum boolean checkdup(int index, int list[]);
Person addobject(int ID);
enum boolean evaluatemap(int r, int c, Person map[][cols]);
/* Function maximum definition */
/* x, y and z are parameters */
int maximum(int x, int y, int z) {
int max = x; /* assume x is the largest */
if (y < max) { /* if y is larger than max, assign y to max */
max = y;
} /* end if */
if (z < max) { /* if z is larger than max, assign z to max */
max = z;
} /* end if */
return max; /* max is the largest value */
} /* end function maximum */
int main()
{
setvbuf (stdout, NULL, _IONBF, 0);
Person map[rows][cols]={
{addobject(-3),addperson(1,-2,0,3,6),addperson(2,0,0,6,0),addobject(-4)},
{addobject(-1),addperson(3,-4,0,0,0),addperson(4,5,0,0,0),addobject(-3)},
{addobject(-2),addobject(-2),addperson(5,-3,0,0,0),addobject(-1)}
};
enum boolean solved = false;
int numberofswaps = 7;
int randomlistsize = numberofswaps*4;
int randomlist[randomlistsize];
int counter = 0;
int counterlimit = 5;
while(solved == false && counter < counterlimit){
for(int i = 0; i<randomlistsize; i++){
if (i % 2 == 0){
randomlist[i] = rand() % rows;
}
else{
randomlist[i] = rand() % cols;
while(map[randomlist[i-1]][randomlist[i]].canmove == false){
randomlist[i-1] = rand() % rows;
randomlist[i] = rand() % cols;
}
}
}
for(int i =0; i<randomlistsize;i++){
printf("%d",randomlist[i]);
}
// now random swapping
for(int i = 0; i<randomlistsize; i+=4){
Person temp = map[i][i+1];
map[i][i+1] = map[i+2][i+3];
map[i+2][i+3] = temp;
}
// evaluate
Person map2[rows][cols]={
{addobject(-3),addperson(2,0,0,6,0),addperson(3,-4,0,0,0),addobject(-4)},
{addobject(-1),addperson(4,5,0,0,0),addperson(5,-3,0,0,0),addobject(-3)},
{addobject(-2),addobject(-2),addperson(1,-2,0,3,6),addobject(-1)}
};
int evaluation = evaluatemap(rows, cols, map2);
if(evaluation == true){
printf("Done at %d iterations",counter);
}
solved = evaluation;
if(counter == counterlimit -1){
printf("Failed");
}
counter++;
}
return EXIT_SUCCESS;
}
Person addperson(int ID, int wantsID, int wantsID2, int avoidID, int holding){
Person addee;
addee.ID = ID;
addee.wantsID = wantsID;
addee.wantsID2 = wantsID2;
addee.avoidID = avoidID;
addee.canmove = true;
addee.holding = holding;
return addee;
}
enum boolean checkdup(int index, int list[]){
enum boolean result = false;
for(int i = 0; i < index-1; i+=2)
{
if(list[index-1]==list[i] && list[index]==list[i+1])
{
result = true;
}
}
return result;
}
Person addobject(int ID)
{
Person addee;
addee.ID = ID;
addee.wantsID = 0;
addee.wantsID2 = 0;
addee.avoidID = 0;
addee.canmove = false;
addee.holding = 0;
return addee;
}
enum boolean evaluatemap(int r, int c, Person map[][cols]){
enum boolean result = true;
int counter1=0;
int counter2=0;
int counter3=0;
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(map[i][j].canmove == true){
int checklist[4][2]={
{(i+1),j},
{(i-1),j},
{i,(j+1)},
{i,(j-1)}
};
int avoidCheck = true;
enum boolean IDcheck = false;
enum boolean test1 = false;
enum boolean test2 = false;
for(int k=0; k<4; k++){
int a = checklist[k][0];
int b = checklist[k][1];
enum boolean test3 = false;
if(a>0 && b>0){
// check wantsID to ID of nextto
if(map[a][b].ID == map[i][j].wantsID){
test1 = true;
}
if((map[i][j].wantsID2 == 0) || (map[a][b].ID == map[i][j].wantsID2)){
test2 = true;
}
if((map[i][j].avoidID != map[a][b].ID) && (map[i][j].avoidID == 0 || map[i][j].avoidID != map[a][b].holding)){
test3 = true;
}
// avoid check is definately right
if(test3 == false){
avoidCheck = false;
}
}
}
if(test1== true && test2 == true){
IDcheck = true;
}
if(avoidCheck == true && IDcheck == true){
result = true;
}
}
}
}
return result;
}