📄 driver.cpp
字号:
/* Copyright (C) Peter Dalton, 2001.
* All rights reserved worldwide.
*
* This software is provided "as is" without express or implied
* warranties. You may freely copy and compile this source into
* applications you distribute provided that the copyright text
* below is included in the resulting source code, for example:
* "Portions Copyright (C) Peter Dalton, 2001"
*/
/***
* File: driver.cpp - Implements driver.h
* -----------------------------------------------------
* Author: Peter Dalton
* Date: 4/10/2001 10:16:32 AM
*
* Description:
This file is the interface for the Checkers class. It provides a menu,
that allows the user to either play checkers with another opponent or the computer.
If the computer is choosen a number of options can be choosen.
Algorithms:
1. MiniMax Search
2. MiniMax Search with Alpha-Beta pruning
3. MiniMax Search with Alpha-Beta pruning and plausable node ordering
4. Iterative Deepening with MiniMax search (1 minute)
Evaluation Functions:
1. Basic (number of pieces I have - number of pieces you have)
2. Personal - Explaned in detail below in the function
3. Change the depth of search using either evaluation function.
*
*/
#include "new_off.h" // This here shows how to avoid compiler errors while using the
#include <ctype.h> // Memory Manager. This example is for illustration, these
#include <string.h> // headers do not redefine the new/delete operators, thus
#include <time.h> // the new_off and new_on headers are not required.
#include "new_on.h"
#define USE_MEMORY_CACHE 1
#include "MemoryManager.h"
#include "Checkers.h"
// ***** Function Prototypes
void eventHandler( void );
void getMove( void );
void mainMenu( void );
void algorithms( void );
void evalFunctions( void );
void displayMoves( PathList *moves );
int PersonalEvaluation( Board *board, bool red );
// ***** Global Variables
Checkers *g_checkers = NULL;
/*******************************************************************************************/
/**
* main():
* Main entry point.
*
* Return Type : int
* Arguments : NONE
*/
int main( void )
{
g_checkers = new Checkers;
eventHandler();
delete g_checkers;
return 0;
}
/*******************************************************************************************/
/**
* eventHandler():
* Controls the game flow.
*
* Return Type : void
* Arguments : NONE
*/
void eventHandler( void )
{
char cmd;
double timer;
bool first = true;
int Num_turns = 1;
mainMenu(); // Display menu initally
do {
cout << ( g_checkers->redsTurn() ? "Red's turn (?):> " : "White's turn (?):> " );
cin >> cmd;
cin.ignore();
cmd = tolower( cmd );
switch (cmd) { // Implement the following menu selections
case 'a': // Select between different AI algorithms
algorithms();
break;
case 'c': // Requests the computer to make the move using current AI Algor.
timer = clock();
g_checkers->computerMove();
cout << *g_checkers->getBoard();
timer = ( clock() - timer )/CLOCKS_PER_SEC;
cout << "\t\tComputer's turn took " << timer << " seconds\n\n";
break;
case 'r': // Put the program into automatic run mode.
Num_turns = 1;
while (Num_turns < 1000 && !g_checkers->gameOver()) {
if (g_checkers->redsTurn()) {
cout << "\nRed's turn: Move number: " << (Num_turns+1)/2 << endl;
}
else {
cout << "\nWhite's turn: Move number: " << (Num_turns+1)/2 << endl;
}
Num_turns++;
timer = clock();
if (!g_checkers->computerMove()) {
break;
}
cout << *g_checkers->getBoard();
timer = ( clock() - timer )/CLOCKS_PER_SEC;
cout << "\t\tComputer's turn took " << timer << " seconds\n\n";
}
break;
case 'e': // Select between possible evaluation algorithms
evalFunctions();
break;
case 'd': // Display board
cout << *g_checkers->getBoard();
break;
case 'g': // Get possible moves for current player
{
PathList *moves = new PathList;
g_checkers->getBoard()->generateMoves( g_checkers->redsTurn(), *moves );
displayMoves( moves );
delete moves;
}
break;
case 'm': // Enter a move
getMove();
break;
case '?': // HELP menu
mainMenu();
break;
case 'q': // Quit
cout << *g_checkers->getBoard();
cout << endl << "\t\tGOODBYE...." << endl << endl;
break;
default: // Invalid Menu selection
cout << "\nInvalid selection ... Please re-enter." << endl;
break;
}
bool endFound = false;
if (g_checkers->gameOver()) {
cout << "\t\tGAME OVER....";
if (g_checkers->getBoard()->NumRedPieces() > 0) {
cout << "RED WINS !!!!!" << endl << endl;
}
else {
cout << "WHITE WINS !!!!!" << endl << endl;
}
endFound = true;
}
else if (g_checkers->staleMate()) {
cout << "\t\tSTALE MATE....";
if (g_checkers->getBoard()->NumRedPieces() > g_checkers->getBoard()->NumWhitePieces()) {
cout << "RED WINS !!!!!" << endl << endl;
}
else {
cout << "WHITE WINS !!!!!" << endl << endl;
}
endFound = true;
}
if (endFound) {
do {
cout << "Play again (Y/N)? ";
cin >> cmd;
cmd = tolower( cmd );
} while (cmd != 'n' && cmd != 'y');
if (cmd == 'n') {
cout << "\t\tGOODBYE...." << endl << endl;
cmd = 'q';
}
else {
g_checkers->getBoard()->init2();
}
}
} while (cmd != 'q');
}
/*******************************************************************************************/
/**
* getMove():
* Lets the user make a move.
*
* Return Type : void
* Arguments : NONE
*/
void getMove( void )
{
pathValue path[MAX_PATHLEN];
char move[80];
int length, num, end=0;
// Read move sequence from keyboard
cout << "Enter move :> ";
cin.getline( move, 70 );
length = strlen( move );
num = 0;
// Only grabs integers from input stream. Ignores any characters
// that are not integers between (0, BOARD_SIZE)
for (int ii = 0; ii < length; ++ii) {
if (isdigit( move[ii] )) {
num = num*10 + move[ii] - 48; // Convert the string to a number
}
else {
if (num > 0 && num < BOARD_SIZE) {
path[++end] = num; // Add the move to the path list
}
num = 0;
}
}
if (num != 0) {
path[++end] = num;
}
path[0] = end; // Add the length to the beginning of the path.
// This function checks the move specified by path. It will return 1 if
// the move is valid, 0 if the move is invalid, or -1 if the move is ambiguous.
int done = g_checkers->makeMove( path );
if (done == 1) { //we made a move
cout << *g_checkers->getBoard();
}
else if (done == -1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -