Cargo clippy fixes

Did cargo clippy fixes
Added github workflows
This commit is contained in:
Uttarayan Mondal
2021-03-15 17:30:10 +05:30
parent 8bc7770bf3
commit 5a9e2a85c1
11 changed files with 481 additions and 106 deletions
+45
View File
@@ -0,0 +1,45 @@
name: Rust Release Build
on:
push:
tags:
- 'v*.*.*'
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
override: true
- name: Build
run: cargo build --release
- name: Run tests
run: cargo test --verbose
- name: Strip
run: strip target/release/snake
- name: Compress
run: tar cvzf snake.tar.gz LICENSE target/release/snake
- name: Release
uses: softprops/action-gh-release@v1
with:
files: |
snake.tar.gz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+22
View File
@@ -0,0 +1,22 @@
name: Rust Build Test
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
-1
View File
@@ -1,2 +1 @@
/target /target
Cargo.lock
Generated
+229 -16
View File
@@ -1,5 +1,40 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "arrayref"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
[[package]]
name = "arrayvec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "blake2b_simd"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587"
dependencies = [
"arrayref",
"arrayvec",
"constant_time_eq",
]
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.66" version = "1.0.66"
@@ -8,26 +43,80 @@ checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "getrandom" name = "constant_time_eq"
version = "0.2.0" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "crossbeam-utils"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d"
dependencies = [ dependencies = [
"autocfg",
"cfg-if", "cfg-if",
"libc", "lazy_static",
"wasi",
] ]
[[package]] [[package]]
name = "libc" name = "dirs"
version = "0.2.81" version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi 0.10.1+wasi-snapshot-preview1",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929"
[[package]] [[package]]
name = "ncurses" name = "ncurses"
@@ -53,10 +142,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]] [[package]]
name = "rand" name = "proc-macro2"
version = "0.8.0" version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12" checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18519b42a40024d661e1714153e9ad0c3de27cd495760ceb09710920f1098b1e"
dependencies = [ dependencies = [
"libc", "libc",
"rand_chacha", "rand_chacha",
@@ -76,11 +183,11 @@ dependencies = [
[[package]] [[package]]
name = "rand_core" name = "rand_core"
version = "0.6.0" version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8b34ba8cfb21243bd8df91854c830ff0d785fff2e82ebd4434c2644cb9ada18" checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5"
dependencies = [ dependencies = [
"getrandom", "getrandom 0.2.2",
] ]
[[package]] [[package]]
@@ -92,16 +199,122 @@ dependencies = [
"rand_core", "rand_core",
] ]
[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "redox_users"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
dependencies = [
"getrandom 0.1.16",
"redox_syscall",
"rust-argon2",
]
[[package]]
name = "rust-argon2"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
dependencies = [
"base64",
"blake2b_simd",
"constant_time_eq",
"crossbeam-utils",
]
[[package]]
name = "serde"
version = "1.0.121"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6159e3c76cab06f6bc466244d43b35e77e9500cd685da87620addadc2a4c40b1"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.121"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3fcab8778dc651bc65cfab2e4eb64996f3c912b74002fb379c94517e1f27c46"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "snake" name = "snake"
version = "0.2.1" version = "0.2.1"
dependencies = [ dependencies = [
"dirs",
"ncurses", "ncurses",
"rand", "rand",
"serde",
"toml",
] ]
[[package]]
name = "syn"
version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93c6c3420963c5c64bca373b25e77acb562081b9bb4dd5bb864187742186cea9"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+5
View File
@@ -8,4 +8,9 @@ edition = "2018"
[dependencies] [dependencies]
ncurses = "5.100.0" ncurses = "5.100.0"
serde = { version = "1.0.121", features = ["derive"] }
# serde = "1.0.121"
# serde_derive = "1.0.121"
toml = "0.5.8"
rand = "0.8.0" rand = "0.8.0"
dirs = "3.0.1"
+65 -68
View File
@@ -1,32 +1,43 @@
extern crate rand; extern crate rand;
use crate::settings::Config;
use core::iter::Iterator; use core::iter::Iterator;
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::LinkedList; use std::collections::LinkedList;
use std::ops::Sub; use std::ops::Sub;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
#[derive(Debug)]
#[derive(Serialize, Deserialize, Clone, Copy)]
#[serde(tag = "type", content = "value")]
pub enum Difficulty {
Linear(f32),
Flat,
}
#[derive(Debug, Clone, Copy)]
pub enum Direction { pub enum Direction {
Up, Up,
Down, Down,
Left, Left,
Right, Right,
} }
impl Copy for Direction {}
impl Clone for Direction {
fn clone(&self) -> Self {
*self
}
}
impl PartialEq for Direction { impl PartialEq for Direction {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match (self, other) { // match (self, other) {
(&Self::Up, &Self::Up) => true, // (&Self::Up, &Self::Up) => true,
(&Self::Down, &Self::Down) => true, // (&Self::Down, &Self::Down) => true,
(&Self::Left, &Self::Left) => true, // (&Self::Left, &Self::Left) => true,
(&Self::Right, &Self::Right) => true, // (&Self::Right, &Self::Right) => true,
_ => false, // _ => false,
} // }
matches!(
(self, other),
(&Self::Up, &Self::Up)
| (&Self::Down, &Self::Down)
| (&Self::Left, &Self::Left)
| (&Self::Right, &Self::Right)
)
} }
} }
@@ -41,46 +52,29 @@ impl Direction {
} }
} }
#[derive(Debug)] #[derive(Debug)]
// impl std::fmt::Debug for Direction {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// // write!(f,)
// write!(
// f,
// "{}",
// match *self {
// Self::Up => (),
// _ => (),
// }
// );
// }
// }
pub enum CellType { pub enum CellType {
Food, Food,
Snake, Snake,
Empty, // Empty,
} }
#[derive(Debug)]
pub struct Cell { pub struct Cell {
line: u32, line: u32,
col: u32, col: u32,
ctype: CellType, _ctype: CellType,
} }
impl PartialEq for Cell { impl PartialEq for Cell {
fn eq(&self, other: &Cell) -> bool { fn eq(&self, other: &Cell) -> bool {
if (self.line == other.line) && (self.col == other.col) { (self.line == other.line) && (self.col == other.col)
return true;
} else {
return false;
}
} }
} }
impl Sub for Cell { impl Sub for Cell {
type Output = (i32, i32); type Output = (i32, i32);
fn sub(self, rhs: Cell) -> (i32, i32) { fn sub(self, rhs: Cell) -> (i32, i32) {
return ( (
self.line as i32 - rhs.line as i32, self.line as i32 - rhs.line as i32,
self.col as i32 - rhs.col as i32, self.col as i32 - rhs.col as i32,
); )
} }
} }
impl Cell { impl Cell {
@@ -88,7 +82,7 @@ impl Cell {
Cell { Cell {
line: l, line: l,
col: c, col: c,
ctype: t, _ctype: t,
} }
} }
pub fn random(lines: u32, cols: u32) -> Cell { pub fn random(lines: u32, cols: u32) -> Cell {
@@ -100,16 +94,16 @@ impl Cell {
) )
} }
pub fn posyx(&self) -> (u32, u32) { pub fn posyx(&self) -> (u32, u32) {
return (self.line, self.col); (self.line, self.col)
} }
pub fn posy(&self) -> u32 { pub fn posy(&self) -> u32 {
return self.line; self.line
} }
pub fn posx(&self) -> u32 { pub fn posx(&self) -> u32 {
return self.col; self.col
} }
pub fn chtype(&mut self, ctype: CellType) { pub fn _chtype(&mut self, ctype: CellType) {
self.ctype = ctype; self._ctype = ctype;
} }
pub fn is_adjacent(&self, other: &Cell) -> Option<Direction> { pub fn is_adjacent(&self, other: &Cell) -> Option<Direction> {
match *self - *other { match *self - *other {
@@ -135,17 +129,20 @@ impl Clone for Cell {
} }
} }
#[derive(Debug)]
enum FailState { enum FailState {
Wall, Wall,
Snake, Snake,
} }
#[derive(Debug)]
enum GameState { enum GameState {
Failed(FailState), Failed(FailState),
Ready, Ready,
// Playing, // Playing,
} }
#[derive(Debug)]
pub struct Board { pub struct Board {
maxlines: u32, maxlines: u32,
maxcols: u32, maxcols: u32,
@@ -165,10 +162,9 @@ impl Board {
} }
pub fn check_collision(&mut self, snake: &Snake) -> bool { pub fn check_collision(&mut self, snake: &Snake) -> bool {
let (snake_line, snake_col): (u32, u32) = snake.posyx(); let (snake_line, snake_col): (u32, u32) = snake.posyx();
if (snake_line >= self.maxlines - 1) if (snake_line >= self.maxlines - 1) || (snake_col >= self.maxcols - 1)
|| (snake_col >= self.maxcols - 1) // || (snake_line <= 0)
|| (snake_line <= 0) // || (snake_col <= 0)
|| (snake_col <= 0)
{ {
self.gamestate = GameState::Failed(FailState::Wall); self.gamestate = GameState::Failed(FailState::Wall);
return true; return true;
@@ -182,17 +178,13 @@ impl Board {
return true; return true;
} }
} }
return false; false
} }
pub fn check_food(&self, snake: &Snake) -> bool { pub fn check_food(&self, snake: &Snake) -> bool {
if self.food_posyx() == snake.posyx() { self.food_posyx() == snake.posyx()
return true;
} else {
return false;
}
} }
pub fn food_posyx(&self) -> (u32, u32) { pub fn food_posyx(&self) -> (u32, u32) {
return self.food.posyx(); self.food.posyx()
} }
// pub fn food_posy(&self) -> u32 { // pub fn food_posy(&self) -> u32 {
// return self.food.line; // return self.food.line;
@@ -205,11 +197,11 @@ impl Board {
pub fn spawn_food(&mut self, snake: &Snake) { pub fn spawn_food(&mut self, snake: &Snake) {
let mut food: Cell = Cell::random(self.maxlines, self.maxcols); let mut food: Cell = Cell::random(self.maxlines, self.maxcols);
let mut spawned_food = false; let mut spawned_food = false;
while spawned_food != true { while !spawned_food {
// check for colliosions with the snake body until a free spot is found and spawn the food there // check for colliosions with the snake body until a free spot is found and spawn the food there
spawned_food = true; spawned_food = true;
let mut snake_iter = snake.iter(); let snake_iter = snake.iter();
for snake_cell in snake_iter.next() { for snake_cell in snake_iter {
if *snake_cell == food { if *snake_cell == food {
// O(n) ; I think this is nessacary/ I don't know how to reduce the order // O(n) ; I think this is nessacary/ I don't know how to reduce the order
// if food collides with the snake body then set food to a new random position and set spawned food to false // if food collides with the snake body then set food to a new random position and set spawned food to false
@@ -228,26 +220,27 @@ pub struct Snake {
head: Cell, head: Cell,
body: LinkedList<Cell>, body: LinkedList<Cell>,
direction: Direction, direction: Direction,
difficulty: Difficulty,
grow: bool, grow: bool,
speed: u32, speed: u32,
last_tail: Option<Cell>, last_tail: Option<Cell>,
} }
impl Snake { impl Snake {
pub fn new(head: Cell) -> Snake { pub fn new(head: Cell, config: &Config) -> Snake {
let mut temp_body: LinkedList<Cell> = LinkedList::new(); let mut temp_body: LinkedList<Cell> = LinkedList::new();
temp_body.push_front(head); temp_body.push_front(head);
Snake { Snake {
head, head,
body: temp_body, body: temp_body,
// length: 1, difficulty: config.difficulty,
direction: Direction::Right, direction: Direction::Right,
grow: false, grow: true,
speed: 15, speed: config.speed,
last_tail: Some(head), last_tail: Some(head),
} }
} }
pub fn posyx(&self) -> (u32, u32) { pub fn posyx(&self) -> (u32, u32) {
return (self.head.line, self.head.col); (self.head.line, self.head.col)
} }
pub fn smove(&mut self, _direction: Direction) { pub fn smove(&mut self, _direction: Direction) {
// smove because move is already a keyword // smove because move is already a keyword
@@ -257,7 +250,7 @@ impl Snake {
} else { } else {
direction = _direction direction = _direction
} }
if self.grow == false { if !self.grow {
self.last_tail = Some(self.body.pop_back().unwrap()); self.last_tail = Some(self.body.pop_back().unwrap());
// self.grow = false; // self.grow = false;
@@ -285,16 +278,20 @@ impl Snake {
sleep(Duration::from_millis((1000 / self.speed) as u64)); sleep(Duration::from_millis((1000 / self.speed) as u64));
self.smove(self.direction); self.smove(self.direction);
} }
pub fn scale_difficulty(&mut self) {
match self.difficulty {
Difficulty::Flat => (),
Difficulty::Linear(scale) => self.speed = (self.speed as f32 * scale) as u32,
}
}
pub fn grow(&mut self) { pub fn grow(&mut self) {
self.scale_difficulty();
self.grow = true; self.grow = true;
} }
pub fn iter(&self) -> impl Iterator<Item = &Cell> { pub fn iter(&self) -> impl Iterator<Item = &Cell> {
return self.body.iter(); self.body.iter()
} }
pub fn remove(&self) -> Option<Cell> { pub fn remove(&self) -> Option<Cell> {
return self.last_tail; self.last_tail
}
pub fn set_speed(&mut self, speed: u32) {
self.speed = speed;
} }
} }
+8 -5
View File
@@ -27,6 +27,11 @@ pub fn destroy_window(win: WINDOW) {
delwin(win); // delete the window delwin(win); // delete the window
} }
pub fn clear_window(win: WINDOW) {
wmove(win, 0, 0);
wclrtobot(win);
}
pub fn draw_snake(snake: &Snake, game_win: WINDOW) { pub fn draw_snake(snake: &Snake, game_win: WINDOW) {
// let mut snake_iter = snake.iter(); // let mut snake_iter = snake.iter();
let (mut prev, mut current, _next): (&Cell, &Cell, &Cell); let (mut prev, mut current, _next): (&Cell, &Cell, &Cell);
@@ -45,12 +50,10 @@ pub fn draw_snake(snake: &Snake, game_win: WINDOW) {
prev.posx() as i32, prev.posx() as i32,
&format!("{}", std::char::from_u32(0x0298).unwrap_or('O')), &format!("{}", std::char::from_u32(0x0298).unwrap_or('O')),
); );
match snake.remove() { if let Some(tail) = snake.remove() {
Some(tail) => { mvwaddstr(game_win, tail.posy() as i32, tail.posx() as i32, " ");
mvwaddstr(game_win, tail.posy() as i32, tail.posx() as i32, " ");
}
None => (),
} }
let _current = snake_iter.next(); let _current = snake_iter.next();
current = match _current { current = match _current {
Some(cell) => cell, Some(cell) => cell,
+24 -15
View File
@@ -1,24 +1,28 @@
mod backend; mod backend;
mod frontend; mod frontend;
use crate::menu; use crate::menu;
use crate::settings::Config;
// use ncurses::*; // use ncurses::*;
pub use backend::Difficulty;
use backend::{Board, Cell, Snake}; use backend::{Board, Cell, Snake};
use ncurses::{ use ncurses::{
getmaxyx, nodelay, stdscr, wgetch, wrefresh, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_UP, WINDOW, getmaxyx, nodelay, stdscr, wgetch, wrefresh, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_UP, WINDOW,
}; };
pub fn start() { pub fn start(config: &Config) {
let (mut mlines, mut mcols): (i32, i32) = (0, 0); let (mut mlines, mut mcols): (i32, i32) = (0, 0);
let game_win: WINDOW; let game_win: WINDOW;
let mut ch: i32; let mut ch: i32;
let (vmargin, hmargin): (u32, u32) = (5, 10); let (vmargin, hmargin): (u32, u32) = (5, 10);
getmaxyx(stdscr(), &mut mlines, &mut mcols); getmaxyx(stdscr(), &mut mlines, &mut mcols);
game_win = frontend::game_window(mlines as u32, mcols as u32, vmargin, hmargin); game_win = frontend::game_window(mlines as u32, mcols as u32, vmargin, hmargin);
let mut snake = Snake::new(Cell::new( let mut snake = Snake::new(
mlines as u32 / 2 - vmargin, Cell::new(
mcols as u32 / 2 - hmargin, mlines as u32 / 2 - vmargin,
backend::CellType::Snake, mcols as u32 / 2 - hmargin,
)); //Initialise snake in the middle of the screen backend::CellType::Snake,
),
config,
); //Initialise snake in the middle of the screen
let mut board = Board::new(mlines as u32 - vmargin * 2, mcols as u32 - hmargin * 2); let mut board = Board::new(mlines as u32 - vmargin * 2, mcols as u32 - hmargin * 2);
nodelay(game_win, true); nodelay(game_win, true);
loop { loop {
@@ -46,15 +50,20 @@ pub fn start() {
//112 is keycode for 'p' //112 is keycode for 'p'
0 => (), //resume 0 => (), //resume
1 => { 1 => {
snake = Snake::new(Cell::new( snake = Snake::new(
mlines as u32 / 2, Cell::new(
mcols as u32 / 2, mlines as u32 / 2 - vmargin,
backend::CellType::Snake, mcols as u32 / 2 - hmargin,
)); //Initialise snake in the middle of the screen backend::CellType::Snake,
board = Board::new(mlines as u32 - vmargin * 2, mcols as u32 - hmargin * 2); ),
} //restart config,
); //Initialise snake in the middle of the screen
board = Board::new(mlines as u32 - vmargin * 2, mcols as u32 - hmargin * 2); //restart
frontend::clear_window(game_win);
}
2 => break, //exit 2 => break, //exit
_ => (), //other charachters just in case _ => (), //other charachters just in case
} }
wrefresh(game_win); wrefresh(game_win);
nodelay(game_win, true); nodelay(game_win, true);
+4 -1
View File
@@ -2,6 +2,7 @@ extern crate ncurses;
mod game; mod game;
mod highscore; mod highscore;
mod menu; mod menu;
mod settings;
// use game::{Cell, Snake}; // use game::{Cell, Snake};
// use ncurses::*; // use ncurses::*;
use ncurses::{ use ncurses::{
@@ -12,6 +13,7 @@ use ncurses::{
fn main() { fn main() {
// let (lines, cols): (i32, i32) = (0, 0); // let (lines, cols): (i32, i32) = (0, 0);
setlocale(LcCategory::all, ""); setlocale(LcCategory::all, "");
let config = settings::Config::new().unwrap();
initscr(); initscr();
raw(); raw();
keypad(stdscr(), true); keypad(stdscr(), true);
@@ -27,11 +29,12 @@ fn main() {
} }
loop { loop {
match menu::main_menu_control() { match menu::main_menu_control() {
0 => game::start(), 0 => game::start(&config),
1 => highscore::show(), 1 => highscore::show(),
_ => break, _ => break,
} }
} }
refresh(); refresh();
endwin(); endwin();
config.write();
} }
+77
View File
@@ -0,0 +1,77 @@
extern crate dirs;
extern crate serde;
// extern crate serde_derive;
extern crate toml;
use serde::{Deserialize, Serialize};
// use serde_derive::{Deserialize, Serialize};
use crate::game::Difficulty;
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::thread::sleep;
#[derive(Serialize, Deserialize)]
pub struct Config {
pub difficulty: Difficulty,
pub speed: u32,
}
impl Config {
pub fn new() -> Result<Self, std::io::Error> {
let config_path = Config::config_path().unwrap();
if config_path.exists() {
let mut config_file = File::open(config_path).unwrap();
let mut config_file_str: String = String::new();
config_file.read_to_string(&mut config_file_str).unwrap();
// let config: Config = toml::from_str(&config_file_str).expect("Error in config file");
let config: Config = match toml::from_str(&config_file_str) {
Ok(config) => config,
Err(e) => {
eprintln!(
"Error in parsing config file {} due to {}",
Config::config_path().unwrap().to_str().unwrap(),
e,
);
eprintln!("Falling back to using the default values");
sleep(std::time::Duration::from_millis(3000));
Config::default()
}
};
Ok(config)
} else {
Ok(Config::default())
}
}
fn default() -> Self {
Self {
difficulty: Difficulty::Flat, //Default set to ten blocks per second
speed: 10,
}
}
pub fn write(&self) {
let config_file_str: String = toml::to_string(self).unwrap();
let config_path = Config::config_path().unwrap();
let mut config_file: File =
File::create(config_path).expect("Couldn't open config file to write");
config_file
.write_all(config_file_str.as_bytes())
.expect("Couldn't write to file");
}
fn config_path() -> Result<PathBuf, std::io::Error> {
let config_path: PathBuf = match dirs::config_dir() {
Some(mut path) => {
path.push("snake");
path.push("snake.toml");
path
}
None => {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"snake.toml not found",
));
}
};
Ok(config_path)
}
}
+2
View File
@@ -0,0 +1,2 @@
mod backend;
pub use backend::Config;