Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix game of life #229

Merged
merged 7 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions examples/game_of_life/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
[package]
name = "game_of_life"
version = "0.1.1"
version = "0.1.3"
authors = ["Diego Cardoso <[email protected]>",
"Ilya Bogdanov <[email protected]"]
"Ilya Bogdanov <[email protected]",
"Junjie Huang <[email protected]>"]

[dependencies]
# Temporary check
rand = { git = "https://github.com/rust-lang-nursery/rand", features = ["stdweb"] }
#rand = { version = "0.5.0-pre.1", features = ["stdweb"] }
log = "0.4"
web_logger = "0.1"
yew = { path = "../.." }
rand = "0.4.1"
21 changes: 12 additions & 9 deletions examples/game_of_life/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![recursion_limit="128"]

#[macro_use]
extern crate yew;
extern crate rand;
#[macro_use] extern crate log;
extern crate web_logger;
#[macro_use] extern crate yew;

use yew::prelude::*;
use std::time::Duration;
use rand::Rng;
use yew::prelude::*;
use yew::services::Task;
use yew::services::interval::IntervalService;

Expand Down Expand Up @@ -72,7 +74,7 @@ fn wrap(coord: isize, range: isize) -> usize {
impl GameOfLife {
pub fn random_mutate(&mut self) {
for cellule in self.cellules.iter_mut() {
if rand::random() {
if rand::thread_rng().gen() {
cellule.set_alive();
} else {
cellule.set_dead();
Expand Down Expand Up @@ -168,27 +170,28 @@ where
match msg {
Msg::Random => {
self.random_mutate();
println!("Random");
info!("Random");
},
Msg::Start => {
let callback = env.send_back(|_| Msg::Step);
let handle = env.as_mut().spawn(Duration::from_millis(200), callback);
let interval: &mut IntervalService = env.as_mut();
let handle = interval.spawn(Duration::from_millis(200), callback);
self.job = Some(Box::new(handle));
println!("Start");
info!("Start");
},
Msg::Step => {
self.step();
},
Msg::Reset => {
self.reset();
println!("Reset");
info!("Reset");
},
Msg::Stop => {
if let Some(mut task) = self.job.take() {
task.cancel();
}
self.job = None;
println!("Stop");
info!("Stop");
},
Msg::ToggleCellule(idx) => {
self.toggle_cellule(idx);
Expand Down
10 changes: 10 additions & 0 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
extern crate yew;
extern crate game_of_life;

#[macro_use] extern crate log;
extern crate web_logger;

use yew::prelude::*;
use yew::services::interval::IntervalService;
use game_of_life::{GameOfLife, Msg};



struct Context {
interval: IntervalService,
}
Expand All @@ -16,7 +21,12 @@ impl AsMut<IntervalService> for Context {
}

fn main() {
web_logger::init();

trace!("Initializing yew...");
yew::initialize();

trace!("Creating a context...");
let context = Context {
interval: IntervalService::new(),
};
Expand Down
2 changes: 1 addition & 1 deletion examples/game_of_life/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link rel="shortcut icon" href="favicon.ico">
</head>
<body>
<script src="js/app.js"></script>
<script src="game_of_life.js"></script>
</body>
</html>