Add example files

This commit is contained in:
Nathan Stocks
2021-06-12 22:36:24 -06:00
commit 26d8ef8451
20 changed files with 448 additions and 0 deletions

1
examples/puzzle_game/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

View 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"

View File

@@ -0,0 +1 @@
this is a puzzle

View 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(())
}