bool play_move(game g, int piece_num, dir d, int distance) { piece p=g->piece_list[piece_num]; int travel=0; while (distance!=0) { move_piece(p, d, 1); g->nb_moves+=1; distance--; travel++; for (int i=0; i<game_nb_pieces(g); ++i) { if (p==g->piece_list[i]) { i++; if (i>=game_nb_pieces(g)) break; } if (intersect(p, game_piece(g, i))) { fprintf(stderr, "Unauthorized move: Piece %d is preventing %d from moving.\n\n", i, piece_num); move_piece(p, d, travel*-1); g->nb_moves-=travel; return false; } if (out_of_grid(p, g)) { fprintf(stderr, "Unauthorized move: %d would be out of bounds.\n\n", piece_num); move_piece(p, d, travel*-1); g->nb_moves-=travel; return false; } if (((d==LEFT||d==RIGHT)&&(!can_move_x(p)))||((d==UP||d==DOWN)&&(!can_move_y(p)))) { fprintf(stderr, "Unauthorized move: Piece orientation doesn't match move direction.\n\n"); return false; } } } return true; }
void MovePicker::score_evasions() { // Try good captures ordered by MVV/LVA, then non-captures if // destination square is not under attack, ordered by history // value, and at the end bad-captures and non-captures with a // negative SEE. This last group is ordered by the SEE score. Move m; int seeScore; // Skip if we don't have at least two moves to order if (lastMove < moves + 2) return; for (MoveStack* cur = moves; cur != lastMove; cur++) { m = cur->move; if ((seeScore = pos.see_sign(m)) < 0) cur->score = seeScore - History::MaxValue; // Be sure we are at the bottom else if (pos.is_capture(m)) #if defined(NANOHA) cur->score = piece_value_midgame(pos.piece_on(move_to(m))) - type_of(move_piece(m)) + History::MaxValue; #else cur->score = piece_value_midgame(pos.piece_on(move_to(m))) - type_of(pos.piece_on(move_from(m))) + History::MaxValue; #endif else #if defined(NANOHA) { Piece piece = is_promotion(m) ? Piece(move_piece(m) | PROMOTED) : move_piece(m); cur->score = H.value(piece, move_to(m)); } #else cur->score = H.value(pos.piece_on(move_from(m)), move_to(m)); #endif }
void test_move_piece(void) { piece test = new_piece_rh(1,2,true,false); delete_piece(test); /** * move_piece(test, LEFT, 1); * return an exit_failure : the delete_piece works ! */ piece test2 = new_piece_rh(1,2,true,false); /** * move_piece(test2, UP, -2); * exit_failure because distance<0 -> test error */ move_piece(test2, UP, 2); display_piece(test2); /** * display the piece in order to verify that move_piece(test2, UP, 2) happened */ move_piece(test2, LEFT, 2); /** * don't move the piece because the direction doesn't suit the piece */ move_piece (test2, UP, 3); /** * don't move the piece, otherwise it would go outside the board */ display_piece(test2); /* * display the piece in order to verify that the movement didn't occur */ delete_piece(test2); }
bool play_move(game g, int piece_num, dir d, int distance){ piece p = g->pieces[piece_num]; if(is_horizontal(p)){ if(d == UP || d == DOWN) return false; }else{ if(d == RIGHT || d == LEFT) return false; } int cover_distance; for(cover_distance = 1; cover_distance<=distance; ++cover_distance){ move_piece(p, d, 1); if(!is_in_game(g, piece_num) || is_above_piece(g, piece_num)){ if(d == RIGHT) move_piece(p, LEFT, cover_distance); else if(d == LEFT) move_piece(p, RIGHT, cover_distance); else if(d == UP) move_piece(p, DOWN, cover_distance); else move_piece(p, UP, cover_distance); return false; } } g->nb_moves += distance; return true; }
bool test_move() { bool result = true; piece p = new_piece_rh(0, 0, true, true); set_up(); for (int dist = 1; dist < NB_PIECES; dist++) for (int i=0; i < NB_PIECES; i++) { copy_piece(pieces[i],p); move_piece(p, LEFT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i])-dist,get_x(p),"move LEFT"); else result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move LEFT"); copy_piece(pieces[i],p); move_piece(p, RIGHT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i])+dist,get_x(p),"move RIGHT"); else result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move RIGHT"); copy_piece(pieces[i],p); move_piece(p, UP, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i])+dist,get_y(p),"move UP"); else result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move UP"); copy_piece(pieces[i],p); move_piece(p, DOWN, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i])-dist,get_y(p),"move DOWN"); else result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move DOWN"); } tear_down(); delete_piece(p); return result; }
static int ambiguity(int move, const board_t * board) { int from, to, piece; list_t list[1]; int i, n, m; // init from = move_from(move); to = move_to(move); piece = move_piece(move,board); gen_legal_moves(list,board); // no ambiguity? n = 0; for (i = 0; i < list_size(list); i++) { m = list_move(list,i); if (move_piece(m,board) == piece && move_to(m) == to) { n++; } } if (n == 1) return AMBIGUITY_NONE; // file ambiguity? n = 0; for (i = 0; i < list_size(list); i++) { m = list_move(list,i); if (move_piece(m,board) == piece && move_to(m) == to) { if (square_file(move_from(m)) == square_file(from)) n++; } } if (n == 1) return AMBIGUITY_FILE; // rank ambiguity? n = 0; for (i = 0; i < list_size(list); i++) { m = list_move(list,i); if (move_piece(m,board) == piece && move_to(m) == to) { if (square_rank(move_from(m)) == square_rank(from)) n++; } } if (n == 1) return AMBIGUITY_RANK; // square ambiguity return AMBIGUITY_SQUARE; }
bool test_move() { bool result = true; piece p = new_piece(0, 0, 2, 1, true, false); //move_x-> TRUE, move_y->FALSE set_up(); for (int dist = 1; dist < NB_PIECES; dist++) for (int i=0; i < NB_PIECES; i++) { // Test move LEFT copy_piece(pieces[i],p); move_piece(p, LEFT, dist); if (can_move_x(pieces[i])) result = result && test_equality_int(get_x(pieces[i])-dist,get_x(p),"move LEFT"); else result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move LEFT"); // Test move RIGHT copy_piece(pieces[i],p); move_piece(p, RIGHT, dist); if (can_move_x(pieces[i])) result = result && test_equality_int(get_x(pieces[i])+dist,get_x(p),"move RIGHT"); else result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move RIGHT"); // Test move UP copy_piece(pieces[i],p); move_piece(p, UP, dist); if (can_move_y(pieces[i])) result = result && test_equality_int(get_y(pieces[i])+dist,get_y(p),"move UP"); else result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move UP"); // Test move DOWN copy_piece(pieces[i],p); move_piece(p, DOWN, dist); if (can_move_y(pieces[i])) result = result && test_equality_int(get_y(pieces[i])-dist,get_y(p),"move DOWN"); else result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move DOWN"); } tear_down(); delete_piece(p); return result; }
bool play_move(game g, int piece_num, dir d, int distance){ if ((!can_move_y(g->pieces[piece_num]) && (d == 0 || d == 2)) || ((!can_move_x(g->pieces[piece_num])) && (d == 1 || d == 3))) return false; bool move_isAllowed = true; piece tmp_piece = new_piece(0,0,0,0,true,true); // Initialisation d'une pièce temporaire (mallocs) copy_piece(g->pieces[piece_num],tmp_piece); for (int i = 0; i < distance; i++) { // On decompose le mouvement en déplacement de une case move_piece(g->pieces[piece_num], d, 1); if ((get_x(g->pieces[piece_num])+get_width(g->pieces[piece_num])-1 >= game_width(g) || get_x(g->pieces[piece_num]) < 0) || (get_y(g->pieces[piece_num])+get_height(g->pieces[piece_num])-1 >= game_height(g) || get_y(g->pieces[piece_num]) < 0)) move_isAllowed = false; for(int p = 0; p < game_nb_pieces(g);++p) { // On verifie si le mouvement est valide (intersect+depassement grille) if (piece_num != p && intersect(g->pieces[piece_num], g->pieces[p])) move_isAllowed = false; } } if (move_isAllowed) { for(int p = 0; p < game_nb_pieces(g);++p) { if (piece_num != p && intersect(g->pieces[piece_num], g->pieces[p])) printf("Erreur\n"); } g->nb_mouv += distance; delete_piece(tmp_piece); return true; } // si le mouvement n'est pas valide on remets la piece a sa place initiale copy_piece(tmp_piece, g->pieces[piece_num]); delete_piece(tmp_piece); return false; }
void MovePicker::score_captures() { // Winning and equal captures in the main search are ordered by MVV/LVA. // Suprisingly, this appears to perform slightly better than SEE based // move ordering. The reason is probably that in a position with a winning // capture, capturing a more valuable (but sufficiently defended) piece // first usually doesn't hurt. The opponent will have to recapture, and // the hanging piece will still be hanging (except in the unusual cases // where it is possible to recapture with the hanging piece). Exchanging // big pieces before capturing a hanging piece probably helps to reduce // the subtree size. // In main search we want to push captures with negative SEE values to // badCaptures[] array, but instead of doing it now we delay till when // the move has been picked up in pick_move_from_list(), this way we save // some SEE calls in case we get a cutoff (idea from Pablo Vazquez). Move m; // Use MVV/LVA ordering for (MoveStack* cur = moves; cur != lastMove; cur++) { m = cur->move; cur->score = piece_value_midgame(pos.piece_on(move_to(m))) - type_of(pos.piece_on(move_from(m))); if (is_promotion(m)) #if defined(NANOHA) cur->score += piece_value_midgame(Piece(move_piece(m) | PROMOTED)); #else cur->score += piece_value_midgame(Piece(promotion_piece_type(m))); #endif } }
void MovePicker::score_noncaptures() { Move m; for (MoveStack* cur = moves; cur != lastMove; cur++) { m = cur->move; #if defined(NANOHA) assert(m != MOVE_NULL); Piece piece = is_promotion(m) ? Piece(move_piece(m) | PROMOTED) : move_piece(m); cur->score = H.value(piece, move_to(m)); #else Square from = move_from(m); cur->score = H.value(pos.piece_on(from), move_to(m)); #endif } }
/* Alpha-beta pruning search. */ int abp_search (int player, int depth, int alpha, int beta) { int legal_moves[BOARD_SIZE]; int curr_util = NEG_INF, i; int mod = -1 * ((player == BPLAYER) ? -1 : 1); /* For each piece on the board, generate its legal moves and evaluate * its utility. Track the move with the greatest utility. */ for (i = 0; i < BOARD_SIZE; i++) { if (board[i] > chp_null * mod) { init_moves_board (legal_moves); gen_plegal_moves (player, i, legal_moves); int j; for (j = 0; j < BOARD_SIZE; j++) { if (legal_moves[j] == TRUE) { int attacked_piece = move_piece (i, j); int move_util = 0; /* If move wins the game, automatically make that move. */ if (game_over () == TRUE) { unmove_piece (i, j, attacked_piece); return (-1 * mod) * POS_INF; } /* If maximum depth reached evaluate the board. Else, * continue search. */ if (depth == 1) { move_util = mod * board_utility (); } else { move_util = mod * abp_search (opponent_player (player), depth - 1, -1 * beta, -1 * alpha); } /* If this move's utility is a new maximum, save it. Alter * alpha value and check against beta to potentially short * circuit the search. */ if (move_util > curr_util) { curr_util = move_util; } if (curr_util > alpha) { alpha = curr_util; } if (alpha >= beta) { unmove_piece (i, j, attacked_piece); return alpha; } unmove_piece (i, j, attacked_piece); } } } } return curr_util; }
int ky_turn(t_program *tetris, t_tetrimino *tet) { tet->rot = (tet->rot + 1); set_piece(tet); if (move_piece(tetris, tet, &tetris->posit)) { tet->rot = (tet->rot - 1); set_piece(tet); } return (0); }
/* Set moves leaving player in check to FALSE in MOVES_ARRAY. */ void remove_check_moves (int player, int start_pos, int *moves_array) { int i; for (i = 0; i < BOARD_SIZE; i++) { if (moves_array[i] == TRUE) { int attacked = move_piece (start_pos, i); if (player_in_check (player) == TRUE) { moves_array[i] = FALSE; } unmove_piece (start_pos, i, attacked); } } }
int ky_left(t_program *tetris, t_tetrimino *tet) { tetris->posit.x--; if (move_piece(tetris, tet, &tetris->posit)) { tetris->posit.x++; display_to_board(tetris); return (0); } display_move_piece(tetris, tet, &tetris->posit); display_to_board(tetris); return (0); }
//checks a specific move from specific location on the board. If move viable and doesn't lead to duplicate board, //new board shows this move and 1 is returned. If no moves found, returns 0 int check_move (movement direction, int i, int j, hash *hash_array, Node *start, Node *latest_node, int parent_board [BOARD_HEIGHT][BOARD_WIDTH], int new_board[BOARD_HEIGHT][BOARD_WIDTH]){ if (valid_move (direction, i, j, new_board)){ move_piece (direction, i, j, new_board); latest_node->identifier = allocate_board_identifier (new_board); if (insert_hash (hash_array, latest_node, latest_node->identifier, new_board) == 1){ copy_board (new_board, parent_board); } else{ return 1; } } return 0; }
void test_failure_piece(cpiece src) { piece dst = NULL; copy_piece(src, dst); move_piece(dst, LEFT, 1); intersect(src, src); get_y(src); get_x(src); get_width(src); get_height(src); is_horizontal(src); can_move_x(src); can_move_y(src); delete_piece(dst); }
int main(int argc, char *argv[]) { Echiquier B = E; double end_game; do{//loop to start a new game do{//loop to change player until mat term_clear(); // un peu mieux ;) print_game(&B); if(hunt_chess(&B)){ printf("Vous etes en echec !\n"); } move_piece(&B); /* printf("\n----------blacks_positions------------\n"); print_binary_chess_table(B.blacks_position); printf("\n---------whites_position----------\n"); print_binary_chess_table(B.whites_position); printf("\n"); printf("a6 : %d, %d, (%d,%d), %d\n", B.t[2][0].t, B.t[2][0].c,B.t[2][0].p.posx,B.t[2][0].p.posy,B.t[2][0].m); getchar();*/ //print_game(&B); }while(!mat(&B)); char *player; switch (B.joueur){ case JOUEUR_BLANC : player = "blanc"; break; case JOUEUR_NOIR : player = "noir"; } term_clear(); print_game(&B); printf("\nJoueur %s, vous etes echec et mat !\n",player); getchar(); B = E; do{ printf("Souhaitez-vous refaire une partie ?\n1. Oui\n2. Non\n\n"); scanf("%lf",&end_game); if(end_game != 1 && end_game != 2){ printf("taper 1 ou 2.\n"); } }while(end_game != 1 && end_game != 2); }while(end_game==1); return 0; }
/* Search and evaluate AI's moves. MV->START_POS and MV->END_POS store AI's best * move. */ void best_move (struct move *mv) { int legal_moves[BOARD_SIZE]; int curr_util = NEG_INF, i; /* Generate legal moves for each black piece. */ for (i = 0; i < BOARD_SIZE; i++) { if (board[i] < chp_null) { init_moves_board (legal_moves); gen_legal_moves (BPLAYER, i, legal_moves); /* For each legal move, evaluate subsequent moves. If this move * leads to current best score, save it. */ int j; for (j = 0; j < BOARD_SIZE; j++) { if (legal_moves[j] == TRUE) { /* Make move and evaluate subsequent moves. */ int attacked_piece = move_piece (i, j); /* If the move wins the game, automatically make it. */ if (game_over () == TRUE) { mv->start_pos = i; mv->end_pos = j; unmove_piece (i, j, attacked_piece); return; } /* Evaluate subsequent moves and choose the best one. */ int move_util = -1 * abp_search (WPLAYER, SEARCH_DEP - 1, NEG_INF, POS_INF); unmove_piece (i, j, attacked_piece); /* If move is best yet, save it. */ if (move_util > curr_util) { mv->start_pos = i; mv->end_pos = j; curr_util = move_util; } } } } } }
static signed char best_move(uint64_t *bo, uint64_t *bs, char side) { uint64_t t_bo, t_bs; unsigned char c, bc = 0; int best = NOMOVE; int pts; for (c = 0; c < 64; c++) { if (!valid_move(bo, bs, side, c)) continue; memcpy(&t_bo, bo, sizeof(uint64_t)); memcpy(&t_bs, bs, sizeof(uint64_t)); pts = move_piece(&t_bo, &t_bs, side, c); if (pts > best) { best = pts; bc = c; } } return (best != NOMOVE) ? bc : NOMOVE; }
/* Perform checks on a move's legality and return TRUE if the move is made. */ int make_move (int player, int start_pos, int end_pos) { if (valid_start_pos (player, start_pos) == FALSE) { return FALSE; } if (valid_end_pos (end_pos) == FALSE) { return FALSE; } if (is_legal_move (player, start_pos, end_pos) == FALSE) { return FALSE; } move_piece (start_pos, end_pos); if (player_in_check (opponent_player (player)) == TRUE) { printf ("Move places opponent in check!\n"); } if (player_has_moves (opponent_player (player)) == FALSE) { checkmate = TRUE; } return TRUE; }
bool GameState::move_piece(const Step &s) { assert(s.is_motion()); const uint8_t c = s.get_color(), p = s.get_piece(), pos = s.get_position(), finish = s.get_finish(); return move_piece(c, p, pos, finish); }
Board* move(short int src_x, short int src_y, short int dest_x,short int dest_y) { Piece* src = get_piece(src_x, src_y); Piece* dest = get_piece(dest_x, dest_y); if (src == NULL) { cout << "\tSource state is not valid." << endl; return NULL; } if (src_x == dest_x && src_y == dest_y) { cout << "\tSource is equal to destination." << endl; return NULL; } if (src->get_color() == White) { if ((src_x - dest_x) == -1) { // Forward move. if (src_y == dest_y && dest == NULL) { // Normal move. return move_piece(src_x, src_y, dest_x, dest_y); } else if (abs(src_y - dest_y) == 1 && dest != NULL && dest->get_color() == Black) { // Kill move. return move_piece(src_x, src_y, dest_x, dest_y); } else { // Impossible move. return NULL; } } else { // Impossible move. return NULL; } } else { // Black Piece if ((src_x - dest_x) == 1) { // Forward move. if (src_y == dest_y && dest == NULL) { // Normal move. return move_piece(src_x, src_y, dest_x, dest_y); } else if (abs(src_y - dest_y) == 1 && dest != NULL && dest->get_color() == White) { // Kill move. return move_piece(src_x, src_y, dest_x, dest_y); } else { // Impossible move. return NULL; } } else { // Impossible move. return NULL; } } return NULL; }
bool test_move() { bool result = true; piece p = new_piece_rh(0, 0, true, true); //the first one (set_up_1()) is with new_piece_rh (int x, int y, bool small, bool horizontal) set_up_1(); for (int dist = 1; dist < NB_PIECES; dist++) for (int i = 0; i < NB_PIECES; i++) { copy_piece(pieces[i], p); move_piece(p, LEFT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) - dist, get_x(p), "move LEFT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move LEFT"); copy_piece(pieces[i], p); move_piece(p, RIGHT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) + dist, get_x(p), "move RIGHT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move RIGHT"); copy_piece(pieces[i], p); move_piece(p, UP, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) + dist, get_y(p), "move UP"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move UP"); copy_piece(pieces[i], p); move_piece(p, DOWN, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) - dist, get_y(p), "move DOWN"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move DOWN"); } tear_down(); //the second one (set_up_2()) is with new_piece(int x, int y, int width, int height, bool move_x, bool move_y) set_up_2(); for (int dist = 1; dist < NB_PIECES; dist++) for (int i = 0; i < NB_PIECES; i++) { copy_piece(pieces[i], p); move_piece(p, LEFT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) - dist, get_x(p), "move LEFT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move LEFT"); copy_piece(pieces[i], p); move_piece(p, RIGHT, dist); if (is_horizontal(pieces[i])) result = result && test_equality_int(get_x(pieces[i]) + dist, get_x(p), "move RIGHT"); else result = result && test_equality_int(get_x(pieces[i]), get_x(p), "move RIGHT"); copy_piece(pieces[i], p); move_piece(p, UP, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) + dist, get_y(p), "move UP"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move UP"); copy_piece(pieces[i], p); move_piece(p, DOWN, dist); if (!is_horizontal(pieces[i])) result = result && test_equality_int(get_y(pieces[i]) - dist, get_y(p), "move DOWN"); else result = result && test_equality_int(get_y(pieces[i]), get_y(p), "move DOWN"); } tear_down(); delete_piece(p); return result; return false; }
int ky_drop(t_program *tetris, t_tetrimino *tet) { while (!(move_piece(tetris, tet, &tetris->posit))) tetris->posit.y++; return (0); }