separated files

This commit is contained in:
Uğur Küpeli
2022-05-04 12:36:38 +03:00
parent f9c4bd620b
commit b1106d3112
4 changed files with 89 additions and 91 deletions

View File

@@ -0,0 +1,29 @@
#[derive(Debug)]
pub struct Coordinate {
x: usize,
y: usize,
pub state: CoordState,
}
impl Coordinate {
pub fn new(pos: (usize, usize), has_mugwump: bool) -> Self {
let state = if has_mugwump {
CoordState::HasMugwump
} else {
CoordState::Normal
};
Coordinate {
x: pos.0,
y: pos.1,
state,
}
}
}
#[derive(Debug, PartialEq)]
pub enum CoordState {
Normal,
HasMugwump,
Checked,
}

View File

@@ -1,4 +1,4 @@
use crate::{CoordState, Coordinate};
use crate::coordinate::{CoordState, Coordinate};
pub fn draw_board(coords: Vec<Coordinate>) {
let draw_top_bottom = |is_top: bool| {
@@ -20,8 +20,6 @@ pub fn draw_board(coords: Vec<Coordinate>) {
println!("");
};
//println!("coords length: {}", coords.len());
draw_top_bottom(true);
let mut y: i8 = 9;

View File

@@ -0,0 +1,56 @@
use rand::Rng;
use crate::coordinate::Coordinate;
pub struct Game {
pub coords: Vec<Coordinate>,
tries: u8,
pub state: GameState,
}
impl Game {
pub fn new() -> Self {
let mut coords = Vec::new();
let mut random_indexes = Vec::new();
let get_random_index = || -> i32 { rand::thread_rng().gen_range(0..100) };
for _ in 0..4 {
let mut i = get_random_index();
while random_indexes.contains(&i) {
i = get_random_index();
}
random_indexes.push(i);
}
let mut x = 0;
let mut y: i8 = 9;
for i in 0..100 {
let mut has_mugwump = false;
if random_indexes.contains(&i) {
has_mugwump = true;
}
coords.push(Coordinate::new((x, y as usize), has_mugwump));
x += 1;
if ((i + 1) % 10) == 0 {
x = 0;
y -= 1;
}
}
Game {
coords,
tries: 0,
state: GameState::Playing,
}
}
}
pub enum GameState {
Playing,
Win,
Lose,
}

View File

@@ -1,95 +1,10 @@
#![allow(dead_code)]
mod coordinate;
mod draw;
mod game;
use rand::Rng;
use crate::draw::draw_board;
struct Game {
coords: Vec<Coordinate>,
tries: u8,
pub state: GameState,
}
impl Game {
fn new() -> Self {
let mut coords = Vec::new();
let mut random_indexes = Vec::new();
let get_random_index = || -> i32 { rand::thread_rng().gen_range(0..100) };
for _ in 0..4 {
let mut i = get_random_index();
while random_indexes.contains(&i) {
i = get_random_index();
}
random_indexes.push(i);
}
let mut x = 0;
let mut y: i8 = 9;
for i in 0..100 {
//println!("current pos: {:?}", (x, y));
let mut has_mugwump = false;
if random_indexes.contains(&i) {
has_mugwump = true;
}
coords.push(Coordinate::new((x, y as usize), has_mugwump));
x += 1;
if ((i + 1) % 10) == 0 {
x = 0;
y -= 1;
}
}
Game {
coords,
tries: 0,
state: GameState::Playing,
}
}
}
enum GameState {
Playing,
Win,
Lose,
}
#[derive(Debug)]
pub struct Coordinate {
x: usize,
y: usize,
state: CoordState,
}
impl Coordinate {
fn new(pos: (usize, usize), has_mugwump: bool) -> Self {
let state = if has_mugwump {
CoordState::HasMugwump
} else {
CoordState::Normal
};
Coordinate {
x: pos.0,
y: pos.1,
state,
}
}
}
#[derive(Debug, PartialEq)]
pub enum CoordState {
Normal,
HasMugwump,
Checked,
}
use crate::{draw::draw_board, game::Game};
fn main() {
println!("\n\nMUGWUMP");