generated from projeto-de-algoritmos-2024/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from projeto-de-algoritmos-2024/refactor/backend
Refactor/backend
- Loading branch information
Showing
5 changed files
with
163 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::coordinate::Coordinate; | ||
use serde::{Deserialize, Serialize}; | ||
use std::mem::swap; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Eq, Copy, Clone, PartialEq, PartialOrd, Ord, Hash)] | ||
pub struct CellPiece { | ||
pub u: bool, | ||
pub l: bool, | ||
pub d: bool, | ||
pub r: bool, | ||
pub can_rotate: bool, | ||
} | ||
|
||
impl CellPiece { | ||
pub fn rotate(&mut self) { | ||
/* | ||
U L L L | ||
L R -> U R - > D R -> D U | ||
D D U R | ||
*/ | ||
swap(&mut self.u, &mut self.l); | ||
swap(&mut self.l, &mut self.d); | ||
swap(&mut self.r, &mut self.d); | ||
} | ||
} | ||
|
||
pub fn can_connect(u_pos: &Coordinate, u: &CellPiece, v_pos: &Coordinate, v: &CellPiece) -> bool { | ||
if u_pos == v_pos { | ||
return true; | ||
} | ||
|
||
let d_x: i32 = u_pos.x - v_pos.x; | ||
let d_y: i32 = u_pos.y - v_pos.y; | ||
|
||
if d_x != 0 && d_y != 0 { | ||
// diagonal | ||
return false; | ||
} | ||
|
||
if d_x != 0 { | ||
if d_x > 0 { | ||
// v above u | ||
return u.u && v.d; | ||
} else { | ||
// v below u | ||
return u.d && v.u; | ||
} | ||
} else { | ||
if d_y > 0 { | ||
// v at left of u | ||
return u.l && v.r; | ||
} else { | ||
return u.r && v.l; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// 5 directions, center, up, right, down, left (clockwise) | ||
pub const DELTA_X: [i32; 5] = [0, -1, 0, 1, 0]; | ||
pub const DELTA_Y: [i32; 5] = [0, 0, 1, 0, -1]; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq, Copy, Ord, PartialOrd)] | ||
pub struct Coordinate { | ||
pub x: i32, | ||
pub y: i32, | ||
} | ||
|
||
pub fn calc_manhattan_negative_distance(u: &Coordinate, v: &Coordinate) -> i32 { | ||
-((u.x - v.x).abs() + (u.y - v.y).abs()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,14 @@ | ||
use crate::cell_piece::CellPiece; | ||
use crate::coordinate::Coordinate; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Eq, Copy, Clone, PartialEq, PartialOrd, Ord, Hash)] | ||
pub struct CellPiece { | ||
pub u: bool, | ||
pub l: bool, | ||
pub d: bool, | ||
pub r: bool, | ||
pub can_rotate: bool, | ||
} | ||
|
||
impl CellPiece { | ||
pub fn rotate(&mut self) { | ||
let rotated_piece: CellPiece = CellPiece { | ||
u: self.l, | ||
r: self.u, | ||
d: self.r, | ||
l: self.d, | ||
can_rotate: self.can_rotate, | ||
}; | ||
// TODO: Clone this properly ! | ||
self.u = rotated_piece.u; | ||
self.r = rotated_piece.r; | ||
self.d = rotated_piece.d; | ||
self.l = rotated_piece.l; | ||
} | ||
} | ||
|
||
pub fn can_connect(u_x: i32, u_y: i32, u: &CellPiece, v_x: i32, v_y: i32, v: &CellPiece) -> bool { | ||
if u_x == v_x && u_y == v_y { | ||
return true; | ||
} | ||
|
||
let d_x: i32 = u_x - v_x; | ||
let d_y: i32 = u_y - v_y; | ||
|
||
if d_x != 0 && d_y != 0 { | ||
// diagonal | ||
return false; | ||
} | ||
|
||
if d_x != 0 { | ||
if d_x > 0 { | ||
// v above u | ||
return u.u && v.d; | ||
} else { | ||
// v below u | ||
return u.d && v.u; | ||
} | ||
} else { | ||
if d_y > 0 { | ||
// v at left of u | ||
return u.l && v.r; | ||
} else { | ||
return u.r && v.l; | ||
} | ||
} | ||
} | ||
|
||
pub type CellGrid = Vec<Vec<CellPiece>>; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct GameSchema { | ||
pub number_of_rows: i32, | ||
pub number_of_columns: i32, | ||
|
||
pub initial_x: i32, | ||
pub initial_y: i32, | ||
pub source: Coordinate, | ||
|
||
pub target_x: i32, | ||
pub target_y: i32, | ||
pub goal: Coordinate, | ||
|
||
pub grid: CellGrid, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.