diff --git a/src/lib.rs b/src/lib.rs index b78ffc7efab8d9418423fd485f92701944cbe70d..d58205cbc26e69128ae4758f801f84ef9770ae1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ impl Connect4 { } for i in 1..self.height { if self.board[i][x] != Connect4Case::Empty { - // on place la nouelle piece au dessus de la plus haute piece de la colonne + // on place la nouvelle piece au dessus de la plus haute piece de la colonne self.board[i - 1][x] = if self.is_red_turn() { Connect4Case::Red } else { @@ -107,7 +107,11 @@ impl Connect4 { self.turn_count += 1; println!( "This is {}'s turn !!", - if self.is_red_turn() { RED_CHAR } else { YELLOW_CHAR } + if self.is_red_turn() { + RED_CHAR + } else { + YELLOW_CHAR + } ); if self.is_red_turn() || self.game_mode == 1 { let mut col: usize; @@ -134,8 +138,7 @@ impl Connect4 { fn get_ia1_play(&self) -> usize { loop { let col = rust_hepia_lib::gen(0, self.width as i32) as usize; - let last_emp = self.get_last_empty(col); - if last_emp.is_some() { + if self.board[0][col] == Connect4Case::Empty { return col; } } @@ -157,7 +160,8 @@ impl Connect4 { } } } - if block_col.is_some() { // c'est dans une option car il ne pourrait ne pas y avoir de case a bloqué + if block_col.is_some() { + // c'est dans une option car il ne pourrait ne pas y avoir de case a bloqué return block_col.unwrap(); } else { return self.get_ia1_play(); @@ -165,10 +169,17 @@ impl Connect4 { } fn get_last_empty(&self, col: usize) -> Option<usize> { - if self.board[self.height - 1][col] == Connect4Case::Empty { return Some(self.height - 1); } - if self.board[0][col] != Connect4Case::Empty { return None; } + // récupère la position vide la plus basse d'une colonne + if self.board[self.height - 1][col] == Connect4Case::Empty { + return Some(self.height - 1); + } + if self.board[0][col] != Connect4Case::Empty { + return None; + } for i in 1..self.height { - if self.board[i][col] != Connect4Case::Empty { return Some(i - 1); } + if self.board[i][col] != Connect4Case::Empty { + return Some(i - 1); + } } None } @@ -214,52 +225,6 @@ impl Connect4 { println!(); } - /*fn is_won(&self) -> bool { - for y in 0..self.height { - for x in 0..self.width { - if self.board[y][x] != Connect4Case::Empty { - if x + 3 < WIDTH { - // verif horizontale - if self.board[y][x + 1] == self.board[y][x] - && self.board[y][x + 2] == self.board[y][x] - && self.board[y][x + 3] == self.board[y][x] - { - return true; - } - } - if y + 3 < HEIGHT { - // verif verticale - if self.board[y + 1][x] == self.board[y][x] - && self.board[y + 2][x] == self.board[y][x] - && self.board[y + 3][x] == self.board[y][x] - { - return true; - } - } - if x + 3 < WIDTH && y + 3 < HEIGHT { - // verif diagonale 1 : \ - if self.board[y + 1][x + 1] == self.board[y][x] - && self.board[y + 2][x + 2] == self.board[y][x] - && self.board[y + 3][x + 3] == self.board[y][x] - { - return true; - } - } - if x >= 3 && y + 3 < HEIGHT { - // verif diagonale 2 : / - if self.board[y + 1][x - 1] == self.board[y][x] - && self.board[y + 2][x - 2] == self.board[y][x] - && self.board[y + 3][x - 3] == self.board[y][x] - { - return true; - } - } - } - } - } - return false; - }*/ - fn is_full(&self) -> bool { if self.turn_count >= self.get_max_turn() { true @@ -297,6 +262,7 @@ impl Connect4 { } fn cnt_same_piece_line(&self, x: usize, y: usize, c: Connect4Case, dir: u8) -> u32 { + //compte le nombre de piece qui se suivent sur une ligne a partir d'une position et d'un direction let i: u32; i = self.cnt_same_piece_dir(x, y, c, dir) + self.cnt_same_piece_dir(x, y, c, (dir + 4) % 8) @@ -305,6 +271,7 @@ impl Connect4 { } fn cnt_same_piece_dir(&self, x: usize, y: usize, c: Connect4Case, dir: u8) -> u32 { + //compte le nombre de piece qui se suivent sur une direction a partir d'une position et d'un direction match dir { 0 => if y != 0 && self.board[y - 1][x] == c { //north @@ -363,4 +330,4 @@ impl Connect4 { } } } -} \ No newline at end of file +}