Skip to content

Commit

Permalink
Merge pull request #25 from Kapsyloffer/is_valid_rewrite
Browse files Browse the repository at this point in the history
Rewrote is_valid() to take less unnecessary arguments.
  • Loading branch information
Kapsyloffer authored Feb 17, 2024
2 parents a110012 + afab97a commit 5cae90b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 120 deletions.
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/bash
git pull
~/.cargo/bin/cargo run --release
77 changes: 39 additions & 38 deletions src/rules/game_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,54 +42,69 @@ impl Tile {
for i in 1..=2 as i8 {
let new_pos = (cur_pos.0 + i * dy, cur_pos.1 + i * dx);

if Tile::is_valid(boardstate, cur_pos, new_pos, &i, aggr, (&dy, &dx)) {
movelist.push((new_pos.0, new_pos.1)); //this is so crummy.
continue;
if !Tile::is_valid(&b, cur_pos, new_pos, aggr) {
break;
}
break;
movelist.push((new_pos.0, new_pos.1));
}
}
return movelist;
}

//This is ugly but eh.
pub fn is_valid(state: &[[Tile; 4]; 4], cur_pos: (i8, i8), new_pos: (i8, i8), size: &i8, aggr: bool, (dy, dx): (&i8, &i8)) -> bool {
pub fn is_valid(board: &Board, cur_pos: (i8, i8), new_pos: (i8, i8), aggr: bool) -> bool {

let state = board.get_state();

//Check if in range.
let newy = new_pos.0 as usize;
let newx = new_pos.1 as usize;

let step_y = (cur_pos.0 + dy * 1) as usize;
let step_x = (cur_pos.1 + dx * 1) as usize;
//Movement deltas.
let dy = new_pos.0 - cur_pos.0;
let dx = new_pos.1 - cur_pos.1;

//Movement directions.
let dir_y = (dy as f32 / 2.0).round() as i8;
let dir_x = (dx as f32 / 2.0).round() as i8;

//i = size of step, either 1 or 2.
let size = dy.abs().max(dx.abs());

//step coordinates (used if move is 2 in size)
let step_y = (cur_pos.0 + dir_y * 1) as usize;
let step_x = (cur_pos.1 + dir_x * 1) as usize;

//If outta range
if newx > 3 || newy > 3 || step_y > 3 || step_x > 3 {
return false;
}

//Passive
//Passive move; invalid if anything is in its way.
if !aggr {
return state[newy][newx] == Tile::Empty && state[step_y][step_x] == Tile::Empty;
}

//Aggressive move.
if aggr {
//We may not push our own rocks.
if state[newy][newx] == state[cur_pos.0 as usize][cur_pos.1 as usize] {
return false;
}

//future rock positions if pushed:
let rock_y = cur_pos.0 + (*size + 1) * dy;
let rock_x = cur_pos.1 + (*size + 1) * dx;
let rock_y = cur_pos.0 + (size + 1) * dir_y;
let rock_x = cur_pos.1 + (size + 1) * dir_x;

//In case the rock is pushed off the board.
if (rock_y) > 3 || (rock_x) > 3 || (rock_y) < 0 || (rock_x) < 0 {
return true;
}

//Check if a rock is behind our new position if we're pushing a rock. If it's empty we good.
if *size == 1 && state[newy][newx] != Tile::Empty {
if size == 1 && state[newy][newx] != Tile::Empty {
return state[rock_y as usize][rock_x as usize] == Tile::Empty;
} else if *size == 2 && state[step_y][step_x] != Tile::Empty {
} else if size == 2 && state[step_y][step_x] != Tile::Empty {
//If a future rock position is not empty then the move is not valid.
if state[rock_y as usize][rock_x as usize] != Tile::Empty{
return false;
Expand All @@ -103,19 +118,13 @@ impl Tile {

pub fn passive_move(b: &mut Board, cur_pos: (i8, i8), new_pos: (i8, i8)) -> bool {

let dx = new_pos.1 - cur_pos.1;
let dy = new_pos.0 - cur_pos.0;

//i = size of step, 1 or 2.
let i = dy.abs().max(dx.abs());

//If thhe move is invalid, return false.
if !Tile::is_valid(b.get_state(), cur_pos, new_pos, &i, false, (&dy, &dx)) {
if !Tile::is_valid(b, cur_pos, new_pos, false) {
return false;
}

//If we move 0 steps the move is false.
if dx == 0 && dy == 0 {
if cur_pos == new_pos {
return false;
}

Expand All @@ -135,55 +144,47 @@ impl Tile {
}

pub fn aggressive_move(b: &mut Board, cur_pos: (i8, i8), new_pos: (i8, i8)) -> bool {

let cur_tile = b.to_owned().get_state()[cur_pos.0 as usize][cur_pos.1 as usize];

if Tile::is_empty(cur_tile) {
eprintln!("\nwtf are you doing? That's not a rock!\n");
}

let dx = new_pos.1 - cur_pos.1;
let dy = new_pos.0 - cur_pos.0;

let dir = (
(dy as f32 / 2.0).round() as i8,
(dx as f32 / 2.0).round() as i8,
);

//i = antal steps, 1 eller 2
let size = dy.abs().max(dx.abs());

//First and foremost check if the move is valid.
if !Tile::is_valid(b.get_state(), cur_pos, new_pos, &size, true, (&dir.0, &dir.1)) {
if !Tile::is_valid(&b, cur_pos, new_pos, true) {
return false;
}

let mut boardstate = *b.get_state();

//Direction
let dir = (
(dy as f32 / 2.0).round() as i8,
(dx as f32 / 2.0).round() as i8,
);
let dir_y = (dy as f32 / 2.0).round() as i8;
let dir_x = (dx as f32 / 2.0).round() as i8;

//Starting position of the aggressive rock.
let start_y = cur_pos.0 as usize;
let start_x = cur_pos.1 as usize;

//The space between end and start (Only used if we move 2 steps)
let step_y = (cur_pos.0 + 1 * dir.0) as usize;
let step_x = (cur_pos.1 + 1 * dir.1) as usize;
let step_y = (cur_pos.0 + 1 * dir_y) as usize;
let step_x = (cur_pos.1 + 1 * dir_x) as usize;

//Target position
let end_y = new_pos.0 as usize;
let end_x = new_pos.1 as usize;

//End position for the rock we push.
let rock_y = (new_pos.0 + 1 * dir.0) as usize;
let rock_x = (new_pos.1 + 1 * dir.1) as usize;
let rock_y = (new_pos.0 + 1 * dir_y) as usize;
let rock_x = (new_pos.1 + 1 * dir_x) as usize;

let mut stepping: bool = true;
let mut on_board: bool = true;

if step_x == end_x && step_y == end_y {
if (step_y, step_x) == (end_y, end_x) {
stepping = false;
}
if rock_x > 3 || rock_y > 3{
Expand Down
94 changes: 12 additions & 82 deletions src/rules/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,15 +650,8 @@ fn movement_one_in_each_dir_1_step_passive() {
(1, -1),
];

for d in directions {
assert!(Tile::is_valid(
b.get_state(),
(2, 2),
(2 + d.0, 2 + d.1),
&1,
false,
(&d.0, &d.1)
));
for d in directions {
assert!(Tile::is_valid(&b, (2, 2), (2 + d.0, 2 + d.1), false ));
}
}

Expand Down Expand Up @@ -687,14 +680,7 @@ fn movement_one_in_each_dir_1_step_aggressive() {
];

for d in directions {
assert!(Tile::is_valid(
b.get_state(),
(2, 2),
(2 + d.0, 2 + d.1),
&1,
true,
(&d.0, &d.1)
));
assert!(Tile::is_valid( &b, (2, 2), (2 + d.0, 2 + d.1), true));
}
}

Expand Down Expand Up @@ -744,47 +730,19 @@ fn movement_one_in_each_dir_2_step_passive() {
let dir_br = [(-1, 0), (-1, -1), (0, -1)];

for d in dir_tl {
assert!(Tile::is_valid(
b_tl.get_state(),
(0, 0),
(2 + d.0, 2 + d.1),
&2,
false,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_tl, (0, 0), (2 + d.0, 2 + d.1), false));
}

for d in dir_tr {
assert!(Tile::is_valid(
b_tr.get_state(),
(0, 3),
(2 + d.0, 2 + d.1),
&2,
false,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_tr, (0, 3), (2 + d.0, 2 + d.1), false));
}

for d in dir_bl {
assert!(Tile::is_valid(
b_bl.get_state(),
(3, 0),
(2 + d.0, 2 + d.1),
&2,
false,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_bl, (3, 0), (2 + d.0, 2 + d.1), false ));
}

for d in dir_br {
assert!(Tile::is_valid(
b_br.get_state(),
(3, 3),
(2 + d.0, 2 + d.1),
&2,
false,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_br, (3, 3), (2 + d.0, 2 + d.1), false));
}
}

Expand Down Expand Up @@ -834,47 +792,19 @@ fn movement_one_in_each_dir_2_step_aggr() {
let dir_br = [(-1, 0), (-1, -1), (0, -1)];

for d in dir_tl {
assert!(Tile::is_valid(
b_tl.get_state(),
(0, 0),
(2 + d.0, 2 + d.1),
&2,
true,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_tl, (0, 0), (2 + d.0, 2 + d.1), true));
}

for d in dir_tr {
assert!(Tile::is_valid(
b_tr.get_state(),
(0, 3),
(2 + d.0, 2 + d.1),
&2,
true,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_tr, (0, 3), (2 + d.0, 2 + d.1), true ));
}

for d in dir_bl {
assert!(Tile::is_valid(
b_bl.get_state(),
(3, 0),
(2 + d.0, 2 + d.1),
&2,
true,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_bl, (3,0), (2 + d.0, 2 + d.1), true ));
}

for d in dir_br {
assert!(Tile::is_valid(
b_br.get_state(),
(3, 3),
(2 + d.0, 2 + d.1),
&2,
true,
(&d.0, &d.1)
));
assert!(Tile::is_valid(&b_br, (3, 3), (2 + d.0, 2 + d.1), true ));
}
}

Expand Down Expand Up @@ -1208,5 +1138,5 @@ fn is_valid_bug_test()
board.set_state(&state);

//Not supposed to do that.
assert!(!Tile::is_valid(board.get_state(), (2, 1), (2, 3), &2, false, (&0, &1)));
assert!(!Tile::is_valid(&board, (2, 1), (2, 3), false));
}

0 comments on commit 5cae90b

Please sign in to comment.