View difference between Paste ID: h5cuvm1P and zPcMFMG8
SHOW: | | - or go back to the newest paste.
1
struct Point {
2
    x:  int,
3
    y:  int
4
}
5
6
impl Point {
7
    fn neighbors() -> ~[Point] {
8
        return ~[Point { x: self.x - 1, y: self.y - 1 },
9
                 Point { x: self.x    , y: self.y - 1 },
10
                 Point { x: self.x + 1, y: self.y - 1 },
11
                 Point { x: self.x + 1, y: self.y     },
12
                 Point { x: self.x + 1, y: self.y + 1 },
13
                 Point { x: self.x    , y: self.y + 1 },
14
                 Point { x: self.x - 1, y: self.y + 1 },
15
                 Point { x: self.x - 1, y: self.y     }];
16
    }
17
}
18
19
struct LifeMap {
20
    width:  int,
21
    height: int,
22
    data:   ~[mut bool]
23
}
24
25
fn LifeMap(width: int, height: int) -> ~LifeMap {
26
    return ~LifeMap {
27
        width:  width,
28
        height: height,
29
        data:   vec::to_mut(vec::from_elem((width * height) as uint, false))
30
    };
31
}
32
33
impl LifeMap {
34
    fn get(p: &Point) -> bool {
35
        let wx = (p.x + self.width) % self.width;
36
        let wy = (p.y + self.height) % self.height;
37
38
        return self.data[(wy * self.width) + wx];
39
    }
40
41
    fn set(p: &Point, v: bool) {
42
        let wx = (p.x + self.width) % self.width;
43
        let wy = (p.y + self.height) % self.height;
44
45
        self.data[(wy * self.width) + wx] = v;
46
    }
47
48
    fn live_neighbors(p: &Point) -> int {
49
        let mut count = 0;
50
51
        for vec::each(p.neighbors()) |n| {
52
            if self.get(n) {
53
                count += 1;
54
            }
55
        }
56
57
        return count;
58
    }
59
60
    fn survives(p: &Point) -> bool {
61
        let n = self.live_neighbors(p);
62
63
        if self.get(p) {
64
            match n {
65
                2 | 3 => true,
66
                _ => false
67
            }
68
        } else {
69
            match n {
70
                3 => true,
71
                _ => false
72
            }
73
        }
74
    }
75
76
    fn render() {
77
        for int::range(0, self.height) |y| {
78
            for int::range(0, self.width) |x| {
79-
                if self.get(&Point { x: x, y: y }) {
79+
                match self.get(&Point { x: x, y: y }) {
80-
                    io::print("*");
80+
                    true => io::print("*"),
81-
                } else {
81+
                    false => io::print("-")
82-
                    io::print("-");
82+
83
            }
84
            io::print("\n");
85
        }
86
        io::print("\n");
87
    }
88
}
89
90
fn evolve(curr: &LifeMap, next: &LifeMap) {
91
    for int::range(0, curr.height) |y| {
92
        for int::range(0, curr.width) |x| {
93
            let p = Point { x: x, y: y };
94
            match curr.survives(&p) {
95-
            if curr.survives(&p) {
95+
                true => next.set(&p, true),
96-
                next.set(&p, true);
96+
                false => next.set(&p, false)
97-
            } else {
97+
98-
                next.set(&p, false);
98+
99
    }
100
}
101
102
fn main() {
103
    let mut curr = LifeMap(4, 4);
104
    let mut next = LifeMap(4, 4);
105
    
106
    curr.set(&Point { x: 1, y: 1 }, true);
107
    curr.set(&Point { x: 2, y: 1 }, true);
108
    curr.set(&Point { x: 3, y: 1 }, true);
109
110
    for 4.times {
111
        curr.render();
112
        evolve(curr, next);
113
        curr <-> next;
114
    }
115
}