perfect: refactor

This commit is contained in:
Calcitem 2021-01-21 00:28:56 +08:00
parent b938e0fd66
commit 9b306a0c32
4 changed files with 45 additions and 33 deletions

View File

@ -11,12 +11,15 @@
using namespace std; using namespace std;
unsigned int startTestFromLayer = 0; unsigned int startTestFromLayer = 0;
unsigned int endTestAtLayer = NUM_LAYERS - 1; unsigned int endTestAtLayer = NUM_LAYERS - 1;
#ifdef _DEBUG #ifdef _DEBUG
char databaseDirectory[] = "D:\\database"; char databaseDirectory[] = "D:\\database";
#elif _RELEASE_X64 #elif _RELEASE_X64
char databaseDirectory[] = ""; char databaseDirectory[] = "";
#endif #endif
bool calculateDatabase = false; bool calculateDatabase = false;
int main(void) int main(void)
@ -25,8 +28,8 @@ int main(void)
bool playerOneHuman = false; bool playerOneHuman = false;
bool playerTwoHuman = false; bool playerTwoHuman = false;
char ch[100]; char ch[100];
unsigned int pushFrom, pushTo; unsigned int from, to;
Mill *pos = new Mill(); Mill *mill = new Mill();
PerfectAI *ai = new PerfectAI(databaseDirectory); PerfectAI *ai = new PerfectAI(databaseDirectory);
SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS); SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
@ -42,9 +45,9 @@ int main(void)
// begin // begin
#ifdef SELF_PLAY #ifdef SELF_PLAY
pos->beginNewGame(ai, ai, fieldStruct::playerOne); mill->beginNewGame(ai, ai, fieldStruct::playerOne);
#else #else
pos->beginNewGame(ai, ai, (rand() % 2) ? fieldStruct::playerOne : fieldStruct::playerTwo); mill->beginNewGame(ai, ai, (rand() % 2) ? fieldStruct::playerOne : fieldStruct::playerTwo);
#endif // SELF_PLAY #endif // SELF_PLAY
if (calculateDatabase) { if (calculateDatabase) {
@ -63,17 +66,23 @@ int main(void)
endTestAtLayer; endTestAtLayer;
ai->testLayers(startTestFromLayer, endTestAtLayer); ai->testLayers(startTestFromLayer, endTestAtLayer);
} else { } else {
#ifdef SELF_PLAY #ifdef SELF_PLAY
int moveCount = 0; int moveCount = 0;
#else #else
cout << "Is Player 1 human? (y/n):"; cout << "Is Player 1 human? (y/n):";
cin >> ch; cin >> ch;
if (ch[0] == 'y') if (ch[0] == 'y')
playerOneHuman = true; playerOneHuman = true;
cout << "Is Player 2 human? (y/n):"; cout << "Is Player 2 human? (y/n):";
cin >> ch; cin >> ch;
if (ch[0] == 'y') if (ch[0] == 'y')
playerTwoHuman = true; playerTwoHuman = true;
#endif // SELF_PLAY #endif // SELF_PLAY
@ -81,10 +90,10 @@ int main(void)
// play // play
do { do {
// print board // print board
cout << "\n\n\n\n\n\n\n\n\n\n\n"; cout << "\n\n\n";
pos->getComputersChoice(&pushFrom, &pushTo); mill->getComputersChoice(&from, &to);
cout << "\n\n"; cout << "\n\n";
cout << "\nlast move was from " << (char)(pos->getLastMoveFrom() + 97) << " to " << (char)(pos->getLastMoveTo() + 97) << "\n\n"; cout << "\nlast move was from " << (char)(mill->getLastMoveFrom() + 'a') << " to " << (char)(mill->getLastMoveTo() + 'a') << "\n\n";
#ifdef SELF_PLAY #ifdef SELF_PLAY
moveCount++; moveCount++;
@ -93,15 +102,17 @@ int main(void)
} }
#endif // SELF_PLAY #endif // SELF_PLAY
pos->printBoard(); mill->printBoard();
// Human // Human
if ((pos->getCurrentPlayer() == fieldStruct::playerOne && playerOneHuman) || (pos->getCurrentPlayer() == fieldStruct::playerTwo && playerTwoHuman)) { if ((mill->getCurrentPlayer() == fieldStruct::playerOne && playerOneHuman) ||
(mill->getCurrentPlayer() == fieldStruct::playerTwo && playerTwoHuman)) {
do { do {
// Show text // Show text
if (pos->mustStoneBeRemoved()) if (mill->mustStoneBeRemoved())
cout << "\n Which stone do you want to remove? [a-x]: \n\n\n"; cout << "\n Which stone do you want to remove? [a-x]: \n\n\n";
else if (pos->inSettingPhase()) else if (mill->inSettingPhase())
cout << "\n Where are you going? [a-x]: \n\n\n"; cout << "\n Where are you going? [a-x]: \n\n\n";
else else
cout << "\n Your train? [a-x][a-x]: \n\n\n"; cout << "\n Your train? [a-x][a-x]: \n\n\n";
@ -109,54 +120,54 @@ int main(void)
// get input // get input
cin >> ch; cin >> ch;
if ((ch[0] >= 'a') && (ch[0] <= 'x')) if ((ch[0] >= 'a') && (ch[0] <= 'x'))
pushFrom = ch[0] - 'a'; from = ch[0] - 'a';
else else
pushFrom = fieldStruct::size; from = fieldStruct::size;
if (pos->inSettingPhase()) { if (mill->inSettingPhase()) {
if ((ch[0] >= 'a') && (ch[0] <= 'x')) if ((ch[0] >= 'a') && (ch[0] <= 'x'))
pushTo = ch[0] - 'a'; to = ch[0] - 'a';
else else
pushTo = fieldStruct::size; to = fieldStruct::size;
} else { } else {
if ((ch[1] >= 'a') && (ch[1] <= 'x')) if ((ch[1] >= 'a') && (ch[1] <= 'x'))
pushTo = ch[1] - 'a'; to = ch[1] - 'a';
else else
pushTo = fieldStruct::size; to = fieldStruct::size;
} }
// undo // undo
if (ch[0] == 'u' && ch[1] == 'n' && ch[2] == 'd' && ch[3] == 'o') { if (ch[0] == 'u' && ch[1] == 'n' && ch[2] == 'd' && ch[3] == 'o') {
// undo moves until a human player shall move // undo moves until a human player shall move
do { do {
pos->undo_move(); mill->undoMove();
} while (!((pos->getCurrentPlayer() == fieldStruct::playerOne && playerOneHuman) || (pos->getCurrentPlayer() == fieldStruct::playerTwo && playerTwoHuman))); } while (!((mill->getCurrentPlayer() == fieldStruct::playerOne && playerOneHuman) ||
(mill->getCurrentPlayer() == fieldStruct::playerTwo && playerTwoHuman)));
// reprint board // reprint board
break; break;
} }
} while (pos->doMove(pushFrom, pushTo) == false); } while (mill->doMove(from, to) == false);
// Computer // Computer
} else { } else {
cout << "\n"; cout << "\n";
pos->doMove(pushFrom, pushTo); mill->doMove(from, to);
} }
} while (pos->getWinner() == 0); } while (mill->getWinner() == 0);
// end // end
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
pos->printBoard(); mill->printBoard();
if (pos->getWinner() == fieldStruct::playerOne) if (mill->getWinner() == fieldStruct::playerOne)
cout << "\n Player 1 (o) won after " << pos->getMovesDone() << " move.\n\n"; cout << "\n Player 1 (o) won after " << mill->getMovesDone() << " move.\n\n";
else if (pos->getWinner() == fieldStruct::playerTwo) else if (mill->getWinner() == fieldStruct::playerTwo)
cout << "\n Player 2 (x) won after " << pos->getMovesDone() << " move.\n\n"; cout << "\n Player 2 (x) won after " << mill->getMovesDone() << " move.\n\n";
else if (pos->getWinner() == fieldStruct::gameDrawn) else if (mill->getWinner() == fieldStruct::gameDrawn)
cout << "\n Draw!\n\n"; cout << "\n Draw!\n\n";
else else
cout << "\n A program error has occurred!\n\n"; cout << "\n A program error has occurred!\n\n";

