Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Created September 29, 2012 20:06
Show Gist options
  • Save JohnMurray/3805064 to your computer and use it in GitHub Desktop.
Save JohnMurray/3805064 to your computer and use it in GitHub Desktop.
gist for blog entry on johnmurray.io
# get all of the necessary tools to build
sudo yum install gcc-c++ llvm llvm-devel perl wget
# following rust-lang.org's instructions
wget http://dl.rust-lang.org/dist/rust-0.3.tar.gz
tar -xzf rust-0.3.tar.gz
cd rust-0.3
./configure
make -j 4 && make install
fn main(args: ~[str]) {
io::println("hello world from '" + args[0] + "'!");
}
use std;
import comm::{listen, methods};
import task::spawn;
import iter::repeat;
import rand::{seeded_rng, seed};
import uint::range;
import io::println;
fn main() {
// Open a channel to receive game results
do listen |result_from_game| {
let times = 10;
let player1 = "graydon";
let player2 = "patrick";
for repeat(times) {
// Start another task to play the game
do spawn |copy player1, copy player2| {
let outcome = play_game(player1, player2);
result_from_game.send(outcome);
}
}
// Report the results as the games complete
for range(0, times) |round| {
let winner = result_from_game.recv();
println(#fmt("%s wins round #%u", winner, round));
}
}
fn play_game(player1: str, player2: str) -> str {
// Our rock/paper/scissors types
enum gesture {
rock, paper, scissors
}
let rng = seeded_rng(seed());
// A small inline function for picking an RPS gesture
let pick = || (~[rock, paper, scissors])[rng.gen_uint() % 3];
// Pick two gestures and decide the result
alt (pick(), pick()) {
(rock, scissors) | (paper, rock) | (scissors, paper) { copy player1 }
(scissors, rock) | (rock, paper) | (paper, scissors) { copy player2 }
_ { "tie" }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment