-
Notifications
You must be signed in to change notification settings - Fork 0
/
Life.h
113 lines (82 loc) · 3.23 KB
/
Life.h
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
#ifndef LIFE_H
#define LIFE_H
#include <iostream>
#include <utility>
#include "Map.h"
#include "Set.h"
class Life {
//OVERVIEW: object representing the Game of Life
public:
// EFFECTS: creates an empty life object
Life();
//EFFECTS: progresses the automaton by one step
void progress();
//REQUIRES: is contains a pattern in RLE format
//MODIFIES: is
//EFFECTS: adds the pattern contained in the given input stream to the
// grid; the top left corner of the pattern is placed at (x, y)
void add_rle(std::istream &is, int x, int y);
//EFFECTS: adds a living cell at (x, y), if there isn't one there already
void add(int x, int y);
//EFFECTS: returns true if there's a living cell at (x, y), false otherwise
bool get(int x, int y) const;
//EFFECTS: returns the position of the living cell with the smallest
// x and y coordinates (the top left corner of the grid)
std::pair<int, int> min_pos() const;
//EFFECTS: returns the position of the living cell with the largest
// x and y coordinates (the bottom right corner of the grid)
std::pair<int, int> max_pos() const;
//EFFECTS: returns the width of the grid (max_x - min_x)
int width() const;
//EFFECTS: returns the height of the grid (max_y - max_y)
int height() const;
// EFFECTS: returns the number of living cells in the grid
std::size_t size() const;
//MODIFIES: os
//EFFECTS : prints this object to the given output stream
void print(std::ostream &os) const;
private:
// key type
struct Pos {
int x;
int y;
bool operator==(Pos other) const {
return x == other.x && y == other.y;
}
};
// hash functor for Pos
struct PosHash {
std::size_t operator()(Pos pos) const;
};
int minx; // x-coordinate of min pos
int miny; // y-coordinate of min pos
int maxx; // x-coordinate of max pos
int maxy; // y-coordinate of max pos
Set<Pos, PosHash> set1; // set 1
Set<Pos, PosHash> set2; // set 2
Set<Pos, PosHash> *grid; // the set that this points to contains the
// state of the life object
Set<Pos, PosHash> *aux; // the set that this points to is used for auxiliary
// calculations
Map<Pos, int, PosHash> counter; // keeps track of counts
//EFFECTS: updates the coordinates of the min and max positions, if the
// given coordinates has values which exceed the current ones
void update_minmax(Pos pos);
//EFFECTS: returns the number of living cells neighboring the the cell
// at the given position
int grid_get_num_neighbors(Pos pos) const;
//REQUIRES: pos points to a living cell
//EFFECTS: inserts the cell at the given position into *aux if it has
// exactly three living neighbors
void grid_update_cell(Pos pos);
//EFFECTS: inserts into counter the key-value pair (pos, 1) if pos is not
// already in counter, otherwise increments the integer mapped
// to by pos by one
void counter_increment_cell(Pos pos);
//REQUIRES: pos points to a living cell
//EFFECTS: increments the counts of all cells neighboring the cell at the
// given position
void counter_increment_neighbors(Pos pos);
};
std::ostream & operator<<(std::ostream &os, Life &life);
#endif