-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.cpp
111 lines (102 loc) · 2.23 KB
/
cell.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 <unordered_map>
#include <stdio.h>
#include <iostream>
#include <list>
#include <ctime>
using namespace std;
class cell
{
public:
int row;
int column;
cell *north;
cell *east;
cell *south;
cell *west;
vector<cell*> link_vec;
cell(int r, int c)
{
row = r;
column = c;
}
void link(cell *c, bool bidi=true)
{
link_vec.push_back(c);
if(bidi)
{
(*c).link(this, false);
}
}
void unlink(cell *c, bool bidi=true)
{
vector<cell*>:: iterator itr;
for(itr = link_vec.begin(); itr != link_vec.end(); ++itr)
{
if((*(*itr)).isEqual(c))
{
link_vec.erase(itr);
}
}
if(bidi)
{
(*c).unlink(this, false);
}
}
void links()
{
vector<cell*>:: iterator itr;
for (itr = link_vec.begin(); itr != link_vec.end(); ++itr)
{
cout << (*(*itr)).row << " " << (*(*itr)).column<< endl;
}
}
bool linked(cell *c)
{
vector<cell*>:: iterator itr;
for (itr = link_vec.begin(); itr != link_vec.end(); ++itr)
{
if((*(*itr)).isEqual(c))
{
return true;
}
}
return false;
}
vector<cell*> neighbors()
{
vector<cell*> n;
if((*north).row > 0 && (*north).column > 0)
{
n.push_back(north);
}
if((*east).row > 0 && (*east).column > 0)
{
n.push_back(east);
}
if((*south).row > 0 && (*south).column > 0)
{
n.push_back(south);
}
if((*west).row > 0 && (*west).column > 0)
{
n.push_back(west);
}
return n;
}
cell* rand_neighbor()
{
vector<cell*> n = neighbors();
return n[rand()%n.size()];
}
bool isEqual(cell *c)
{
return (row == (*c).row && column == (*c).column);
/*{
return true;
}
else
{
return false;
}*/
}
};