Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
Arquivo: gui.c Projeto: ofrinevo/Chess
/*present the options of selecting depth for best_moves func and return the chosen the lise of the best_moves*/
itemMove* best_move_choise(SDL_Surface* screen){
	int dep_chosen, depth;
	itemMove* all_moves = getMoves(state);
	itemMove* best_moves;
	dep_chosen = select_depth(screen);
	if (dep_chosen == -1){
		quit = TRUE;
		destroyMoveList(all_moves);
		return NULL;
	}
	if (dep_chosen == 5){ depth = get_best_depth(state); }
	else{ depth = dep_chosen; }
	best_moves = get_best_moves(all_moves, state, state->board, depth);
	destroyMoveList(all_moves);
	return best_moves;
}
Exemplo n.º 3
0
int HighlightBestMove(int blinknum, char** error)
{
	struct move_list* best_move_list = NULL;
	int number_of_boards_evaluated = 0;

	// Get the best next move
	int current_move_grade = get_best_moves(curSettings->minimax_depth, board, curSettings->next_turn, curMovesList, &best_move_list, &number_of_boards_evaluated);

	// Check for errors
	if (FAILED_ERROR == current_move_grade) {
		free_move_list(best_move_list);
		return -1;
	}

	move bestMove = best_move_list->mov;
	position startPos = bestMove.start_pos;
	position endPos = bestMove.end_pos;

	control* startSquare = buttonsBoard[(int)startPos.col][7 - (int)startPos.row];
	control* endSquare = buttonsBoard[(int)endPos.col][7 - (int)endPos.row];

	bool startHiglighted = startSquare->ishighlighted;
	bool endHiglighted = endSquare->ishighlighted;

	startSquare->ishighlighted = 1;
	endSquare->ishighlighted = 1;

	for (int i = 0; i < blinknum; i++){
		SwitchButtonHighlight(startSquare);
		SwitchButtonHighlight(endSquare);

		if (isPawnUpgradePossible(&bestMove, get_piece(board, startPos)))
		{
			char* fileName = ResolveFileNameFromLetter(bestMove.new_disc);
			char* name = ResolveNameFromLetter(bestMove.new_disc);

			if (fileName != NULL){
				control* chessPiece_control;
				if (-1 == Create_panel_from_bmp(
					fileName,
					name,
					(Sint16)(GAMEBOARDBACKGROUND_W - BUTTON_W - MARGIN),
					(Sint16)(0.5*BOARD_H - SQUARE_H - 0.5),
					(Uint16)SQUARE_W,
					(Uint16)SQUARE_H,
					&chessPiece_control, error))
				{
					return -1;
				}
				UINode* chessPiece_node;
				if (-1 == CreateAndAddNodeToTree(chessPiece_control, tree->children[0], &chessPiece_node, error))
				{
					FreeControl(chessPiece_control);
					return -1;
				}
			}
		}

		// DrawTree
		if (-1 == FlipTree(error))
		{
			return -1;
		}
		SDL_Delay(250);
	}



	startSquare->ishighlighted = startHiglighted;
	endSquare->ishighlighted = endHiglighted;

	free_move_list(best_move_list);
	return 0;
}