Move to struct, cause it's what we do fr fr
This commit is contained in:
parent
8beecb1fec
commit
94220e85a3
@ -6,3 +6,4 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.9.0"
|
||||
|
||||
@ -1 +1,82 @@
|
||||
pub fn move_to() {}
|
||||
use rand::Rng;
|
||||
|
||||
const COLUMNS: usize = 4;
|
||||
const ROWS: usize = 4;
|
||||
|
||||
pub enum Directions {
|
||||
Down,
|
||||
Up,
|
||||
Right,
|
||||
Left,
|
||||
}
|
||||
|
||||
pub struct TaquinGame {
|
||||
empty_coord: [usize; 2],
|
||||
grid: [[u8; COLUMNS]; ROWS],
|
||||
rng: rand::rngs::ThreadRng,
|
||||
score: usize,
|
||||
high_score: usize,
|
||||
}
|
||||
|
||||
impl TaquinGame {
|
||||
pub fn new() -> Self {
|
||||
return TaquinGame {
|
||||
empty_coord: [COLUMNS - 1, ROWS - 1],
|
||||
grid: [[0; COLUMNS]; ROWS],
|
||||
rng: rand::rng(),
|
||||
score: 0,
|
||||
high_score: 0,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn score(&self) -> usize {
|
||||
return self.score;
|
||||
}
|
||||
|
||||
pub fn grid(&self) -> [[u8; COLUMNS]; ROWS] {
|
||||
return self.grid;
|
||||
}
|
||||
|
||||
pub fn high_score(&self) -> usize {
|
||||
return self.high_score;
|
||||
}
|
||||
|
||||
pub fn move_to(&mut self, direction: Directions) -> bool {
|
||||
let (mut row, mut column) = (self.empty_coord[0], self.empty_coord[1]);
|
||||
let mut valid_direction = false;
|
||||
|
||||
match direction {
|
||||
Directions::Up => {
|
||||
if row + 1 < ROWS {
|
||||
row += 1;
|
||||
valid_direction = true;
|
||||
}
|
||||
}
|
||||
Directions::Down => {
|
||||
if row > 0 {
|
||||
row -= 1;
|
||||
valid_direction = true;
|
||||
}
|
||||
}
|
||||
Directions::Left => {
|
||||
if column + 1 < COLUMNS {
|
||||
column += 1;
|
||||
valid_direction = true;
|
||||
}
|
||||
}
|
||||
Directions::Right => {
|
||||
if column > 0 {
|
||||
column -= 1;
|
||||
valid_direction = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if valid_direction {
|
||||
self.exchange(self.empty_coord[1], self.empty_coord[0], column, row);
|
||||
self.score += 1;
|
||||
}
|
||||
|
||||
return valid_direction;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user