Add example files
This commit is contained in:
1
examples/puzzle_game/.gitignore
vendored
Normal file
1
examples/puzzle_game/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
11
examples/puzzle_game/Cargo.toml
Normal file
11
examples/puzzle_game/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "puzzle_game"
|
||||
version = "0.1.0"
|
||||
authors = ["Nathan Stocks <nathan@agileperception.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
puzzles = { path = "../puzzles" }
|
||||
log = "0.4"
|
||||
env_logger = "0.8"
|
||||
1
examples/puzzle_game/puzzle.dat
Normal file
1
examples/puzzle_game/puzzle.dat
Normal file
@@ -0,0 +1 @@
|
||||
this is a puzzle
|
||||
21
examples/puzzle_game/src/main.rs
Normal file
21
examples/puzzle_game/src/main.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use anyhow::{Context, Result};
|
||||
use log::info;
|
||||
use puzzles::Puzzle;
|
||||
use std::fs::File;
|
||||
|
||||
fn get_puzzle(filename: &str) -> Result<Puzzle> {
|
||||
let fh = File::open(filename)
|
||||
.with_context(|| format!("couldn't open the puzzle file {}", filename))?;
|
||||
let puzzle = Puzzle::from_file(fh).context("couldn't convert data into a puzzle")?;
|
||||
Ok(puzzle)
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
env_logger::init();
|
||||
let puzzle = match get_puzzle("puzzle.dat").context("Couldn't get the first puzzle") {
|
||||
Ok(p) => p,
|
||||
Err(_) => Puzzle::new(),
|
||||
};
|
||||
info!("Playing puzzle: {}", puzzle.name);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user