-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.c
67 lines (54 loc) · 1.58 KB
/
cell.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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "cell.h"
cell init_cell( int y, int x, int size ) {
cell new_cell = (cell) { .y = y, .x = x, .visited = 0 };
// new_cell.id = y * size + x;
new_cell.value = 0.0;
new_cell.total_value = 1000; // ="nieskończoność"
new_cell.label = ' ';
for ( int i = 0; i < 4; ++i )
new_cell.bounds[i] = 1;
return new_cell;
}
/* dajemy wskaźniki do sąsiadów c */
void init_nears( cell *c, cell *t, cell *r, cell *b, cell *l ) {
for ( int i = 0; i < 4; ++i)
c->near[i] = malloc( sizeof( cell ) );
c->near[0] = t;
c->near[1] = r;
c->near[2] = b;
c->near[3] = l;
}
/*
zwraca losowego, nieodwiedzonego sąsiada komórki c
lub NULL gdy takiej komórki nie ma
*/
cell *leave( cell *c ) {
cell *next = NULL;
cell *reloaded[4];
int ids [4];
int count = 0;
/* reloaded zawiera wszystkich nieodwiedzonych sąsiadów, count - ile tych sąsiadków jest */
for ( int i = 0; i < 4; ++i ) {
reloaded[i] = malloc( sizeof(cell ) );
if ( c->near[i] != NULL && c->near[i]->visited == 0 ) {
ids[count] = i;
reloaded[ count++ ] = c->near[i];
}
}
/* oops - nie ma takich sąsiadów */
if ( count == 0 ) {
return NULL;
}
/* zwrot losowego sąsiada */
int rnd_index = rand() % count;
next = reloaded[ rnd_index ]; // 0 1 2 3
// c->near[ ids[rnd_index] ] = NULL;
for ( int i = 0; i < 4; ++i)
free( reloaded[i] );
return next;
}