added score counting

This commit is contained in:
JOLIMAITRE Matthieu 2022-04-04 19:49:36 +03:00
parent 56a80cf57e
commit e1acc7d803

View file

@ -90,7 +90,8 @@ impl Game {
} }
pub fn turn(&mut self, movement: Move) -> Result<(), GameError> { pub fn turn(&mut self, movement: Move) -> Result<(), GameError> {
self.perform_move(movement); let move_score = self.perform_move(movement);
self.score += move_score;
for _ in 0..self.spawn_per_turn { for _ in 0..self.spawn_per_turn {
self.spawn_random()?; self.spawn_random()?;
} }
@ -119,50 +120,53 @@ impl Game {
} }
// TODO: macro peut être ? // TODO: macro peut être ?
pub fn perform_move(&mut self, movement: Move) { pub fn perform_move(&mut self, movement: Move) -> usize {
let mut move_score = 0;
match movement { match movement {
Move::LEFT => { Move::LEFT => {
for y in 0..self.board.size() { for y in 0..self.board.size() {
for x in 0..self.board.size() { for x in 0..self.board.size() {
if !self.board.get((x, y)).unwrap().is_empty() { move_score += self.perform_linear_move((-1, 0), (x, y));
self.perform_linear_move((-1, 0), (x, y));
}
} }
} }
} }
Move::RIGHT => { Move::RIGHT => {
for y in 0..self.board.size() { for y in 0..self.board.size() {
for x in (0..self.board.size()).rev() { for x in (0..self.board.size()).rev() {
if !self.board.get((x, y)).unwrap().is_empty() { move_score += self.perform_linear_move((1, 0), (x, y));
self.perform_linear_move((1, 0), (x, y));
}
} }
} }
} }
Move::UP => { Move::UP => {
for x in 0..self.board.size() { for x in 0..self.board.size() {
for y in 0..self.board.size() { for y in 0..self.board.size() {
if !self.board.get((x, y)).unwrap().is_empty() { move_score += self.perform_linear_move((0, -1), (x, y));
self.perform_linear_move((0, -1), (x, y));
}
} }
} }
} }
Move::DOWN => { Move::DOWN => {
for x in 0..self.board.size() { for x in 0..self.board.size() {
for y in (0..self.board.size()).rev() { for y in (0..self.board.size()).rev() {
if !self.board.get((x, y)).unwrap().is_empty() { move_score += self.perform_linear_move((0, 1), (x, y));
self.perform_linear_move((0, 1), (x, y));
}
} }
} }
} }
}; };
move_score
} }
fn perform_linear_move(&mut self, direction: (isize, isize), tile_pos: (usize, usize)) { fn perform_linear_move(
&mut self,
direction: (isize, isize),
tile_pos: (usize, usize),
) -> usize {
if self.board.get(tile_pos.clone()).unwrap().is_empty() {
0
} else {
let mut displacement = Displacement::new(&mut self.board, tile_pos, direction); let mut displacement = Displacement::new(&mut self.board, tile_pos, direction);
displacement.move_all(); displacement.move_all();
displacement.pop_score()
}
} }
} }
@ -170,6 +174,7 @@ pub struct Displacement<'g> {
grid: &'g mut Grid, grid: &'g mut Grid,
position: (usize, usize), position: (usize, usize),
direction: (isize, isize), direction: (isize, isize),
score: usize,
} }
impl<'g> Displacement<'g> { impl<'g> Displacement<'g> {
@ -178,9 +183,15 @@ impl<'g> Displacement<'g> {
grid, grid,
position, position,
direction, direction,
score: 0,
} }
} }
pub fn pop_score(self) -> usize {
let Displacement { score, .. } = self;
score
}
pub fn move_all(&mut self) { pub fn move_all(&mut self) {
loop { loop {
let can_continue = self.move_once(); let can_continue = self.move_once();
@ -203,6 +214,7 @@ impl<'g> Displacement<'g> {
Some(value) if value == current_value => { Some(value) if value == current_value => {
self.grid.move_tile(current_pos, next_pos); self.grid.move_tile(current_pos, next_pos);
self.grid.set(next_pos, Some(value * 2)); self.grid.set(next_pos, Some(value * 2));
self.score = value * 2;
false false
} }
Some(_) => false, Some(_) => false,