View File

@ -795,10 +795,10 @@ void Mill::printBoard()
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Name: undo_move() // Name: undoMove()
// Desc: Sets the initial board as the current one and apply all (minus one) moves from the move history. // Desc: Sets the initial board as the current one and apply all (minus one) moves from the move history.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Mill::undo_move(void) void Mill::undoMove(void)
{ {
// locals // locals
unsigned int *moveLogFrom_bak = new unsigned int[movesDone]; unsigned int *moveLogFrom_bak = new unsigned int[movesDone];

View File

@ -68,7 +68,7 @@ public:
~Mill(); ~Mill();
// Functions // Functions
void undo_move(); void undoMove();
void beginNewGame(MillAI *firstPlayerAI, MillAI *secondPlayerAI, int currentPlayer); void beginNewGame(MillAI *firstPlayerAI, MillAI *secondPlayerAI, int currentPlayer);
void setAI(int player, MillAI *AI); void setAI(int player, MillAI *AI);
bool doMove(unsigned int pushFrom, unsigned int pushTo); bool doMove(unsigned int pushFrom, unsigned int pushTo);

View File

@ -16,6 +16,7 @@
/*** Konstanten ******************************************************/ /*** Konstanten ******************************************************/
#define MAX_NUM_POS_MOVES (3 * 18) // not (9 * 4) = 36 since the possibilities with 3 stones are more #define MAX_NUM_POS_MOVES (3 * 18) // not (9 * 4) = 36 since the possibilities with 3 stones are more
#define SAFE_DELETE(p) \ #define SAFE_DELETE(p) \
{ \ { \
if (p) \ if (p) \