int minimax(t_grid *grid, int max, int i) { int ret; int tmp; ret = 0; while (i < grid->columns) { if (grid->map[0][i] == '.') { put_piece(grid, i + 1, 'X'); if (check_winner(grid, 'X') != 0) { remove_piece(grid, i + 1); return (i); } tmp = min(grid, 3, 0); if (tmp > max) { tmp = max; ret = i; } remove_piece(grid, i + 1); } i++; } return (ret); }
int min(t_grid *grid, int depth, int i) { int min; int tmp; min = 3000; if (depth <= 0) return (eval(grid)); while (i < grid->columns) { if (grid->map[0][i] == '.') { put_piece(grid, i + 1, 'O'); if (check_winner(grid, 'O') != 0) { remove_piece(grid, i + 1); return (-3000); } tmp = max(grid, depth - 1, 0); if (tmp < min) min = tmp; remove_piece(grid, i + 1); } i++; } return (min); }
int max(t_grid *grid, int depth, int i) { int max; int tmp; max = -3000; if (depth <= 0) return (eval(grid)); while (i < grid->columns) { if (grid->map[0][i] == '.') { put_piece(grid, i + 1, 'X'); if (check_winner(grid, 'X') != 0) { remove_piece(grid, i + 1); return (3000); } tmp = min(grid, depth - 1, 0); if (tmp > max) max = tmp; remove_piece(grid, i + 1); } i++; } return (max); }
/*============================================================================= Return NO_ERROR if MOVE was completely legal in THIS board; otherwise return the appropiate error code (KING_LEFT_IN_CHECK, OPPONENTS_TURN, WRONG_MOVEMENT) Precondition: MOVE.from () and MOVE.to () return values in range [0, SQUARES) Postcondition: The board has been updated to show the effect of MOVE. ===========================================================================*/ MaeBoard::Error MaeBoard::make_move (Move& move, bool is_computer_move) { Error move_error; if (this->board[move.from ()] == EMPTY_SQUARE) return NO_PIECE_IN_SQUARE; BoardSquare start = move.from (); BoardSquare end = move.to (); move.set_moving_piece (board[start].piece); if (board[end] != EMPTY_SQUARE) move.set_captured_piece (board[end].piece); // Assume that moves generated by the computer are pseudo-legal, so don't // bother making a verification. if (!is_computer_move) if ((move_error = can_move (move)) != NO_ERROR) return move_error; label_move (move); save_restore_information (move); Square initial = board[start]; Square final = this->board[end]; remove_piece (start); remove_piece (end); add_piece (end, initial.piece, initial.player); if (move.get_type () == Move::EN_PASSANT_CAPTURE) { uint offset = this->is_whites_turn ? BOARD_SIZE: -((int) BOARD_SIZE); uint position = util::Util::MSB_position (this->en_passant_capture_square) + offset; remove_piece (BoardSquare (position)); } int king_position = util::Util::MSB_position (this->piece[player][Piece::KING]); if (attacks_to (BoardSquare (king_position), true /* include_king */)) { remove_piece (end); add_piece (start, initial.piece, initial.player); if (final.piece != Piece::NULL_PIECE) add_piece (end, final.piece, final.player); if (move.get_type () == Move::EN_PASSANT_CAPTURE) { int offset = (this->is_whites_turn ? BOARD_SIZE: -((int) BOARD_SIZE)); uint position = util::Util::MSB_position (en_passant_capture_square) + offset; add_piece (BoardSquare (position), Piece::PAWN, opponent); } this->game_history.pop (); return KING_LEFT_IN_CHECK; }
void list_hits(GLint hits, GLuint *names){ for(int i = 0; i <= pieces.size()-1; i++){ pieces.at(i)->unpick(); } bool move_pressed = false; for (int i = 0; i < hits; i++){ int name = (GLubyte)names[i*4+3]; if(name == 165){ move_pressed = true; break; } if(name >= 101){ name -= 100; grid_row = 9-(name%8); if(grid_row == 9) grid_row = 1; grid_col = ceil((float)name/8.0f); grid_column = column[grid_col-1]; printf("row = %i col = %i name = %i\n",grid_row,grid_col,name); } } if(move_pressed){ if((grid_pieces[grid_row-1][grid_col-1] == BLACK && gamestate == WHITE_TURN) || (grid_pieces[grid_row-1][grid_col-1] == WHITE && gamestate == BLACK_TURN)) remove_piece(grid_col,grid_row); selected_piece->move((unsigned int)grid_col,(unsigned int)grid_row); swap_turns(); clearMovesList(); selected_piece->unpick(); } pickPiece(grid_col,grid_row); }
///< === 更新棋盤 void update_board(int Board[BOUNDARYSIZE][BOUNDARYSIZE], int X, int Y, int turn) { int num_neighborhood_self = 0; int num_neighborhood_oppo = 0; int num_neighborhood_empt = 0; int num_neighborhood_boun = 0; int Liberties[4]; int NeighboorhoodState[4]; count_neighboorhood_state(Board, X, Y, turn, &num_neighborhood_empt, &num_neighborhood_self, &num_neighborhood_oppo, &num_neighborhood_boun, NeighboorhoodState); if (num_neighborhood_oppo != 0) { count_liberty(X, Y, Board, Liberties); for (int d = 0 ; d < MAXDIRECTION; ++d) { ///< === 對手只有一氣 if (NeighboorhoodState[d] == OPPONENT && Liberties[d] == 1 && Board[ X + DirectionX[ d ] ][ Y + DirectionY[ d ] ] != EMPTY) { remove_piece(Board, X + DirectionX[ d ], Y + DirectionY[ d ], Board[ X + DirectionX[ d ] ][ Y + DirectionY[ d ] ]); } } } Board[X][Y] = turn; }
/*============================================================================= Return TRUE if a piece was removed from square LOCATION ===========================================================================*/ bool MaeBoard::remove_piece (const string& location) { BoardSquare square; if (!Move::translate_to_square (location, square)) return false; return remove_piece (square); }
///< === 提子 void remove_piece(int Board[BOUNDARYSIZE][BOUNDARYSIZE], int X, int Y, int turn) { Board[X][Y] = EMPTY; for (int d = 0; d < MAXDIRECTION; ++d) { if (Board[ X + DirectionX[ d ] ][ Y + DirectionY[ d ] ] == turn ) { remove_piece(Board, X + DirectionX[ d ] , Y + DirectionY[ d ], turn ); } } }
///< === 更新棋盤,並會判斷是否為合法步 int update_board_check(int Board[BOUNDARYSIZE][BOUNDARYSIZE], int X, int Y, int turn) { if ( X < 1 || X > 7 || Y < 1 || Y > 7 || Board[X][Y]!=EMPTY) return 0; int num_neighborhood_self = 0; int num_neighborhood_oppo = 0; int num_neighborhood_empt = 0; int num_neighborhood_boun = 0; int Liberties[4]; int NeighboorhoodState[4]; count_neighboorhood_state(Board, X, Y, turn, &num_neighborhood_empt, &num_neighborhood_self, &num_neighborhood_oppo, &num_neighborhood_boun, NeighboorhoodState); ///< --- 判斷是否為合法步 ///< --- Condition 1 : 空的格點 int legal_flag = 0; count_liberty(X, Y, Board, Liberties); if (num_neighborhood_empt != 0) { legal_flag = 1; } else { ///< --- Condition 2: 有自己的 string,且超過一氣 for (int d = 0; d < MAXDIRECTION; ++d) { if (NeighboorhoodState[d] == SELF && Liberties[d] > 1) { legal_flag = 1; } } if (legal_flag == 0) { ///< --- Condition 3: 對方的子只有一氣 for (int d = 0; d < MAXDIRECTION; ++d) { if (NeighboorhoodState[d] == OPPONENT && Liberties[d] == 1) { legal_flag = 1; } } } } if (legal_flag == 1) { ///< --- 判斷是否有對手的棋子在附近 if (num_neighborhood_oppo != 0) { for (int d = 0 ; d < MAXDIRECTION; ++d) { ///< --- 如果,對手的棋子只有一氣 if (NeighboorhoodState[d] == OPPONENT && Liberties[d] == 1 && Board[X+DirectionX[d]][Y+DirectionY[d]]!=EMPTY) { remove_piece(Board, X + DirectionX[ d ], Y + DirectionY[ d ], Board[ X + DirectionX[ d ] ][ Y + DirectionY[ d ] ]); } } } Board[X][Y] = turn; } return (legal_flag==1)?1:0; }