void mouse_pressed (int button, int state, int x, int y) { int xm, ym; if ( state != 0 ) return; transform_to_model (x, y, &xm, &ym); if (move_state == 0) { if (getNumberOfShikaarLeft_Board (board) > 0) { Move_t move = create_Move (MOVE_T_SHIKAAR_PLACED, xm, ym, board); make_move (move); } else { move_state = 1; move_sx = xm; move_sy = ym; } } else { int dir, type; Move_t m; move_state = 0; dir = get_dir (xm, ym); if (dir == -1) { glutPostRedisplay (); return; } type = MOVE_T_SHIKAAR_MOVED; m = create_Move (type, move_sx, move_sy, dir); make_move (m); } glutPostRedisplay (); }
void test_board_is_done() { Board* board = new_board(3); make_move(board, 6, 'X'); make_move(board, 7, 'X'); make_move(board, 8, 'X'); assert(is_done(board)); destroy_board(board); }
void test_board_is_won_diag2() { Board* board = new_board(3); assert(!is_won(board)); make_move(board, 2, 'X'); make_move(board, 4, 'X'); make_move(board, 6, 'X'); assert(is_won(board)); destroy_board(board); }
void test_board_is_won_column() { Board* board = new_board(3); assert(!is_won(board)); make_move(board, 0, 'X'); make_move(board, 3, 'X'); make_move(board, 6, 'X'); assert(is_won(board)); destroy_board(board); }
void test_board_is_won_row() { Board* board = new_board(3); assert(!is_won(board)); make_move(board, 0, 'X'); make_move(board, 1, 'X'); make_move(board, 2, 'X'); assert(is_won(board)); destroy_board(board); }
void hash_expand_pv( int side_to_move, int mode, int flags, int max_selectivity ) { int i; int pass_count; int new_pv_depth; int new_pv[61]; int new_side_to_move[61]; HashEntry entry; determine_hash_values( side_to_move, board ); new_pv_depth = 0; pass_count = 0; while ( pass_count < 2 ) { new_side_to_move[new_pv_depth] = side_to_move; if ( (new_pv_depth < pv_depth[0]) && (new_pv_depth == 0) ) { if ( (board[pv[0][new_pv_depth]] == EMPTY) && make_move( side_to_move, pv[0][new_pv_depth], TRUE ) ) { new_pv[new_pv_depth] = pv[0][new_pv_depth]; new_pv_depth++; pass_count = 0; } else { hash1 ^= hash_flip_color1; hash2 ^= hash_flip_color2; pass_count++; } } else { find_hash( &entry, mode ); if ( (entry.draft != NO_HASH_MOVE) && (entry.flags & flags) && (entry.selectivity <= max_selectivity) && (board[entry.move[0]] == EMPTY) && make_move( side_to_move, entry.move[0], TRUE ) ) { new_pv[new_pv_depth] = entry.move[0]; new_pv_depth++; pass_count = 0; } else { hash1 ^= hash_flip_color1; hash2 ^= hash_flip_color2; pass_count++; } } side_to_move = OPP( side_to_move ); } for ( i = new_pv_depth - 1; i >= 0; i-- ) unmake_move( new_side_to_move[i], new_pv[i] ); for ( i = 0; i < new_pv_depth; i++ ) pv[0][i] = new_pv[i]; pv_depth[0] = new_pv_depth; }
void test_board_finds_winner() { Board* board = new_board(3); make_move(board, 2, 'X'); make_move(board, 1, 'O'); make_move(board, 4, 'X'); make_move(board, 0, 'O'); make_move(board, 6, 'X'); assert(winner(board) == 'X'); destroy_board(board); }
/** pgn_save(): pgn_save saves the current game into a pgn file. If the file already exists, it appends the game to the end. Created 091407; last modified 091407 **/ void pgn_save(char *file_name) { FILE *pgn_file; char text[80]; GAME_ENTRY *current_entry; pgn_file = fopen(file_name, "at"); if (pgn_file == NULL) { print("%s: file not found.\n"); return; } /* Make sure we have the game result, then print the PGN tags. */ check_result(FALSE); strcpy(pgn_game.tag.result, result_str[zct->game_result]); fprintf(pgn_file, "[Event \"%s\"]\n", pgn_game.tag.event); fprintf(pgn_file, "[Site \"%s\"]\n", pgn_game.tag.site); fprintf(pgn_file, "[Date \"%s\"]\n", pgn_game.tag.date); fprintf(pgn_file, "[Round \"%s\"]\n", pgn_game.tag.round); fprintf(pgn_file, "[White \"%s\"]\n", pgn_game.tag.white); fprintf(pgn_file, "[Black \"%s\"]\n", pgn_game.tag.black); fprintf(pgn_file, "[Result \"%s\"]\n", pgn_game.tag.result); if (strcmp(pgn_game.tag.fen, "") != 0) fprintf(pgn_file, "[FEN \"%s\"]\n", pgn_game.tag.fen); fprintf(pgn_file, "\n"); /* Return to the beginning of the game. */ current_entry = board.game_entry; while (board.game_entry > board.game_stack) unmake_move(); /* Start with the proper move number indicator, depending on the side. We have to check that at least one move has been made before this. */ if (board.game_entry < current_entry) { sprint(text, 80, "%i.%s%M ", board.move_number, board.side_tm == WHITE ? "" : "..", board.game_entry->move); make_move(board.game_entry->move); } /* Wade through the rest of the game. */ while (board.game_entry < current_entry) { if (board.side_tm == WHITE) sprint(text, 80, "%s%i.", text, board.move_number); sprint(text, 80, "%s%M ", text, board.game_entry->move); make_move(board.game_entry->move); if (strlen(text) >= 70) { fprintf(pgn_file, "%s\n", text); strcpy(text, ""); } } fprintf(pgn_file, "%s%s\n\n", text, result_str[zct->game_result]); fclose(pgn_file); }
int ComputerTurn(char** error) { if (gameOver == false && tie == false){ struct move_list* best_move_list = NULL; int number_of_boards_evaluated = 0; int current_move_grade = get_best_moves(curSettings->minimax_depth, board, get_opposite_color(curSettings->user_color), curMovesList, &best_move_list, &number_of_boards_evaluated); // Check for errors if (FAILED_ERROR == current_move_grade) { free_move_list(curMovesList); curMovesList = NULL; free_move_list(best_move_list); best_move_list = NULL; *error = "ERROR: Failed getting best moves."; return -1; } // Make the move make_move(board, &best_move_list->mov); curSettings->next_turn = get_opposite_color(curSettings->next_turn); free_move_list(curMovesList); curMovesList = NULL; free_move_list(posMovesFromCurPos); posMovesFromCurPos = NULL; free(chosenMove); chosenMove = NULL; free_move_list(best_move_list); Game(); } return 0; }
void backtrack(int a[], int k, void* input) { int c[MAXCANDIDATES]; int ncandidates; if (is_a_solution(a, k, input)) { process_solution(a, k, input); } else { k = k + 1; construct_candidates(a, k, input, c, &ncandidates); for (int i = 0; i < ncandidates; i++) { a[k] = c[i]; make_move(a, k, input); backtrack(a, k, input); unmake_move(a, k, input); if (finished) return; } } }
ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) { assert(!pos.checkers()); Color us = pos.side_to_move(); CheckInfo ci(pos); Bitboard dc = ci.dcCandidates; while (dc) { Square from = pop_lsb(&dc); PieceType pt = type_of(pos.piece_on(from)); if (pt == PAWN) continue; // Will be generated together with direct checks Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces(); if (pt == KING) b &= ~PseudoAttacks[QUEEN][ci.ksq]; while (b) *moveList++ = make_move(from, pop_lsb(&b)); } return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces(), &ci) : generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces(), &ci); }
ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) { assert(!pos.checkers()); Color us = pos.side_to_move(); Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us); while (dc) { Square from = pop_lsb(&dc); PieceType pt = type_of(pos.piece_on(from)); if (pt == PAWN) continue; // Will be generated together with direct checks Bitboard b = pos.attacks_from(pt, from) & ~pos.pieces(); if (pt == KING) b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)]; while (b) *moveList++ = make_move(from, pop_lsb(&b)); } return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces()) : generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces()); }
void do_in4_in8(t_list **a, t_list **b, t_list **in) { char **patterns; int count; int *abcd; int *ops; int j; patterns = load_file("patterns/IN4"); count = lstlen(*a); abcd = make_sequence(count, 1, 1, 4); while (count) { ops = make_instr_arr(recognize_4pat(patterns, \ grab_next_n(*a, 4), abcd)); while (*ops != -1) make_move(a, b, in, *ops++); j = -1; while (++j < 4) abcd[j] = abcd[j] + 4; count -= 4; } free(abcd); free_split(patterns); }
int get_shallow_move(int *board, int side) { /* Return the move that gives the best position immediately after */ int i; int move = 0; int score; int flips[20]; int old_board[100]; int best_score = MIN_SCORE; // backup the existing board copy_board(board, old_board); for (i=11; i<89; ++i) { if (legal_move(board,i,side,flips) == 1) { make_move(board,i,side,flips); score = evaluate_board(board, side, 0); if (score > best_score) { best_score = score; move = i; } // reset the board before each iteration copy_board(old_board, board); } } return move; }
void record_a_move(int x, int y, int to_x, int to_y, int captures_only, int depth) { //printf("record(%d,%d,%d,%d,%d,%d)\n",x,y,to_x,to_y,captures_only,depth); if (captures_only && board.cells[to_x][to_y] == 0) return; if (board.cells[x][y]) ++attacked_squares[depth][to_x][to_y]; if (attacked_squares_only == 0) { int check; moves[depth][move_counter[depth]].x = x; moves[depth][move_counter[depth]].y = y; moves[depth][move_counter[depth]].piece = board.cells[x][y]; moves[depth][move_counter[depth]].to_x = to_x; moves[depth][move_counter[depth]].to_y = to_y; moves[depth][move_counter[depth]].captured_piece = board.cells[to_x][to_y]; make_move(&moves[depth][move_counter[depth]]); board.side = !board.side; // these switches are dumb check = in_check(depth); board.side = !board.side; unmake_move(&moves[depth][move_counter[depth]]); if (!check) ++move_counter[depth]; } }
void begin_game(t_env *e) { if (e->my_turn) { gnl_until("0123456"); populate_map(e); } else { e->my_turn = 1; gnl_until("01234"); populate_map(e); } malloc_piece(e); populate_piece(e); if (!make_move(e)) { print_move(0, 0); ft_freemap(e); exit(0); } free_piece(e); e->my_turn = 0; gnl_until("Plateau"); begin_game(e); }
void do_perft(position_t *pos, scored_move_t *ms, int ply, int depth) { scored_move_t *msbase = ms; uint8 stm = Stm(ply); scored_move_t *mv; if (Checked(stm^1)) return; if (Checked(stm)) { ms = generate_evasions(pos, ms, ply); for (mv = msbase; mv < ms; mv++) if (PieceType(Capture(mv->move)) == KING) return; } else { ms = generate_captures(pos, ms, ply); for (mv = msbase; mv < ms; mv++) if (PieceType(Capture(mv->move)) == KING) return; ms = generate_noncaptures(pos, ms, ply); } for (mv = msbase; mv < ms; mv++) { make_move(pos, mv->move, ply); if (depth - 1) do_perft(pos, ms, ply + 1, depth - 1); else if (!Checked(stm)) total_moves++; unmake_move(pos, mv->move, ply); } }
/* Makes a move_t from a supplied string (in long algebraic notation), or returns 0 if it's an invalid move. */ move_t move_from_text(const char* text, board_t* board) { int text_length = strlen(text); if (text_length != 4 && text_length != 5) return 0; move_t move = (((text[1]-'1')*8)+(tolower(text[0])-'a')) + ((((text[3]-'1')*8)+(tolower(text[2])-'a'))<<6); if (text_length == 5) if (board->to_move == WHITE) move |= piece_type_from_char(toupper(text[4]))<<23; else move |= piece_type_from_char(tolower(text[4]))<<23; move_t move_list[MAX_MOVES]; move_t* end_of_move_list = generate_pseudolegal_moves(board, move_list); for (int i = 0; &move_list[i] != end_of_move_list; i++) { if ((move_list[i]&0x7800FFF) == move) { if (IS_CASTLE(move_list[i]) && !is_legal_castle(board, move_list[i])) return 0; make_move(board, move_list[i]); if (!is_king_in_check(board, !board->to_move)) { undo_move(board, move_list[i]); return move_list[i]; } } } return 0; }
void perft_divide(FILE* stream, Board* board, unsigned int depth) { int i, totalMoves = 0; Move moves[256]; if (depth <= 0) { return; } int num_moves = generate_moves(board, moves); for (i = 0; i < num_moves; ++i) { int numDividedMoves = 0; char moveString[8]; sprint_move(moveString, moves[i]); printf("%s ", moveString); make_move(board, moves[i]); numDividedMoves = perft_perft(board, depth - 1); unmake_move(board, moves[i]); totalMoves += numDividedMoves; printf("%i\n", numDividedMoves); } fprintf(stream, "\nMoves: %i\n", num_moves); fprintf(stream, "Nodes: %i\n", totalMoves); }
/* Tests `make_move` and `undo_move` by making and undoing every single move for a set of fen positions, then making sure the resulting board is the same as the original board. */ int test_make_undo_move() { static const char* fen_positions[] = { "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "1B4n1/5k2/5P2/4p2N/P2QR3/p2pb2K/3Pb2R/8 w - - 0 1", "3b4/N2P1p2/1kP4p/p4n1K/2p5/1PPp2P1/1p6/8 w - - 0 1", "8/4p3/3R4/2P4R/P3Pp2/6kp/8/K7 w - - 0 1", "8/8/p3K3/k1N2B2/3P4/6Pp/5P1p/4R3 w - - 0 1", "K7/8/1P3r2/1P2b1p1/8/5p2/1Q2q2n/7k b - - 0 1", "1q1rr3/P1RN1P1p/Kp3p2/2P2p2/b4pP1/n4kb1/8/3Rn3 b - - 0 1", "8/2P5/Bp4Kn/q7/5P2/k7/8/B7 b - - 0 1", "8/8/1B6/8/P1b4K/8/8/1k6 b - - 0 1", "8/P2kp3/7B/1R3r2/1bK4p/Q4pp1/P6N/8 b - - 0 1" }; board_t board[ELEMENTS_IN(fen_positions)], board_original[ELEMENTS_IN(fen_positions)]; move_t move_list[256]; for (int i = 0; i < ELEMENTS_IN(fen_positions); i++) { set_board_from_fen_string(&board[i], fen_positions[i]); memcpy(&board_original[i], &board[i], sizeof(board_t)); } for (int i = 0; i < ELEMENTS_IN(fen_positions); i++) { move_t* end_of_list = generate_pseudolegal_moves(&board[i], move_list); for (int j = 0; move_list[j] != *end_of_list; j++) { make_move(&board[i], move_list[j]); undo_move(&board[i], move_list[j]); if (!compare_boards(&board[i], &board_original[i])) { fprintf(stderr, "test_make_undo_move -- Test \"%s\" failed: move %d.\n", fen_positions[i], j); return TEST_ERROR; } } } return TEST_SUCCESS; }
void do_in8(t_list **a, t_list **b, t_list **in) { do_tr4_in8(a, b, in); do_in4_in8(a, b, in); while (*b) make_move(a, b, in, PA); }
static void setup_child_node(struct node *node, move m, int *LMR_factor) { struct node *child = node + 1; if (node->common->sd.settings.use_repetition_check) { if (is_move_irreversible(node->pos, m)) { child->is_GHI_barrier = true; child->has_repetition_in_history = false; } else { child->is_GHI_barrier = false; child->has_repetition_in_history = node->has_repetition_in_history; } } make_move(child->pos, node->pos, m); debug_trace_tree_push_move(node, m); handle_node_types(node); child->depth = node->depth - PLY; child->beta = -node->alpha; child->alpha = -node->beta; *LMR_factor = get_LMR_factor(node); if (*LMR_factor != 0) { child->depth -= *LMR_factor; child->alpha = -node->alpha - 1; } }
void move_set_attr(board_t *b, move_t *move) { int check, mated; board_t board = *b; if (move_is_capture(b, move)) move->type = CAPTURE; else move->type = NORMAL; if (PIECE(b->square[move->source]) == KING) { int hor = move->destination % 8 - move->source % 8; if (hor > 1) move->type = KINGSIDE_CASTLE; else if (hor < -1) move->type = QUEENSIDE_CASTLE; } make_move(&board, move); check = in_check(&board, board.turn); mated = is_mated(&board, board.turn); if (check && mated) move->state = MOVE_CHECKMATE; else if (check) move->state = MOVE_CHECK; else if (mated) move->state = MOVE_STALEMATE; else move->state = MOVE_NORMAL; }
// Player's turn void player_turn(int board[][BOARD_SIZE], int player) { char turn; int x; int y; if (player == 1) { turn = 'X'; } if (player == -1) { turn = 'O'; } while(1) { printf("Player %c, enter a move: ", turn); scanf("%d %d", &x, &y); if (valid_move(x, y, board) == 1) break; else printf("Sorry, invalid move!\n"); } make_move(x, y, board, player); }
ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) { assert(pos.checkers()); Color us = pos.side_to_move(); Square ksq = pos.square<KING>(us); Bitboard sliderAttacks = 0; Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN); // Find all the squares attacked by slider checkers. We will remove them from // the king evasions in order to skip known illegal moves, which avoids any // useless legality checks later on. while (sliders) { Square checksq = pop_lsb(&sliders); sliderAttacks |= LineBB[checksq][ksq] ^ checksq; } // Generate evasions for king, capture and non capture moves Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks; while (b) *moveList++ = make_move(ksq, pop_lsb(&b)); if (more_than_one(pos.checkers())) return moveList; // Double check, only a king move can save the day // Generate blocking evasions or captures of the checking piece Square checksq = lsb(pos.checkers()); Bitboard target = between_bb(checksq, ksq) | checksq; return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList, target) : generate_all<BLACK, EVASIONS>(pos, moveList, target); }
static void uci_set_position(char *str) { char *c=str, *m; char movestr[10]; move_t move; undo_info_t u[1]; while(isspace(*c)) c++; if(strncasecmp(c, "startpos", 8)==0) { c+=8; while(isspace(*c)) c++; set_position(RootPosition, STARTPOS); } else set_position(RootPosition, c);/*if(strncasecmp(c, "fen", 3)==0) { c+=3; while(isspace(*c)) c++; set_position(RootPosition, (unsigned char*)c); while(*c != '\0' && strncasecmp(c, "moves", 5) != 0) c++; }*/ while(isspace(*c)) c++; if(strncasecmp(c, "moves", 5)==0) { c+=5; while(isspace(*c)) c++; while(*c!='\0') { m=movestr; while(*c!='\0' && !isspace(*c)) *m++ = *c++; *m = '\0'; move = parse_move(RootPosition, movestr); if(move) make_move(RootPosition, move, u); else printf("Illegal move: %s\n", movestr); while(isspace(*c)) c++; } } }
static void MakeMoveCB(Widget, XtPointer client_data, XtPointer) { if (winner() != NO_ONE) initBoard(); make_move((int)(long)client_data); }
/* **AI的核心,得到当前走法的权重 */ int weigh_move(int x, int y, int who, int max_depth, int add) { int bottom, top, i, move_weight = 0, depth_weight = 0, temp; bottom = flips_next; make_move(x, y, who); top = flips_next; for (i = bottom; i < top; ++i) move_weight += (add ? 1 : (-1)) * weight[flips_x[i]][flips_y[i]]; if (max_depth && empties()) { who = OPPOSITE(who); add = !add; bottom = moves_next; get_all(who); top = moves_next; for(i = bottom; i < top; ++i) { temp = weigh_move(moves_x[i], moves_y[i], who, max_depth - 1, add); if(temp < depth_weight) depth_weight = temp; } unget_all(); } undo_move(); return move_weight + depth_weight; }
void upgradePieces_ButtonClick(control* input) { char piece = ResolveLetterFromButtonName(input->name); chosenMove->new_disc = piece; if (is_move_in_move_list(chosenMove, curMovesList) == true){ make_move(board, chosenMove); SwitchButtonHighlight(gameSelectedSquare_control); gameSelectedSquare_control = NULL; curSettings->next_turn = get_opposite_color(curSettings->next_turn); free_move_list(curMovesList); curMovesList = NULL; free_move_list(posMovesFromCurPos); posMovesFromCurPos = NULL; free(chosenMove); chosenMove = NULL; Game(); } // DrawTree if (-1 == FlipTree(&error_global)) { guiQuit = -1; return; } }
Bitmap xperft(int depth) { Bitmap x, k, desde, hasta, r; desde = (Bitmap) board.ply_moves[board.ply - 1]; hasta = (Bitmap) board.ply_moves[board.ply]; if (depth > 1) { x = 0; // xfen();xl(); for (k = desde; k < hasta; k++) { // b0 = board; //DBG make_move(board.moves[k]); movegen(); r = xperft(depth - 1); x += r; unmake_move(); } return x; } else { return hasta - desde; } }