⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
     cout << "You have entered an ambiguous move." << endl;
	}
  else if (done == -2) {
     cout << "You must make a jump when available." << endl;
	}
  else {
     cout << "You have entered an invalid move." << endl;
	}
} 

/*******************************************************************************************/

/**
 * displayMoves():
 *  Given a list of moves this method will display the list to the user.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	PathList *moves	: The list of moves to be displayed.
 */
void displayMoves( PathList *moves ) 
{
  int length;
  pathValue *currPath;        //pointer to current path move array
   
  if (moves == NULL ) return;

  length = moves->getLength();
  if (length == 0) {
		cout << endl
	       << "There are currently no moves for you to make!. GAME OVER!." << endl
	       << endl;
    return;
  }

  // Loop through every move
  for (int ii = 0; ii < length; ++ii) {
		currPath = moves->getPath( ii );
    cout << "Move[" << ii << "]  =  {";

    // Step through the sequence of legal moves
    for (int jj = 1; jj <= currPath[0]; ++jj) {
			cout << (int)currPath[jj];
			if (jj != currPath[0]) {
				cout << '-';
			}
		}
		cout << '}' << endl;
	}
} 

/*******************************************************************************************/

/**
 * mainMenu():
 *  The main menu.
 * 
 *  Return Type : void 
 *  Arguments   : NONE
 */
void mainMenu( void ) 
{
	cout << endl << endl
			 << "***********************************************************************" << endl
	     << endl
	     << "\tC --> Computer. Computer makes a move." << endl
			 << "\tR --> Run.      Computer vs Computer." << endl
			 << "\tM --> Move.     User makes a move." << endl
			 << "\tG --> Get.      Gets a list of legal moves for current player." << endl
			 << endl
			 << "\tE --> Eval.     Select evaluation function to use." << endl
			 << "\tA --> Algo.     Select between AI Algorithms used." << endl
			 << endl
			 << "\tD --> Display.  Displays the game board." << endl
			 << endl
			 << "\t? --> Help.     Displays command menu." << endl
			 << "\tQ --> Quit.     Terminate." << endl
			 << endl
			 << "***********************************************************************" << endl
			 << endl;
}

/*******************************************************************************************/

/**
 * algorithms():
 *  Allows for the search algorithm to be changed.
 * 
 *  Return Type : void 
 *  Arguments   : NONE
 */
void algorithms( void ) 
{
	cout << endl << endl
			 << "***********************************************************************" << endl
			 << endl
			 << "\tM --> Minimax Search by itself (Default)." << endl
			 << "\tP --> Minimax Search with Alpha-Beta Pruning." << endl
			 << "\tA --> Add Plausible Move Ordering for Alpha-Beta Pruning." << endl
			 << "\tI --> Iterative Deepening with Alpha-Beta Pruning (1 minute)." << endl
			 << endl
			 << "***********************************************************************" << endl
			 << endl << endl << "Enter choice :> ";

	char cmd;
	do {
		cin >> cmd;
		cin.ignore();
		cmd = tolower( cmd );
		switch (cmd) {
			case 'm':
				if (g_checkers->redsTurn()) g_checkers->setRedsAlgorithm( Checkers::MINMAX );
				else                        g_checkers->setWhitesAlgorithm( Checkers::MINMAX );
				break;
			case 'p':
				if (g_checkers->redsTurn()) g_checkers->setRedsAlgorithm( Checkers::PRUNNING );
				else                        g_checkers->setWhitesAlgorithm( Checkers::PRUNNING );
				break;
			case 'a':
				if (g_checkers->redsTurn()) g_checkers->setRedsAlgorithm( Checkers::PLAUSIBLE );
				else                        g_checkers->setWhitesAlgorithm( Checkers::PLAUSIBLE );
				break;
			case 'i':
				if (g_checkers->redsTurn()) g_checkers->setRedsAlgorithm( Checkers::ITERATIVE );
				else                        g_checkers->setWhitesAlgorithm( Checkers::ITERATIVE );
				break;
			default:    // invalid selection
				cout << "\nInvalid selection ... Please re-enter." << endl << "--> ";
				break;
		}
	} while( cmd != 'm' && cmd != 'p' && cmd != 'a' && cmd != 'i' );
}

/*******************************************************************************************/

