Skip to content

Commit 2864865

Browse files
authored
Merge pull request #294 from Buzz-Lightyear/main
Adding a new error to warn user against tile placement next to opponent's artifact at the start of the game
2 parents b44cb9b + 2ff8ea8 commit 2864865

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Diff for: truncate_core/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum GamePlayError {
3333
OccupiedPlace,
3434
#[error("You can only place tiles touching your artifact or your existing tiles")]
3535
NonAdjacentPlace,
36+
#[error("You are attempting to place a tile next to your opponent's artifact")]
37+
OpponentStartPlace,
3638

3739
#[error("Player {player:?} doesn't have a '{tile:?}' tile")]
3840
PlayerDoesNotHaveTile { player: usize, tile: char },

Diff for: truncate_core/src/game.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,13 @@ impl Game {
566566
return Err(GamePlayError::OccupiedPlace);
567567
}
568568

569-
if !self.board.neighbouring_squares(position).iter().any(
569+
let neighbors = self.board.neighbouring_squares(position);
570+
571+
if self.turn_count == 0 && neighbors.iter().any(|&(_, square)| matches!(square, Square::Artifact { player: p, .. } if p != player)) {
572+
return Err(GamePlayError::OpponentStartPlace);
573+
}
574+
575+
if !neighbors.iter().any(
570576
|&(_, square)| match square {
571577
Square::Occupied { player: p, .. } => p == player,
572578
Square::Artifact { player: p, .. } => p == player,

Diff for: truncate_core/src/moves/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ mod tests {
143143
..Game::new_legacy(3, 3, None, GameRules::generation(0))
144144
};
145145

146+
// Can't accidentally place beside opponent's artifact on first turn
147+
assert_eq!(
148+
game.make_move(
149+
Move::Place {
150+
player: 0,
151+
tile: 'A',
152+
position: Coordinate { x: 2, y: 5 },
153+
},
154+
None,
155+
None,
156+
None,
157+
),
158+
Err(GamePlayError::OpponentStartPlace)
159+
);
160+
146161
// Places beside artifact
147162
let changes = game.make_move(
148163
Move::Place {
@@ -999,6 +1014,7 @@ mod tests {
9991014
players,
10001015
player_turn_count: vec![0, 0],
10011016
judge: short_dict(),
1017+
turn_count: 1, // any non zero value will do to avoid hitting OpponentStartPlace error
10021018
..Game::new_legacy(3, 1, None, GameRules::generation(0))
10031019
};
10041020
game.start();

Diff for: truncate_core/src/npc/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,11 @@ mod tests {
717717
initial_board: &'a str,
718718
depth: usize,
719719
dict: &WordDict,
720+
turn_count: u32,
720721
) -> (&'a str, String) {
721722
let mut game = test_game(initial_board, hand);
723+
game.turn_count = turn_count;
724+
722725
let (best_move, pruned_checks, total_checks) = best_test_move(&game, &dict, depth);
723726

724727
enact_move(&mut game, best_move.clone(), &dict);
@@ -986,6 +989,7 @@ mod tests {
986989
"###,
987990
3,
988991
&dict,
992+
0,
989993
);
990994

991995
insta::with_settings!({
@@ -1027,6 +1031,7 @@ mod tests {
10271031
"###,
10281032
3,
10291033
&dict,
1034+
0,
10301035
);
10311036

10321037
insta::with_settings!({
@@ -1068,6 +1073,7 @@ mod tests {
10681073
"###,
10691074
3,
10701075
&dict,
1076+
0,
10711077
);
10721078

10731079
insta::with_settings!({
@@ -1109,6 +1115,7 @@ mod tests {
11091115
"###,
11101116
3,
11111117
&dict,
1118+
0,
11121119
);
11131120

11141121
insta::with_settings!({
@@ -1150,6 +1157,7 @@ mod tests {
11501157
"###,
11511158
3,
11521159
&dict,
1160+
0,
11531161
);
11541162

11551163
insta::with_settings!({
@@ -1194,6 +1202,7 @@ mod tests {
11941202
"###,
11951203
3,
11961204
&dict,
1205+
0,
11971206
);
11981207

11991208
insta::with_settings!({
@@ -1241,6 +1250,7 @@ mod tests {
12411250
"###,
12421251
4,
12431252
&dict,
1253+
0,
12441254
);
12451255

12461256
insta::with_settings!({
@@ -1294,6 +1304,7 @@ mod tests {
12941304
"###,
12951305
2,
12961306
&dict,
1307+
3, // any non zero turn count value will do
12971308
);
12981309

12991310
insta::with_settings!({

0 commit comments

Comments
 (0)