maerg for test
This commit is contained in:
commit
d9b301ea04
4 files changed
+124
-18
No files matched your search
@@ -4,3 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
crossterm = "0.29"
|
||||
@@ -0,0 +1,61 @@
|
||||
use core::error;
|
||||
use crossterm::{
|
||||
event::{self, Event, KeyCode},
|
||||
terminal::{disable_raw_mode, enable_raw_mode},
|
||||
};
|
||||
use std::io::{self, Write};
|
||||
|
||||
use crate::input;
|
||||
|
||||
pub enum ComandEvent {
|
||||
//who is this
|
||||
None,
|
||||
Exit,
|
||||
Key,
|
||||
}
|
||||
pub fn Comand_pars() -> Result<ComandEvent, String> {
|
||||
let mut input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("error input scan\n");
|
||||
let mut words: Vec<&str> = input.trim().split(" ").collect();
|
||||
if words.len() == 0 {
|
||||
return Err(String::from("Zerro lenth comand\n"));
|
||||
}
|
||||
|
||||
match words[0] {
|
||||
"q" => {
|
||||
return Ok(ComandEvent::Exit);
|
||||
}
|
||||
"key" => {
|
||||
_ = enable_raw_mode();
|
||||
return Ok(ComandEvent::Key);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Ok(ComandEvent::None)
|
||||
}
|
||||
|
||||
pub enum KeyEvent {
|
||||
//KeuEvent for your input
|
||||
None,
|
||||
Exit,
|
||||
Comand,
|
||||
}
|
||||
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
||||
if let Event::Key(key) = event::read()? {
|
||||
match key.code {
|
||||
KeyCode::Char('q') => {
|
||||
_ = disable_raw_mode();
|
||||
return Ok(Some(KeyEvent::Exit));
|
||||
}
|
||||
KeyCode::Char('c') => {
|
||||
_ = disable_raw_mode();
|
||||
return Ok(Some(KeyEvent::Comand));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(Some(KeyEvent::None))
|
||||
}
|
||||
+56
-4
@@ -1,8 +1,60 @@
|
||||
|
||||
|
||||
mod world;
|
||||
use world::*;
|
||||
|
||||
mod input;
|
||||
use input::*;
|
||||
|
||||
mod console_render;
|
||||
use console_render::*;
|
||||
|
||||
enum Input_type {
|
||||
comand,
|
||||
keybard,
|
||||
}
|
||||
fn main() {
|
||||
println!("Hello, dev!");
|
||||
println!("Hello, lemon-cat");
|
||||
println!("Hello, world!");
|
||||
let mut it = Input_type::comand;
|
||||
loop {
|
||||
match it {
|
||||
Input_type::comand => {
|
||||
println!("input your comand: ");
|
||||
let input = Comand_pars();
|
||||
if input.is_err() {
|
||||
println!("ERROR: {}", input.err().unwrap());
|
||||
} else {
|
||||
match input.unwrap() {
|
||||
ComandEvent::Exit => {
|
||||
println!("progarm is end");
|
||||
return;
|
||||
}
|
||||
ComandEvent::Key => {
|
||||
println!("jamp to keybard");
|
||||
it = Input_type::keybard
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Input_type::keybard => {
|
||||
let input = Key_pars();
|
||||
if input.is_err() {
|
||||
println!("ERROR: {}", input.err().unwrap())
|
||||
} else {
|
||||
if let Some(k) = input.unwrap() {
|
||||
match k {
|
||||
KeyEvent::Exit => {
|
||||
println!("progarm is end");
|
||||
return;
|
||||
}
|
||||
KeyEvent::Comand => {
|
||||
println!("jamp to comand");
|
||||
it = Input_type::comand
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-14
@@ -1,8 +1,3 @@
|
||||
use std::usize;
|
||||
|
||||
|
||||
|
||||
|
||||
fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) {
|
||||
let x = ((p.0 % s.0 as i32 + s.0 as i32) % s.0 as i32) as usize;
|
||||
let y = ((p.1 % s.1 as i32 + s.1 as i32) % s.1 as i32) as usize;
|
||||
@@ -10,27 +5,24 @@ fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) {
|
||||
(x, y)
|
||||
}
|
||||
|
||||
struct TorusGrid<T: Clone>{
|
||||
pub struct TorusGrid<T: Clone> {
|
||||
vec: Vec<T>,
|
||||
size: (usize, usize),
|
||||
}
|
||||
|
||||
impl<T: Clone> TorusGrid<T> {
|
||||
pub fn new(value: T, s: (usize, usize)) -> Self{
|
||||
pub fn new(value: T, s: (usize, usize)) -> Self {
|
||||
let v = vec![value; s.0 * s.1];
|
||||
TorusGrid {
|
||||
vec: v,
|
||||
size: s
|
||||
}
|
||||
TorusGrid { vec: v, size: s }
|
||||
}
|
||||
fn index(&self, x: usize, y: usize) -> usize {
|
||||
y * self.size.0 + x
|
||||
}
|
||||
pub fn if_in_grid(&self, s: (i32, i32)) -> bool{
|
||||
if s.0 < 0 || s.0 > self.size.0 as i32 - 1{
|
||||
pub fn if_in_grid(&self, s: (i32, i32)) -> bool {
|
||||
if s.0 < 0 || s.0 > self.size.0 as i32 - 1 {
|
||||
return false;
|
||||
}
|
||||
if s.1 < 0 || s.1 > self.size.1 as i32 - 1{
|
||||
if s.1 < 0 || s.1 > self.size.1 as i32 - 1 {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
|
||||
Reference in new issue
Block a user