Skip to content

Commit

Permalink
Adding a new error to warn user against tile placement next to oppone…
Browse files Browse the repository at this point in the history
…nt's artifact at the start of the game
  • Loading branch information
Buzz-Lightyear authored and Srinivas Muthu committed Jan 9, 2025
1 parent f8c0a1e commit 2ff8ea8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions truncate_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum GamePlayError {
OccupiedPlace,
#[error("You can only place tiles touching your artifact or your existing tiles")]
NonAdjacentPlace,
#[error("You are attempting to place a tile next to your opponent's artifact")]
OpponentStartPlace,

#[error("Player {player:?} doesn't have a '{tile:?}' tile")]
PlayerDoesNotHaveTile { player: usize, tile: char },
Expand Down
8 changes: 7 additions & 1 deletion truncate_core/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,13 @@ impl Game {
return Err(GamePlayError::OccupiedPlace);
}

if !self.board.neighbouring_squares(position).iter().any(
let neighbors = self.board.neighbouring_squares(position);

if self.turn_count == 0 && neighbors.iter().any(|&(_, square)| matches!(square, Square::Artifact { player: p, .. } if p != player)) {
return Err(GamePlayError::OpponentStartPlace);
}

if !neighbors.iter().any(
|&(_, square)| match square {
Square::Occupied { player: p, .. } => p == player,
Square::Artifact { player: p, .. } => p == player,
Expand Down
16 changes: 16 additions & 0 deletions truncate_core/src/moves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ mod tests {
..Game::new_legacy(3, 3, None, GameRules::generation(0))
};

// Can't accidentally place beside opponent's artifact on first turn
assert_eq!(
game.make_move(
Move::Place {
player: 0,
tile: 'A',
position: Coordinate { x: 2, y: 5 },
},
None,
None,
None,
),
Err(GamePlayError::OpponentStartPlace)
);

// Places beside artifact
let changes = game.make_move(
Move::Place {
Expand Down Expand Up @@ -999,6 +1014,7 @@ mod tests {
players,
player_turn_count: vec![0, 0],
judge: short_dict(),
turn_count: 1, // any non zero value will do to avoid hitting OpponentStartPlace error
..Game::new_legacy(3, 1, None, GameRules::generation(0))
};
game.start();
Expand Down
11 changes: 11 additions & 0 deletions truncate_core/src/npc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,11 @@ mod tests {
initial_board: &'a str,
depth: usize,
dict: &WordDict,
turn_count: u32,
) -> (&'a str, String) {
let mut game = test_game(initial_board, hand);
game.turn_count = turn_count;

let (best_move, pruned_checks, total_checks) = best_test_move(&game, &dict, depth);

enact_move(&mut game, best_move.clone(), &dict);
Expand Down Expand Up @@ -986,6 +989,7 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1027,6 +1031,7 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1068,6 +1073,7 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1109,6 +1115,7 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1150,6 +1157,7 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1194,6 +1202,7 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1241,6 +1250,7 @@ mod tests {
"###,
4,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1294,6 +1304,7 @@ mod tests {
"###,
2,
&dict,
3, // any non zero turn count value will do
);

insta::with_settings!({
Expand Down

0 comments on commit 2ff8ea8

Please sign in to comment.