Пример #1
0
/* Clear the cache (a change was made that might give stale information) */
void Cache_Clear(void)
{
	CACHE_WLOCK;
	FlipTree() ;
	FlipTree() ;
	CACHE_WUNLOCK;
}
Пример #2
0
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;
	}
}
Пример #3
0
void NextPlayer_ButtonClick(control* input)
{
	// white
	if (strcmp(input->name, BUTTONWHITENAME) == 0)
	{
		if (curSettings->next_turn == BLACK)
		{
			curSettings->next_turn = WHITE;
			UINode* node = SearchTreeByName(tree, BUTTONBLACKNAME);
			if (node != NULL){
				SwitchButtonHighlight(node->control);
			}
			SwitchButtonHighlight(input);
		}
	}
	// black
	else if (strcmp(input->name, BUTTONBLACKNAME) == 0)
	{
		if (curSettings->next_turn == WHITE)
		{
			curSettings->next_turn = BLACK;
			UINode* node = SearchTreeByName(tree, BUTTONWHITENAME);
			if (node != NULL){
				SwitchButtonHighlight(node->control);
			}
			SwitchButtonHighlight(input);
		}
	}

	if (-1 == FlipTree(&error_global))
	{
		guiQuit = -1;
		return;
	}
}
Пример #4
0
/* Note: done in single-threaded mode so locking not yet needed */
void Cache_Open(void)
{
	memset(&cache, 0, sizeof(struct cache_data));

	cache.retired_lifespan = TimeOut(fc_stable);
	if (cache.retired_lifespan > 3600) {
		cache.retired_lifespan = 3600;	/* 1 hour tops */
	}

	// Flip once (at start) to set up old tree.
	FlipTree() ;
}
Пример #5
0
/* return 0 if good, 1 if not */
static GOOD_OR_BAD Cache_Add_Common(struct tree_node *tn)
{
	struct tree_opaque *opaque;
	enum { no_add, yes_add, just_update } state = no_add;

	node_show(tn);
	LEVEL_DEBUG("Add to cache sn " SNformat " pointer=%p index=%d size=%d", SNvar(tn->tk.sn), tn->tk.p, tn->tk.extension, tn->dsize);
	CACHE_WLOCK;
	if (cache.time_to_kill < NOW_TIME) {	// old database has timed out
		FlipTree() ;
	}
	if (Globals.cache_size && (cache.old_ram_size + cache.new_ram_size > Globals.cache_size)) {
		// failed size test
		owfree(tn);
	} else if ((opaque = tsearch(tn, &cache.temporary_tree_new, tree_compare))) {
		//printf("Cache_Add_Common to %p\n",opaque);
		if (tn != opaque->key) {
			cache.new_ram_size += sizeof(tn) - sizeof(opaque->key);
			owfree(opaque->key);
			opaque->key = tn;
			state = just_update;
		} else {
			state = yes_add;
			cache.new_ram_size += sizeof(tn);
		}
	} else {					// nothing found or added?!? free our memory segment
		owfree(tn);
	}
	CACHE_WUNLOCK;
	/* Added or updated, update statistics */
	switch (state) {
		case yes_add: // add new entry
			STATLOCK;
			AVERAGE_IN(&new_avg);
			++cache_adds;			/* statistics */
			STATUNLOCK;
			return gbGOOD;
		case just_update: // update the time mark and data
			STATLOCK;
			AVERAGE_MARK(&new_avg);
			++cache_adds;			/* statistics */
			STATUNLOCK;
			return gbGOOD;
		default: // unable to add
			return gbBAD;
	}
}
Пример #6
0
void SetPiece_ButtonClick(control* input)
{
	SwitchButtonHighlight(input);
	if (setPieces == 0)
	{
		setPieces = 1;
	}
	else
	{
		setPieces = 0;
	}

	// DrawTree
	if (-1 == FlipTree(&error_global))
	{
		guiQuit = -1;
		return;
	}
}
Пример #7
0
void ChooseMode_ButtonClick(control* input)
{
	// PVP
	if (strcmp(input->name, BUTTONPVPNAME) == 0)
	{
		if (curSettings->game_mode == PLAYER_VS_AI_GAME_MODE)
		{
			curSettings->game_mode = TWO_PLAYERS_GAME_MODE;
			UINode* node = SearchTreeByName(tree, BUTTONPVCNAME);
			if (node != NULL){
				SwitchButtonHighlight(node->control);
			}
			SwitchButtonHighlight(input);
		}
	}
	// PVC
	else if (strcmp(input->name, BUTTONPVCNAME) == 0)
	{
		if (curSettings->game_mode == TWO_PLAYERS_GAME_MODE)
		{
			curSettings->game_mode = PLAYER_VS_AI_GAME_MODE;
			UINode* node = SearchTreeByName(tree, BUTTONPVPNAME);
			if (node != NULL){
				SwitchButtonHighlight(node->control);
			}
			SwitchButtonHighlight(input);
		}
	}

	// DrawTree
	if (-1 == FlipTree(&error_global))
	{
		guiQuit = -1;
		return;
	}
}
Пример #8
0
/* retire the cache (flip) if too old, and start a new one (keep the old one for a while) */
static void Cache_Add_Alias_Common(struct alias_tree_node *atn)
{
	struct tree_opaque *opaque;

	CACHE_WLOCK;
	if (cache.time_to_kill < NOW_TIME) {	// old database has timed out
		FlipTree() ;
	}
	if (Globals.cache_size && (cache.old_ram_size + cache.new_ram_size > Globals.cache_size)) {
		// failed size test
		owfree(atn);
	} else if ((opaque = tsearch(atn, &cache.temporary_alias_tree_new, alias_tree_compare))) {
		if ( (void *)atn != (void *) (opaque->key) ) {
			cache.new_ram_size += sizeof(atn) - sizeof(opaque->key);
			owfree(opaque->key);
			opaque->key = (void *) atn;
		} else {
			cache.new_ram_size += sizeof(atn);
		}
	} else {					// nothing found or added?!? free our memory segment
		owfree(atn);
	}
	CACHE_WUNLOCK;
}
Пример #9
0
void Game()
{
	check = false;

	FreeTree(tree);
	if (NULL != buttonsBoard){
		FreeButtonsBoard();
	}
	if (-1 == EventHandler_init(&Quit, &error_global))
	{
		guiQuit = -1;
		return;
	}

	chosenMove = NULL;

	gameSelectedSquare_control = NULL;

	showDepthOptions = false;

	get_moves_for_color(board, curSettings->next_turn, &curMovesList);

	CheckGameOver();

	control* window;
	if (-1 == Create_window(GAMEBOARDBACKGROUND_W, GAMEBOARDBACKGROUND_H, &window, &error_global))
	{
		guiQuit = -1;
		return;
	}


	if (-1 == CreateTree(window, &tree, &error_global))
	{
		FreeControl(window);
		guiQuit = -1;
		return;
	}


	control* gameBoarBackground_control;
	if (-1 == Create_panel_from_bmp(
		GAMEBOARDBACKGROUNDFILENAME,
		GAMEBOARDBACKGROUNDNAME,
		0,
		0,
		(Uint16)GAMEBOARDBACKGROUND_W,
		(Uint16)GAMEBOARDBACKGROUND_H,
		&gameBoarBackground_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* gameBoarBackground_node;
	if (-1 == CreateAndAddNodeToTree(gameBoarBackground_control, tree, &gameBoarBackground_node, &error_global))
	{
		FreeControl(gameBoarBackground_control);
		guiQuit = -1;
		return;
	}

	if (-1 == buildBoardUITree(&error_global))
	{
		guiQuit = -1;
		return;
	}

	Sint16 quitButton_x_location = (Sint16)(GAMEBOARDBACKGROUND_W - BUTTON_W - 0.5 * MARGIN);
	Sint16 quitButton_y_location = (Sint16)(GAMEBOARDBACKGROUND_H - BUTTON_H - 1.5 * MARGIN);
	control* quitButton_control;
	if (-1 == Create_Button_from_bmp(
		BUTTONQUITFILENAME,
		BUTTONQUITFILENAME,
		BUTTONQUITNAME,
		quitButton_x_location,
		quitButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&Quit_ButtonClick,
		&quitButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* quitButton_node;
	if (-1 == CreateAndAddNodeToTree(quitButton_control, gameBoarBackground_node, &quitButton_node, &error_global))
	{
		FreeControl(quitButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(quitButton_control, &error_global))
	{
		FreeControl(quitButton_control);
		guiQuit = -1;
		return;
	}


	Sint16 bestButton_x_location = quitButton_x_location;
	Sint16 bestButton_y_location = (Sint16)(quitButton_y_location - BUTTON_H - 1.5 * MARGIN);
	control* bestButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONBESTMOVEFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONBESTMOVENAME,
		bestButton_x_location,
		bestButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&GameBoardBest_ButtonClick,
		&bestButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* bestButton_node;
	if (-1 == CreateAndAddNodeToTree(bestButton_control, gameBoarBackground_node, &bestButton_node, &error_global))
	{
		FreeControl(bestButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(bestButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}

	int saveButton_x_location = quitButton_x_location;
	int saveButton_y_location = 2 * MARGIN;
	control* saveButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONSAVEFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONSAVENAME,
		saveButton_x_location,
		saveButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&GameBoardSave_ButtonClick,
		&saveButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}

	UINode* saveButton_node;
	if (-1 == CreateAndAddNodeToTree(saveButton_control, gameBoarBackground_node, &saveButton_node, &error_global))
	{
		FreeControl(saveButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(saveButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}


	Sint16 mainMenuButton_x_location = (Sint16)(quitButton_x_location);
	Sint16 mainMenuButton_y_location = (Sint16)(saveButton_y_location + BUTTON_H + 1.5 *MARGIN);
	control* mainMenuButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONMAINMENUFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONMAINMENUNAME,
		mainMenuButton_x_location,
		mainMenuButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&GameBoardMainMenu_ButtonClick,
		&mainMenuButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* mainMenuButton_node;
	if (-1 == CreateAndAddNodeToTree(mainMenuButton_control, gameBoarBackground_node, &mainMenuButton_node, &error_global))
	{
		FreeControl(mainMenuButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(mainMenuButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}

	if (check)
	{

		char* filename = NULL;
		char* name = NULL;

		if (curSettings->next_turn == WHITE)
		{
			filename = LABELBLACKCHECKFILENAME;
			name = LABELBLACKCHECKNAME;
		}
		else
		{
			filename = LABELWHITECHECKFILENAME;
			name = LABELWHITECHECKNAME;
		}

		int checkLabel_x_location = mainMenuButton_x_location;
		int checkabel_y_location = mainMenuButton_y_location + BUTTON_H + MARGIN;

		control* checkLabel_control;
		if (-1 == Create_panel_from_bmp(
			filename,
			name,
			checkLabel_x_location,
			checkabel_y_location,
			GAMEBOARDBACKGROUND_W,
			GAMEBOARDBACKGROUND_H, &checkLabel_control, &error_global))
		{
			guiQuit = -1;
			return;
		}
		UINode* checkLabel_node;
		if (-1 == CreateAndAddNodeToTree(checkLabel_control, gameBoarBackground_node, &checkLabel_node, &error_global))
		{
			FreeControl(checkLabel_control);
			guiQuit = -1;
			return;
		}
	}

	if (gameOver)
	{
		Sint16 checkMateLabel_x_location = 0;
		Sint16 checkMateLabel_y_location = (Sint16)(0.25 * BOARD_H - 20);
		control* checkMateLabel_control;
		if (-1 == Create_panel_from_bmp(
			LABELCHECKMATEFILENAME,
			LABELCHECKMATENAME,
			checkMateLabel_x_location,
			checkMateLabel_y_location,
			GAMEBOARDBACKGROUND_W,
			GAMEBOARDBACKGROUND_H,
			&checkMateLabel_control,
			&error_global))
		{
			guiQuit = -1;
			return;
		}

		UINode* checkMateLabel_node;
		if (-1 == CreateAndAddNodeToTree(checkMateLabel_control, board_node, &checkMateLabel_node, &error_global))
		{
			FreeControl(checkMateLabel_control);
			guiQuit = -1;
			return;
		}

		char* filename = NULL;
		char* name = NULL;

		if (curSettings->next_turn == WHITE)
		{
			filename = LABELBLACKWINSFILENAME;
			name = LABELBLACKWINSNAME;
		}
		else
		{
			filename = LABELWHITEWINSFILENAME;
			name = LABELWHITEWINSNAME;
		}

		Sint16 winsLabel_x_location = 0;
		Sint16 winsLabel_y_location = (Sint16)(0.5 * BOARD_H - 20);
		control* winsLabel_control;
		if (-1 == Create_panel_from_bmp(
			filename,
			name,
			winsLabel_x_location,
			winsLabel_y_location,
			GAMEBOARDBACKGROUND_W,
			GAMEBOARDBACKGROUND_H,
			&winsLabel_control,
			&error_global))
		{
			guiQuit = -1;
			return;
		}
		UINode* winsLabel_node;
		if (-1 == CreateAndAddNodeToTree(winsLabel_control, board_node, &winsLabel_node, &error_global))
		{
			FreeControl(winsLabel_control);
			guiQuit = -1;
			return;
		}
	}
	else if (tie)
	{
		Sint16 tieLabel_x_location = 0;
		Sint16 tieLabel_y_location = (Sint16)(0.25 * BOARD_H - 20);
		control* tieLabel_control;
		if (-1 == Create_panel_from_bmp(
			LABELTIEFILENAME,
			LABELTIENAME,
			tieLabel_x_location,
			tieLabel_y_location,
			GAMEBOARDBACKGROUND_W,
			GAMEBOARDBACKGROUND_H,
			&tieLabel_control,
			&error_global))
		{
			guiQuit = -1;
			return;
		}
		UINode* tieLabel_node;
		if (-1 == CreateAndAddNodeToTree(tieLabel_control, board_node, &tieLabel_node, &error_global))
		{
			FreeControl(tieLabel_control);
			guiQuit = -1;
			return;
		}

		Sint16 gameOverLabel_x_location = 0;
		Sint16 gameOverLabel_y_location = (Sint16)(0.50 * BOARD_H - 20);
		control* gameOverLabel_control;
		if (-1 == Create_panel_from_bmp(
			LABELGAMEOVERFILENAME,
			LABELGAMEOVERNAME,
			gameOverLabel_x_location,
			gameOverLabel_y_location,
			GAMEBOARDBACKGROUND_W,
			GAMEBOARDBACKGROUND_H,
			&gameOverLabel_control,
			&error_global))
		{
			guiQuit = -1;
			return;
		}
		UINode* gameOverLabel_node;
		if (-1 == CreateAndAddNodeToTree(gameOverLabel_control, board_node, &gameOverLabel_node, &error_global))
		{
			FreeControl(gameOverLabel_control);
			guiQuit = -1;
			return;
		}
	}



	// DrawTree
	if (-1 == FlipTree(&error_global))
	{
		guiQuit = -1;
		return;
	}



	if (PLAYER_VS_AI_GAME_MODE == curSettings->game_mode && curSettings->next_turn != curSettings->user_color)
	{
		ComputerTurn(&error_global);
	}
}
Пример #10
0
void GameBoardSquare_ButtonClick(control* input)
{
	if ((curSettings->next_turn == curSettings->user_color || curSettings->game_mode == TWO_PLAYERS_GAME_MODE || isUpgrade) && gameOver == false){
		if (input == gameSelectedSquare_control)
		{
			switchOffAllButtons();
			gameSelectedSquare_control = NULL;
			free_move_list(posMovesFromCurPos);
			posMovesFromCurPos = NULL;
		}
		else if (gameSelectedSquare_control == NULL)
		{
			position chosenPos = GetPosOfSquare(input);

			if (is_piece_of_color(get_piece(board, chosenPos), curSettings->next_turn) == true){
				SwitchButtonHighlight(input);
				gameSelectedSquare_control = input;
				get_moves_from_pos(curMovesList, chosenPos, &posMovesFromCurPos);
				HightlightPosMoves(posMovesFromCurPos);
			}
		}
		else if (gameSelectedSquare_control != NULL)
		{
			position startPos = GetPosOfSquare(gameSelectedSquare_control);

			position endPos = GetPosOfSquare(input);

			chosenMove = (move*)(calloc(1, sizeof(move)));
			if (chosenMove == NULL)
			{
				guiQuit = -1;
				return;
			}
			chosenMove->start_pos = startPos;
			chosenMove->end_pos = endPos;
			chosenMove->new_disc = EMPTY;

			if (is_move_in_move_list(chosenMove, curMovesList) == true){
				if (isPawnUpgradePossible(chosenMove, board[(int)startPos.col][(int)startPos.row]) == true){
					if (-1 == DrawPiecesOnSidePanelFilterColor(tree->children[0], &upgradePieces_ButtonClick, curSettings->next_turn, &error_global))
					{
						free(chosenMove);
						chosenMove = NULL;
						guiQuit = -1;

					}
					isUpgrade = true;
				}
				else
				{
					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();
					return;
				}
			}
		}

		// DrawTree
		if (-1 == FlipTree(&error_global))
		{
			guiQuit = -1;
			return;
		}
	}
}
Пример #11
0
void GameBoardBest_ButtonClick(control * input)
{
	if (gameOver == 0){
		if (showDepthOptions == false){
			showDepthOptions = true;
			input->ishighlighted = 1;
		}
		else
		{
			showDepthOptions = false;
			input->ishighlighted = 0;
		}
		if (showDepthOptions){
			if (curSettings->game_mode == TWO_PLAYERS_GAME_MODE){


				UINode* gameBoarBackground_node = tree->children[0];
				int oneButton_x_location = (Sint16)(BOARD_W + 0.5 * MARGIN);
				int oneButton_y_location = (Sint16)(BOARD_H *0.5 + 0.5 * NUMBUTTON_H);

				control* oneButton_control;
				if (-1 == Create_Button_from_bmp_transHighlight(
					BUTTON1FILENAME,
					NUMBUTTONHIGHLIGHTEDFILENAME,
					BUTTON1NAME,
					oneButton_x_location,
					oneButton_y_location,
					(Uint16)NUMBUTTON_W,
					(Uint16)NUMBUTTON_H,
					&GameMinimaxDepth_ButtonClick,
					&oneButton_control,
					&error_global))
				{
					guiQuit = -1;
					return;
				}
				UINode* oneButton_node;
				if (-1 == CreateAndAddNodeToTree(oneButton_control, gameBoarBackground_node, &oneButton_node, &error_global))
				{
					FreeControl(oneButton_control);
					guiQuit = -1;
					return;
				}
				if (-1 == AddToListeners(oneButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}


				Sint16 twoButton_x_location = (Sint16)(oneButton_x_location + NUMBUTTON_W + 0.5 * MARGIN);
				Sint16 twoButton_y_location = (Sint16)(oneButton_y_location);

				control* twoButton_control;
				if (-1 == Create_Button_from_bmp_transHighlight(
					BUTTON2FILENAME,
					NUMBUTTONHIGHLIGHTEDFILENAME,
					BUTTON2NAME,
					twoButton_x_location,
					twoButton_y_location,
					(Uint16)NUMBUTTON_W,
					(Uint16)NUMBUTTON_H,
					&GameMinimaxDepth_ButtonClick,
					&twoButton_control,
					&error_global))
				{
					guiQuit = -1;
					return;
				}
				UINode* twoButton_node;
				if (-1 == CreateAndAddNodeToTree(twoButton_control, gameBoarBackground_node, &twoButton_node, &error_global))
				{
					FreeControl(twoButton_control);
					guiQuit = -1;
					return;
				}
				if (-1 == AddToListeners(twoButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}


				Sint16 threeButton_x_location = (Sint16)(twoButton_x_location + NUMBUTTON_W + 0.5 * MARGIN);
				Sint16 threeButton_y_location = (Sint16)(oneButton_y_location);

				control* threeButton_control;
				if (-1 == Create_Button_from_bmp_transHighlight(
					BUTTON3FILENAME,
					NUMBUTTONHIGHLIGHTEDFILENAME,
					BUTTON3NAME,
					threeButton_x_location,
					threeButton_y_location,
					(Uint16)NUMBUTTON_W,
					(Uint16)NUMBUTTON_H,
					&GameMinimaxDepth_ButtonClick,
					&threeButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}
				UINode* threeButton_node;
				if (-1 == CreateAndAddNodeToTree(threeButton_control, gameBoarBackground_node, &threeButton_node, &error_global))
				{
					FreeControl(threeButton_control);
					guiQuit = -1;
					return;
				}
				if (-1 == AddToListeners(threeButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}

				Sint16 fourButton_x_location = (Sint16)(threeButton_x_location + NUMBUTTON_W + 0.5 * MARGIN);
				Sint16 fourButton_y_location = (Sint16)(oneButton_y_location);
				control* fourButton_control;
				if (-1 == Create_Button_from_bmp_transHighlight(
					BUTTON4FILENAME,
					NUMBUTTONHIGHLIGHTEDFILENAME,
					BUTTON4NAME,
					fourButton_x_location,
					fourButton_y_location,
					(Uint16)NUMBUTTON_W,
					(Uint16)NUMBUTTON_H,
					&GameMinimaxDepth_ButtonClick,
					&fourButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}
				UINode* fourButton_node;
				if (-1 == CreateAndAddNodeToTree(fourButton_control, gameBoarBackground_node, &fourButton_node, &error_global))
				{
					FreeControl(fourButton_control);
					guiQuit = -1;
					return;
				}
				if (-1 == AddToListeners(fourButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}

				Sint16 bestButton_x_location = (Sint16)(fourButton_x_location + NUMBUTTON_W + 0.5 *MARGIN);
				Sint16 bestButton_y_location = (Sint16)(oneButton_y_location);
				control* bestButton_control;
				if (-1 == Create_Button_from_bmp_transHighlight(
					BUTTONBESTFILENAME,
					BUTTONBESTHIGHLIGHTFILENAME,
					BUTTONBESTNAME,
					bestButton_x_location,
					bestButton_y_location,
					(Uint16)NUMBUTTON_W,
					(Uint16)NUMBUTTON_H,
					&GameMinimaxDepth_ButtonClick, &bestButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}
				UINode* bestButton_node;
				if (-1 == CreateAndAddNodeToTree(bestButton_control, gameBoarBackground_node, &bestButton_node, &error_global))
				{
					FreeControl(bestButton_control);
					guiQuit = -1;
					return;
				}
				if (-1 == AddToListeners(bestButton_control, &error_global))
				{
					guiQuit = -1;
					return;
				}

				switch (curSettings->minimax_depth){
				case 1:
					oneButton_control->ishighlighted = 1;
					break;
				case 2:
					twoButton_control->ishighlighted = 1;
					break;
				case 3:
					threeButton_control->ishighlighted = 1;
					break;
				case 4:
					fourButton_control->ishighlighted = 1;
					break;
				case MAX_MINIMAX_DEPTH_POSSIBLE:
					bestButton_control->ishighlighted = 1;
					break;
				}
			}

			if (-1 == HighlightBestMove(BLINKNUM, &error_global))
			{
				guiQuit = -1;
				return;
			}

			// DrawTree
			if (-1 == FlipTree(&error_global))
			{
				guiQuit = -1;
				return;
			}
		}
		else
		{
			Game();
		}
	}
}
Пример #12
0
void GameMinimaxDepth_ButtonClick(control* input)
{
	// 1
	if (strcmp(input->name, BUTTON1NAME) == 0)
	{
		if (curSettings->minimax_depth != 1)
		{
			curSettings->minimax_depth = 1;
			SwitchOffHighlightAllMinimaxDepths();
			SwitchOnHighlightbyName(BUTTON1NAME);
		}
	}
	// 2
	else if (strcmp(input->name, BUTTON2NAME) == 0)
	{
		if (curSettings->minimax_depth != 2)
		{
			curSettings->minimax_depth = 2;
			SwitchOffHighlightAllMinimaxDepths();
			SwitchOnHighlightbyName(BUTTON2NAME);
		}
	}
	// 3
	else if (strcmp(input->name, BUTTON3NAME) == 0)
	{
		if (curSettings->minimax_depth != 3)
		{
			curSettings->minimax_depth = 3;
			SwitchOffHighlightAllMinimaxDepths();
			SwitchOnHighlightbyName(BUTTON3NAME);
		}
	}
	// 4 
	else if (strcmp(input->name, BUTTON4NAME) == 0)
	{
		if (curSettings->minimax_depth != 4)
		{
			curSettings->minimax_depth = 4;
			SwitchOffHighlightAllMinimaxDepths();
			SwitchOnHighlightbyName(BUTTON4NAME);
		}
	}
	// Best
	else if (strcmp(input->name, BUTTONBESTNAME) == 0)
	{
		if (curSettings->minimax_depth != MAX_MINIMAX_DEPTH_POSSIBLE)
		{
			curSettings->minimax_depth = get_best_depth(board);
			SwitchOffHighlightAllMinimaxDepths();
			SwitchOnHighlightbyName(BUTTONBESTNAME);
		}
	}

	if (-1 == HighlightBestMove(BLINKNUM, &error_global))
	{
		guiQuit = -1;
		return;
	}

	// DrawTree
	if (-1 == FlipTree(&error_global))
	{
		guiQuit = -1;
		return;
	}
}
Пример #13
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;
}
Пример #14
0
void SettingMenu()
{
	FreeTree(tree);
	if (-1 == EventHandler_init(&Quit, &error_global))
	{
		guiQuit = -1;
		return;
	}

	control* window;
	if (-1 == Create_window(GAMESETTING_W, GAMESETTING_H, &window, &error_global))
	{
		guiQuit = -1;
		return;
	}
	if (-1 == CreateTree(window, &tree, &error_global))
	{
		FreeControl(window);
		guiQuit = -1;
		return;
	}

	// Drawing all controls

	control* gameSettingBackground_control;
	if(-1 ==Create_panel_from_bmp(
		GAMESETTINGFILENAME,
		GAMESETTINGNAME,
		0,
		0,
		(Uint16)GAMESETTING_W,
		(Uint16)GAMESETTING_H,
		&gameSettingBackground_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* gameSettingBackground_node;
	if (-1 == CreateAndAddNodeToTree(gameSettingBackground_control, tree, &gameSettingBackground_node, &error_global))
	{
		FreeControl(gameSettingBackground_control);
		guiQuit = -1;
		return;
	}

	int pvpButtonn_x_location = 285;
	int pvpButton_y_location = 45;
	control* pvpButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONPVPFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONPVPNAME,
		pvpButtonn_x_location,
		pvpButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&ChooseMode_ButtonClick,
		&pvpButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}

	UINode* pvpButton_node;
	if (-1 == CreateAndAddNodeToTree(pvpButton_control, gameSettingBackground_node, &pvpButton_node, &error_global))
	{
		FreeControl(pvpButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(pvpButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}

	int pvcButtonn_x_location = pvpButtonn_x_location + BUTTON_W + MARGIN;//227
	int pvcButton_y_location = pvpButton_y_location;
	control* pvcButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONPVCFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONPVCNAME,
		pvcButtonn_x_location,
		pvcButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&ChooseMode_ButtonClick,
		&pvcButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* pvcButton_node;
	if (-1 == CreateAndAddNodeToTree(pvcButton_control, gameSettingBackground_node, &pvcButton_node, &error_global))
	{
		FreeControl(pvcButton_control);
		guiQuit = -1;
		return;
	}

	if (-1 == AddToListeners(pvcButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}

	int setPiecesButton_x_location = pvpButtonn_x_location;
	int setPiecesButton_y_location = 133;

	control* setPiecesButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONSETPIECESFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONSETPIECESNAME,
		setPiecesButton_x_location,
		setPiecesButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&SetPiece_ButtonClick,
		&setPiecesButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* setPiecesButton_node;
	if (-1 == CreateAndAddNodeToTree(setPiecesButton_control, gameSettingBackground_node, &setPiecesButton_node, &error_global))
	{
		FreeControl(setPiecesButton_control);
		guiQuit = -1;
		return;
	}

	if (-1 == AddToListeners(setPiecesButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}

	int whiteButton_x_location = pvpButtonn_x_location;
	int whiteButton_y_location = 222;
	control* whiteButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONWHITEFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONWHITENAME,
		whiteButton_x_location,
		whiteButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&NextPlayer_ButtonClick,
		&whiteButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* whiteButton_node;
	if (-1 == CreateAndAddNodeToTree(whiteButton_control, gameSettingBackground_node, &whiteButton_node, &error_global))
	{
		FreeControl(whiteButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(whiteButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}

	int blackButton_x_location = pvcButtonn_x_location;
	int blackButton_y_location = 222;
	control* blackButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONBLACKFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONBLACKNAME,
		blackButton_x_location,
		blackButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&NextPlayer_ButtonClick,
		&blackButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* blackButton_node;
	if (-1 == CreateAndAddNodeToTree(blackButton_control, gameSettingBackground_node, &blackButton_node, &error_global))
	{
		FreeControl(blackButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(blackButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}


	int cancelButton_x_location = 44;
	int cancelButton_y_location = 398;
	control* cancelButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONCANCELFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONCANCELNAME,
		cancelButton_x_location,
		cancelButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&GameSettingsCancel_ButtonClick,
		&cancelButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* cancelButton_node;
	if (-1 == CreateAndAddNodeToTree(cancelButton_control, gameSettingBackground_node, &cancelButton_node, &error_global))
	{
		FreeControl(cancelButton_control); 
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(cancelButton_control, &error_global))
	{
		guiQuit = -1;
		return;
	}

	int nextButton_x_location = cancelButton_x_location;
	int nextButton_y_location = 474;
	control* nextButton_control;
	if (-1 == Create_Button_from_bmp_transHighlight(
		BUTTONNEXTFILENAME,
		BUTTONTRANSPARENTHIGHLIGHTEDFILENAME,
		BUTTONNEXTNAME,
		nextButton_x_location,
		nextButton_y_location,
		(Uint16)BUTTON_W,
		(Uint16)BUTTON_H,
		&GameSettingsNext_ButtonClick,
		&nextButton_control,
		&error_global))
	{
		guiQuit = -1;
		return;
	}
	UINode* nextButton_node;
	if (-1 == CreateAndAddNodeToTree(nextButton_control, gameSettingBackground_node, &nextButton_node, &error_global))
	{
		FreeControl(nextButton_control);
		guiQuit = -1;
		return;
	}
	if (-1 == AddToListeners(nextButton_control, &error_global))
	{
		guiQuit = -1;;
		return;
	}

	if (curSettings->game_mode == TWO_PLAYERS_GAME_MODE)
	{
		pvpButton_control->ishighlighted = 1;
	}
	else
	{
		pvcButton_control->ishighlighted = 1;
	}

	if (curSettings->next_turn == WHITE)
	{
		whiteButton_control->ishighlighted = 1;
	}
	else
	{
		blackButton_control->ishighlighted = 1;
	}

	if (1 == setPieces)
	{
		setPiecesButton_control->ishighlighted = 1;
	}

	// DrawTree
	if (-1 == FlipTree(&error_global))
	{
		guiQuit = -1;
		return;
	}
}