Skip to content

Commit

Permalink
Improve bot logic
Browse files Browse the repository at this point in the history
slightly change bot tests
  • Loading branch information
kimpors committed Jan 30, 2024
1 parent f412e55 commit 03fee38
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Build {
}

pub struct Game {
size: u8,
size: usize,
field: Vec<Vec<char>>,
}

Expand Down Expand Up @@ -68,6 +68,15 @@ impl Game {
}
}

impl Clone for Game {
fn clone(&self) -> Self {
Self {
field: self.field.clone(),
size: self.size,
}
}
}

impl Display for Game {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut result = String::new();
Expand Down
63 changes: 38 additions & 25 deletions src/play/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ use crate::state::State;
use crate::Game;
use super::Play;

pub struct Bot {}
pub struct Bot {
}

impl Bot {
pub fn new() -> Self {
Self {}
}

fn calculate(game: &mut Game) {

}
}

impl Play for Bot {
fn make_move(&self, game: &mut Game) -> Result<(), &'static str> {
let size = game.size as usize;
let size = game.size;

match game.set(size / 2, size / 2) {
Some(value) => {
Expand All @@ -30,10 +27,9 @@ impl Play for Bot {
};

let mut wins = Vec::new();
let mut defends = Vec::new();
let mut nones = Vec::new();

Bot::calculate(game);

for y in 0..size {
for x in 0..size {
if game.field[y][x].is_numeric() {
Expand All @@ -46,12 +42,19 @@ impl Play for Bot {
_ => (),
}

game.field[y][x] = 'x';

match State::calculate(&game) {
State::Win => defends.push((y, x)),
_ => (),
}

game.field[y][x] = other;
}
}
}

for state in vec![&wins, &nones] {
for state in vec![&wins, &defends, &nones] {
if !state.is_empty() {
match game.set(state[0].0, state[0].1) {
Some(value) => {
Expand All @@ -61,8 +64,6 @@ impl Play for Bot {

None => return Err("Error, while changing ceil."),
}


}
}

Expand Down Expand Up @@ -103,7 +104,7 @@ mod test {
vec!['x', 'x', '9']]).build();

let case3 = Game::build_with()
.field(vec![vec!['o', 'x', 'x'],
.field(vec![vec!['o', '2', 'x'],
vec!['4', 'x', 'o'],
vec!['7', '8', '9']]).build();

Expand All @@ -112,23 +113,35 @@ mod test {
vec!['4', 'o', '6'],
vec!['7', '8', 'x']]).build();

let underline = "-".repeat(13);

let case1 = (case1, format!(" 1 | 2 | 3 \n{underline}\n \
4 | o | 6 \n{underline}\n \
7 | 8 | 9 \n{underline}\n"));
let res1 = Game::build_with()
.field(vec![vec!['1', '2', '3'],
vec!['4', 'o', '6'],
vec!['7', '8', '9']]).build();

let res2 = Game::build_with()
.field(vec![vec!['1', 'x', '3'],
vec!['o', 'o', 'o'],
vec!['x', 'x', '9']]).build();

let res3 = Game::build_with()
.field(vec![vec!['o', '2', 'x'],
vec!['4', 'x', 'o'],
vec!['o', '8', '9']]).build();

let res4 = Game::build_with()
.field(vec![vec!['x', 'x', 'o'],
vec!['4', 'o', '6'],
vec!['o', '8', 'x']]).build();


let case1 = (case1, res1.to_string());

let case2 = (case2, format!(" 1 | x | 3 \n{underline}\n \
o | o | o \n{underline}\n \
x | x | 9 \n{underline}\n"));
let case2 = (case2, res2.to_string());

let case3 = (case3, format!(" o | x | x \n{underline}\n \
o | x | o \n{underline}\n \
7 | 8 | 9 \n{underline}\n"));
let case3 = (case3, res3.to_string());

let case4 = (case4, format!(" x | x | o \n{underline}\n \
4 | o | 6 \n{underline}\n \
o | 8 | x \n{underline}\n"));
let case4 = (case4, res4.to_string());

return vec![case1, case2, case3, case4];
}
Expand Down
2 changes: 1 addition & 1 deletion src/play/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Player {

impl Play for Player {
fn make_move(&self, game: &mut crate::Game) -> Result<(), &'static str> {
let size = game.size as usize;
let size = game.size;
let mut buf = String::new();

print!("\nEnter free ceil: ");
Expand Down
2 changes: 1 addition & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl State {
}

// Horizontal Pattern
for i in 0..game.size as usize {
for i in 0..game.size {
let horizontal: &[char] = &[game.field[0][i], game.field[1][i], game.field[2][i]];

if horizontal == win {
Expand Down

0 comments on commit 03fee38

Please sign in to comment.