/**
 * evalFunctions():
 *  Allows for a new evaluation function to be used as well as the search depth to be changed.
 * 
 *  Return Type : void 
 *  Arguments   : NONE
 */
void evalFunctions( void ) 
{
	cout << endl << endl
			 << "***********************************************************************" << endl
			 << endl
			 << "\tB --> Basic Evaluation Function (Default)." << endl
			 << "\tP --> Personal Evaluation Fucntion." << endl
			 << "\tD --> Change the depth of the search algorithm (Default = 4, "
			 <<          "max = 30)." << endl
			 << endl
			 << "***********************************************************************" << endl
			 << endl << "Enter choice :> ";

	char cmd;
	int depth;
	do {
		cin >> cmd;
		cin.ignore();
		cmd = tolower( cmd );
		switch (cmd) {
			case 'b':
				if (g_checkers->redsTurn()) g_checkers->setRedsEvalFunc( NULL );
				else                        g_checkers->setWhitesEvalFunc( NULL );
				break;
			case 'p':
				if (g_checkers->redsTurn()) g_checkers->setRedsEvalFunc( PersonalEvaluation );
				else                        g_checkers->setWhitesEvalFunc( PersonalEvaluation );
				break;
			case 'd':
				cout << "\nEnter the new desired depth for the search algorithm :> ";
				cin >> depth;
				cout << endl;
				while( depth < 1 || depth > 20 ) {  // Invalid depth level
					cout << "\nYou have entered an invalid depth.  Please re-enter\n"
							 << "the desired depth: Range = 1 - 20 :> :";
					cin >> depth;
					cout << endl;
				}
				if (g_checkers->redsTurn()) g_checkers->setRedsSearchDepth( depth );
				else                        g_checkers->setWhitesSearchDepth( depth );
				break;
			default:   // invalid selection
				cout << "\nInvalid selection ... Please re-enter."
						 << endl;
				break;
		}
	} while( cmd != 'b' && cmd != 'p' && cmd != 'd' );
}

/*******************************************************************************************/

/**
 * PersonalEvaluation():
 *  Personal Evaluation: The value of a node is determined by a variety of 
 *  heuristics that are described directly below in the variable declarations.
 * 
 *  Return Type : int -> The value of the board.
 *  Arguments   : 
 *  	Board *board : The board to be evaluated.
 *  	bool red	   : Whether or not it is reds turn.
 */
int PersonalEvaluation( Board *board, bool red )
{
  int Red_vs_White;
	int MyCenterControl = board->CenterControl( red );
	int OpCenterControl = board->CenterControl( !red );
	int MyKingsRow      = board->KingsRow( red );
	int OpKingsRow      = board->KingsRow( !red );
	int MyPawnsExposed  = board->killedPawn( red );
	int MyKingsExposed  = board->killedKing( red );

	int MyNumKings, OpNumKings, Victory, MyNumPieces;
	if (red) {
		MyNumKings = board->NumRedKings();
		OpNumKings = board->NumWhiteKings();

		Red_vs_White = board->NumRedPieces() - board->NumWhitePieces();

		MyNumPieces = board->NumRedPieces();
		if (board->NumWhitePieces() == 0) Victory = 1;
		else                              Victory = 0;
	}
	else {
		MyNumKings = board->NumWhiteKings();
		OpNumKings = board->NumRedKings();

		Red_vs_White = board->NumWhitePieces() - board->NumRedPieces(); 

		MyNumPieces = board->NumWhitePieces();
		if (board->NumRedPieces() == 0) Victory = 1;
		else                            Victory = 0;
	}

	if (red) {
		return ((Red_vs_White*4)    + (MyNumKings*2)       + (OpNumKings*-1)    + 
						(MyCenterControl*0) + (OpCenterControl*-0) + 
						(MyKingsRow*1)      + (OpKingsRow*-1)      + 
						(MyPawnsExposed*-1) + (MyKingsExposed*-1)  + 
						(MyNumPieces*0)     + (Victory*100));
	}
	else {
		return ((Red_vs_White*4)    + (MyNumKings*2)       + (OpNumKings*-1)    + 
						(MyCenterControl*0) + (OpCenterControl*-0) + 
						(MyKingsRow*0)      + (OpKingsRow*-0)      + 
						(MyPawnsExposed*-1) + (MyKingsExposed*-1)  + 
						(MyNumPieces*0)     + (Victory*100));
	}
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -