1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(
clippy::too_many_lines,
clippy::non_ascii_literal,
clippy::module_name_repetitions,
clippy::use_self
)]
#![cfg_attr(not(test), no_std)]
#[macro_use]
extern crate alloc;
use alloc::vec::Vec;
/// Spits out all the possible opponent (downward)'s move that is played from the hop1zuo1 onto the board.
#[must_use]
pub fn from_hop1zuo1_candidates(game_state: &PureGameState) -> Vec<PureMove> {
let mut ans = vec![];
for piece in &game_state.f.hop1zuo1of_downward {
for empty_square in empty_squares(game_state) {
ans.push(PureMove::NonTamMoveFromHopZuo {
color: piece.color,
prof: piece.prof,
dest: to_absolute_coord(empty_square, game_state.perspective),
});
}
}
ans
}
pub use pure_move::PureMove;
mod calculate_movable;
pub use calculate_movable::calculate_movable_positions_for_either_side;
#[derive(Debug, Eq, Clone, PartialEq)]
pub struct MovablePositions {
pub finite: Vec<Coord>,
pub infinite: Vec<Coord>,
}
fn can_get_occupied_by(
side: Side,
dest: Coord,
piece_to_move: Piece,
board: Board,
tam_itself_is_tam_hue: bool,
) -> bool {
if piece_to_move == Piece::Tam2 {
let [i, j] = dest;
let dest_piece = board[i][j];
/* It is allowed to enter an empty square */
dest_piece.is_none()
} else {
can_get_occupied_by_non_tam(side, dest, board, tam_itself_is_tam_hue)
}
}
fn empty_neighbors_of(board: Board, c: Coord) -> impl Iterator<Item = Coord> {
calculate_movable::iter::eight_neighborhood(c)
.filter(move |[i, j]| board[*i][*j].is_none())
}
fn can_get_occupied_by_non_tam(
side: Side,
dest: Coord,
board: Board,
tam_itself_is_tam_hue: bool,
) -> bool {
/* Intentionally does not verify whether the piece itself is of opponent */
let is_protected_by_opponent_tam_hue_auai = |side: Side, coord: Coord| {
calculate_movable::vec::eight_neighborhood(coord)
.into_iter()
.filter(|[a, b]| {
let piece = board[*a][*b];
match piece {
None | Some(Piece::Tam2) => false,
Some(Piece::NonTam2Piece {
side: piece_side,
prof: piece_prof,
color: _,
}) => {
piece_prof == Profession::Uai1
&& piece_side != side
&& calculate_movable::is_tam_hue([*a, *b], board, tam_itself_is_tam_hue)
}
}
})
.count()
> 0
};
let [i, j] = dest;
let dest_piece = board[i][j];
match dest_piece {
Some(Piece::Tam2) => false, /* Tam2 can never be taken */
None => true, /* It is always allowed to enter an empty square */
Some(Piece::NonTam2Piece {
side: piece_side,
prof: _,
color: _,
}) => {
piece_side != side /* cannot take your own piece */ &&
!is_protected_by_opponent_tam_hue_auai(
side,
dest
)
} /* must not be protected by tam2 hue a uai1 */
}
}
const fn is_ciurl_required(dest: Coord, moving_piece_prof: Profession, src: Coord) -> bool {
is_water(dest) && !is_water(src) && !matches!(moving_piece_prof, Profession::Nuak1)
}
/// Spits out all the possible opponent (downward)'s move that is played by moving a piece on the board, not from the hop1zuo1.
#[must_use]
pub fn not_from_hop1zuo1_candidates_(config: &Config, game_state: &PureGameState) -> Vec<PureMove> {
let mut ans = vec![];
for rand_i in 0..9 {
for rand_j in 0..9 {
let src = [rand_i, rand_j];
let piece = game_state.f.current_board[rand_i][rand_j];
if let Some(p) = piece {
match p {
Piece::Tam2 => {
let candidates: Vec<Coord> =
calculate_movable::vec::eight_neighborhood(src);
for tentative_dest in candidates {
let dest_piece =
game_state.f.current_board[tentative_dest[0]][tentative_dest[1]];
/* avoid self-occlusion */
let mut subtracted_board = game_state.f.current_board;
subtracted_board[src[0]][src[1]] = None;
// FIXME: tam2 ty sak2 not handled
if dest_piece.is_none() {
/* empty square; first move is completed without stepping */
let fst_dst: Coord = tentative_dest;
ans.append(&mut calculate_movable::iter::eight_neighborhood(fst_dst).flat_map(|neighbor| {
/* if the neighbor is empty, that is the second destination */
let snd_dst: Coord = neighbor;
if game_state.f.current_board[neighbor[0]][neighbor[1]].is_none() /* the neighbor is utterly occupied */ ||
neighbor == src
/* the neighbor is occupied by yourself, which means it is actually empty */
{
vec![PureMove::TamMoveNoStep {
second_dest: to_absolute_coord(snd_dst, game_state.perspective),
first_dest: to_absolute_coord(fst_dst, game_state.perspective),
src: to_absolute_coord(src, game_state.perspective),
}].into_iter()
} else {
/* if not, step from there */
let step: Coord = neighbor;
empty_neighbors_of(subtracted_board, step)
.flat_map(|snd_dst| {
vec![PureMove::TamMoveStepsDuringLatter {
first_dest: to_absolute_coord(fst_dst, game_state.perspective),
second_dest: to_absolute_coord(snd_dst, game_state.perspective),
src: to_absolute_coord(src, game_state.perspective),
step: to_absolute_coord(step, game_state.perspective),
}].into_iter()
}).collect::<Vec<PureMove>>().into_iter()
}
}).collect::<Vec<PureMove>>());
} else {
/* not an empty square: must complete the first move */
let step = tentative_dest;
ans.append(
&mut empty_neighbors_of(subtracted_board, step)
.flat_map(|fst_dst| {
let v = empty_neighbors_of(subtracted_board, fst_dst);
v.flat_map(move |snd_dst| {
vec![PureMove::TamMoveStepsDuringFormer {
first_dest: to_absolute_coord(
fst_dst,
game_state.perspective,
),
second_dest: to_absolute_coord(
snd_dst,
game_state.perspective,
),
src: to_absolute_coord(
src,
game_state.perspective,
),
step: to_absolute_coord(
step,
game_state.perspective,
),
}]
.into_iter()
})
.collect::<Vec<PureMove>>()
.into_iter()
})
.collect::<Vec<PureMove>>(),
);
}
}
}
Piece::NonTam2Piece {
side: Side::Downward,
prof,
color,
} => {
let MovablePositions { finite, infinite } =
calculate_movable::calculate_movable_positions_for_nontam(
src,
prof,
game_state.f.current_board,
game_state.tam_itself_is_tam_hue,
Side::Downward,
);
let candidates: Vec<Coord> = [&finite[..], &infinite[..]].concat();
for tentative_dest in candidates {
let dest_piece =
game_state.f.current_board[tentative_dest[0]][tentative_dest[1]];
let candidates_when_stepping = || {
let step = tentative_dest; // tentative_dest becomes the position on which the stepping occurs
let perspective = game_state.perspective;
let tam_itself_is_tam_hue: bool = game_state.tam_itself_is_tam_hue;
/* now, to decide the final position, we must remove the piece to prevent self-occlusion */
let mut subtracted_board = game_state.f.current_board;
subtracted_board[src[0]][src[1]] = None; /* must remove the piece to prevent self-occlusion */
let MovablePositions { finite, infinite } =
calculate_movable::calculate_movable_positions_for_nontam(
step,
prof,
subtracted_board,
tam_itself_is_tam_hue,
Side::Downward,
);
let candidates = finite.into_iter();
let candidates_inf = infinite.into_iter();
[
&candidates
.flat_map(|final_dest| {
if can_get_occupied_by(
Side::Downward,
final_dest,
Piece::NonTam2Piece {
color,
prof,
side: Side::Downward,
},
subtracted_board,
tam_itself_is_tam_hue,
) {
vec![PureMove::NonTamMoveSrcStepDstFinite {
src: to_absolute_coord(src, perspective),
step: to_absolute_coord(step, perspective),
dest: to_absolute_coord(
final_dest,
perspective,
),
is_water_entry_ciurl: is_ciurl_required(
final_dest, prof, src,
),
}]
.into_iter()
} else {
vec![].into_iter()
}
})
.collect::<Vec<PureMove>>()[..],
&candidates_inf
.flat_map(|planned_dest| {
if !can_get_occupied_by(
Side::Downward,
planned_dest,
Piece::NonTam2Piece {
color,
prof,
side: Side::Downward,
},
subtracted_board,
tam_itself_is_tam_hue,
) {
return vec![].into_iter();
// retry
}
let obj: PureMove = PureMove::InfAfterStep {
src: to_absolute_coord(src, perspective),
step: to_absolute_coord(step, perspective),
planned_direction: to_absolute_coord(
planned_dest,
perspective,
),
};
vec![obj].into_iter()
})
.collect::<Vec<PureMove>>()[..],
]
.concat()
};
match dest_piece {
None => {
// cannot step
ans.append(&mut vec![PureMove::NonTamMoveSrcDst {
src: to_absolute_coord(src, game_state.perspective),
dest: to_absolute_coord(
tentative_dest,
game_state.perspective,
),
is_water_entry_ciurl: is_ciurl_required(
tentative_dest,
prof,
src,
),
}]);
}
Some(Piece::Tam2) => {
// if allowed by config, allow stepping on Tam2;
if config.allow_kut2tam2 {
ans.append(&mut candidates_when_stepping());
} else {
ans.append(&mut vec![]);
}
}
Some(Piece::NonTam2Piece {
side: Side::Upward,
color: _,
prof: _,
}) => {
// opponent's piece; stepping and taking both attainable
// except when protected by tam2 hue a uai1
if can_get_occupied_by(
Side::Downward,
tentative_dest,
Piece::NonTam2Piece {
color,
prof,
side: Side::Downward,
},
game_state.f.current_board,
game_state.tam_itself_is_tam_hue,
) {
ans.append(
&mut [
&[PureMove::NonTamMoveSrcDst {
src: to_absolute_coord(
src,
game_state.perspective,
),
dest: to_absolute_coord(
tentative_dest,
game_state.perspective,
),
is_water_entry_ciurl: is_ciurl_required(
tentative_dest,
prof,
src,
),
}][..],
&candidates_when_stepping()[..],
]
.concat(),
);
} else {
ans.append(&mut candidates_when_stepping());
}
}
Some(_) => {
ans.append(&mut candidates_when_stepping());
}
}
}
}
Piece::NonTam2Piece {
side: Side::Upward, ..
} => {}
}
}
}
}
ans
}
fn empty_squares(game_state: &PureGameState) -> Vec<Coord> {
let mut ans = vec![];
for rand_i in 0..9 {
for rand_j in 0..9 {
let coord: Coord = [rand_i, rand_j];
if game_state.f.current_board[rand_i][rand_j].is_none() {
ans.push(coord);
}
}
}
ans
}
use cetkaik_core::relative::{is_water, Board, Coord, Field, NonTam2PieceUpward, Piece, Side};
pub use cetkaik_core::absolute;
pub mod pure_move;
pub struct Config {
pub allow_kut2tam2: bool,
}
#[cfg(test)]
mod tests;
pub use cetkaik_core::perspective::*;
pub use cetkaik_core::{Color, Profession};
#[derive(Debug)]
pub struct PureGameState {
pub f: Field,
pub perspective: Perspective,
pub tam_itself_is_tam_hue: bool,
pub opponent_has_just_moved_tam: bool,
}
/// According to <https://github.com/cetkaik/cetkaik_yhuap_move_candidates/pull/7>,
/// this function was a bottleneck that accounted for roughly fifty percent of all the runtime
/// in an attempt to implement an AI for this game.
/// Hence, this module contains multiple implementations of this functions and test that they are equal.
#[allow(clippy::similar_names)]
pub mod get_blocker_deltas;