changed to egui + made the taquin_lib
Signed-off-by: thatscringebro <thatscringebro@tutanota.com>
This commit is contained in:
@@ -4,4 +4,5 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced = "0.13.1"
|
eframe = "0.32.0"
|
||||||
|
taquin_lib = { path = "../taquin_lib" }
|
||||||
|
|||||||
@@ -1 +1,47 @@
|
|||||||
|
use taquin_lib;
|
||||||
|
|
||||||
|
use eframe::egui;
|
||||||
|
|
||||||
|
fn main() -> eframe::Result {
|
||||||
|
let options = eframe::NativeOptions {
|
||||||
|
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
eframe::run_native(
|
||||||
|
"My egui App",
|
||||||
|
options,
|
||||||
|
Box::new(|cc| Ok(Box::<MyApp>::default())),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MyApp {
|
||||||
|
name: String,
|
||||||
|
age: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for MyApp {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
name: "Arthur".to_owned(),
|
||||||
|
age: 42,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for MyApp {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.heading("My egui Application");
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
let name_label = ui.label("Your name: ");
|
||||||
|
ui.text_edit_singleline(&mut self.name)
|
||||||
|
.labelled_by(name_label.id);
|
||||||
|
});
|
||||||
|
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
|
||||||
|
if ui.button("Increment").clicked() {
|
||||||
|
self.age += 1;
|
||||||
|
}
|
||||||
|
ui.label(format!("Hello '{}', age {}", self.name, self.age));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
rand = "0.9.1"
|
||||||
|
|||||||
162
Session2/TP2/taquin_lib/src/lib.rs
Normal file
162
Session2/TP2/taquin_lib/src/lib.rs
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
pub const COLUMNS: usize = 4;
|
||||||
|
pub 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: [0, 0],
|
||||||
|
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::Down => {
|
||||||
|
if row + 1 < ROWS {
|
||||||
|
row += 1;
|
||||||
|
valid_direction = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Directions::Up => {
|
||||||
|
if row > 0 {
|
||||||
|
row -= 1;
|
||||||
|
valid_direction = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Directions::Right => {
|
||||||
|
if column + 1 < COLUMNS {
|
||||||
|
column += 1;
|
||||||
|
valid_direction = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Directions::Left => {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_grid_done(&mut self) -> bool {
|
||||||
|
let mut error_found = false;
|
||||||
|
let mut value = 0;
|
||||||
|
|
||||||
|
for i in 0..ROWS {
|
||||||
|
if !error_found {
|
||||||
|
for j in 0..COLUMNS {
|
||||||
|
if !error_found {
|
||||||
|
if self.grid[i][j] != value {
|
||||||
|
error_found = true;
|
||||||
|
}
|
||||||
|
value += 1;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !error_found {
|
||||||
|
if self.score > 0 && (self.high_score == 0 || self.score < self.high_score) {
|
||||||
|
self.high_score = self.score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !error_found;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_grid(&mut self) {
|
||||||
|
let move_nmbr = self.rng.random_range(400..500);
|
||||||
|
let mut rnd_nmbr = [0u8, 0, 0, 0];
|
||||||
|
|
||||||
|
self.resolve();
|
||||||
|
|
||||||
|
for _ in 0..move_nmbr {
|
||||||
|
loop {
|
||||||
|
rnd_nmbr[3] = self.rng.random_range(0..4);
|
||||||
|
if rnd_nmbr[0] != rnd_nmbr[3] {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rnd_nmbr = [rnd_nmbr[1], rnd_nmbr[2], rnd_nmbr[3], rnd_nmbr[3]];
|
||||||
|
|
||||||
|
self.move_to(match rnd_nmbr[3] {
|
||||||
|
0 => Directions::Up,
|
||||||
|
1 => Directions::Down,
|
||||||
|
2 => Directions::Left,
|
||||||
|
3 => Directions::Right,
|
||||||
|
_ => Directions::Up,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.score = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exchange(&mut self, ax: usize, ay: usize, bx: usize, by: usize) {
|
||||||
|
if ax < COLUMNS && ay < ROWS && bx < COLUMNS && by < ROWS {
|
||||||
|
let temp = self.grid[by][bx];
|
||||||
|
self.grid[by][bx] = self.grid[ay][ax];
|
||||||
|
self.grid[ay][ax] = temp;
|
||||||
|
self.empty_coord = [by, bx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolve(&mut self) {
|
||||||
|
let mut value = 0;
|
||||||
|
self.score = 0;
|
||||||
|
self.empty_coord = [0, 0];
|
||||||
|
|
||||||
|
for i in 0..ROWS {
|
||||||
|
for j in 0..COLUMNS {
|
||||||
|
self.grid[i][j] = value;
|
||||||
|
value += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user