perfect: KI -> AI

This commit is contained in:
Calcitem 2021-01-20 00:32:21 +08:00
parent 672486f176
commit 1aef4bbc4e
9 changed files with 50 additions and 50 deletions

View File

@ -27,7 +27,7 @@ int main(void)
char ch[100]; char ch[100];
unsigned int pushFrom, pushTo; unsigned int pushFrom, pushTo;
Position *myGame = new Position(); Position *myGame = new Position();
perfectAI *myKI = new perfectAI(databaseDirectory); perfectAI *myAI = new perfectAI(databaseDirectory);
SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS); SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
srand(GetTickCount()); srand(GetTickCount());
@ -37,24 +37,24 @@ int main(void)
cout << "* Muehle *" << endl; cout << "* Muehle *" << endl;
cout << "*************************" << endl << endl; cout << "*************************" << endl << endl;
myKI->setDatabasePath(databaseDirectory); myAI->setDatabasePath(databaseDirectory);
// begin // begin
#ifdef SELF_PLAY #ifdef SELF_PLAY
myGame->beginNewGame(myKI, myKI, fieldStruct::playerOne); myGame->beginNewGame(myAI, myAI, fieldStruct::playerOne);
#else #else
myGame->beginNewGame(myKI, myKI, (rand() % 2) ? fieldStruct::playerOne : fieldStruct::playerTwo); myGame->beginNewGame(myAI, myAI, (rand() % 2) ? fieldStruct::playerOne : fieldStruct::playerTwo);
#endif // SELF_PLAY #endif // SELF_PLAY
if (calculateDatabase) { if (calculateDatabase) {
// calculate // calculate
myKI->calculateDatabase(MAX_DEPTH_OF_TREE, false); myAI->calculateDatabase(MAX_DEPTH_OF_TREE, false);
// test database // test database
cout << endl << "Begin test starting from layer: "; startTestFromLayer; cout << endl << "Begin test starting from layer: "; startTestFromLayer;
cout << endl << "End test at layer: "; endTestAtLayer; cout << endl << "End test at layer: "; endTestAtLayer;
myKI->testLayers(startTestFromLayer, endTestAtLayer); myAI->testLayers(startTestFromLayer, endTestAtLayer);
} else { } else {

View File

@ -6,8 +6,8 @@
https://github.com/madweasel/madweasels-cpp https://github.com/madweasel/madweasels-cpp
\*********************************************************************/ \*********************************************************************/
#ifndef MUEHLE_KI_H #ifndef MUEHLE_AI_H
#define MUEHLE_KI_H #define MUEHLE_AI_H
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>

View File

@ -6,8 +6,8 @@
https://github.com/madweasel/madweasels-cpp https://github.com/madweasel/madweasels-cpp
\*********************************************************************/ \*********************************************************************/
#ifndef MINIMAXKI_H #ifndef MINIMAXAI_H
#define MINIMAXKI_H #define MINIMAXAI_H
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>

View File

@ -60,7 +60,7 @@ class miniMaxWinInspectDb
protected: protected:
// General Variables // General Variables
miniMax *pMiniMax = nullptr; // pointer to perfect KI class granting the access to the database miniMax *pMiniMax = nullptr; // pointer to perfect AI class granting the access to the database
miniMaxGuiField *pGuiField = nullptr; miniMaxGuiField *pGuiField = nullptr;
bool showingInspectionControls = false; bool showingInspectionControls = false;
unsigned int curShowedLayer = 0; // current showed layer unsigned int curShowedLayer = 0; // current showed layer
@ -113,7 +113,7 @@ protected:
// Calculation variables // Calculation variables
wildWeasel::masterMind *ww = nullptr; // pointer to engine wildWeasel::masterMind *ww = nullptr; // pointer to engine
miniMax *pMiniMax = nullptr; // pointer to perfect KI class granting the access to the database miniMax *pMiniMax = nullptr; // pointer to perfect AI class granting the access to the database
ostream *outputStream = nullptr; // pointer to a stream for the console output of the calculation done by the class miniMax ostream *outputStream = nullptr; // pointer to a stream for the console output of the calculation done by the class miniMax
stringbuf outputStringBuf; // buffer linked to the stream, for reading out of the stream into the buffer stringbuf outputStringBuf; // buffer linked to the stream, for reading out of the stream into the buffer
locale myLocale; // for formatting the output locale myLocale; // for formatting the output

View File

@ -1,5 +1,5 @@
/********************************************************************* /*********************************************************************
perfectKI.cpp perfectAI.cpp
Copyright (c) Thomas Weber. All rights reserved. Copyright (c) Thomas Weber. All rights reserved.
Copyright (C) 2021 The Sanmill developers (see AUTHORS file) Copyright (C) 2021 The Sanmill developers (see AUTHORS file)
Licensed under the MIT License. Licensed under the MIT License.
@ -183,8 +183,8 @@ unsigned int fieldPosIsOfGroup[] = { GROUP_C, GROUP_D,
GROUP_C, GROUP_D, GROUP_C }; GROUP_C, GROUP_D, GROUP_C };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Name: perfectKI() // Name: perfectAI()
// Desc: perfectKI class constructor // Desc: perfectAI class constructor
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
perfectAI::perfectAI(const char *directory) perfectAI::perfectAI(const char *directory)
{ {
@ -626,8 +626,8 @@ perfectAI::perfectAI(const char *directory)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Name: ~perfectKI() // Name: ~perfectAI()
// Desc: perfectKI class destructor // Desc: perfectAI class destructor
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
perfectAI::~perfectAI() perfectAI::~perfectAI()
{ {
@ -676,10 +676,10 @@ void perfectAI::play(fieldStruct *theField, unsigned int *pushFrom, unsigned int
// current state already calculated? // current state already calculated?
if (isCurrentStateInDatabase(0)) { if (isCurrentStateInDatabase(0)) {
cout << "perfectKI is using database!\n\n\n"; cout << "perfectAI is using database!\n\n\n";
threadVars[0].depthOfFullTree = 3; threadVars[0].depthOfFullTree = 3;
} else { } else {
cout << "perfectKI is thinking thinking with a depth of " << threadVars[0].depthOfFullTree << " steps!\n\n\n"; cout << "perfectAI is thinking thinking with a depth of " << threadVars[0].depthOfFullTree << " steps!\n\n\n";
} }
// start the miniMax-algorithmn // start the miniMax-algorithmn

View File

@ -1,13 +1,13 @@
/*********************************************************************\ /*********************************************************************\
perfectKI.h perfectAI.h
Copyright (c) Thomas Weber. All rights reserved. Copyright (c) Thomas Weber. All rights reserved.
Copyright (C) 2021 The Sanmill developers (see AUTHORS file) Copyright (C) 2021 The Sanmill developers (see AUTHORS file)
Licensed under the MIT License. Licensed under the MIT License.
https://github.com/madweasel/madweasels-cpp https://github.com/madweasel/madweasels-cpp
\*********************************************************************/ \*********************************************************************/
#ifndef PERFEKT_KI_H #ifndef PERFEKT_AI_H
#define PERFEKT_KI_H #define PERFEKT_AI_H
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>

View File

@ -18,8 +18,8 @@ Position::Position()
moveLogFrom = nullptr; moveLogFrom = nullptr;
moveLogTo = nullptr; moveLogTo = nullptr;
playerOneKI = nullptr; playerOneAI = nullptr;
playerTwoKI = nullptr; playerTwoAI = nullptr;
movesDone = 0; movesDone = 0;
field.createField(); field.createField();
@ -52,7 +52,7 @@ void Position::deleteArrays()
// Name: beginNewGame() // Name: beginNewGame()
// Desc: Reinitializes the Position object. // Desc: Reinitializes the Position object.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Position::beginNewGame(millAI *firstPlayerKI, millAI *secondPlayerKI, int currentPlayer) void Position::beginNewGame(millAI *firstPlayerAI, millAI *secondPlayerAI, int currentPlayer)
{ {
// free mem // free mem
deleteArrays(); deleteArrays();
@ -72,8 +72,8 @@ void Position::beginNewGame(millAI *firstPlayerKI, millAI *secondPlayerKI, int c
winner = 0; winner = 0;
movesDone = 0; movesDone = 0;
playerOneKI = firstPlayerKI; playerOneAI = firstPlayerAI;
playerTwoKI = secondPlayerKI; playerTwoAI = secondPlayerAI;
moveLogFrom = new unsigned int[MAX_NUM_MOVES]; moveLogFrom = new unsigned int[MAX_NUM_MOVES];
moveLogTo = new unsigned int[MAX_NUM_MOVES]; moveLogTo = new unsigned int[MAX_NUM_MOVES];
@ -85,9 +85,9 @@ void Position::beginNewGame(millAI *firstPlayerKI, millAI *secondPlayerKI, int c
// Name: startSettingPhase() // Name: startSettingPhase()
// Desc: // Desc:
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool Position::startSettingPhase(millAI *firstPlayerKI, millAI *secondPlayerKI, int currentPlayer, bool settingPhase) bool Position::startSettingPhase(millAI *firstPlayerAI, millAI *secondPlayerAI, int currentPlayer, bool settingPhase)
{ {
beginNewGame(firstPlayerKI, secondPlayerKI, currentPlayer); beginNewGame(firstPlayerAI, secondPlayerAI, currentPlayer);
field.settingPhase = settingPhase; field.settingPhase = settingPhase;
@ -283,8 +283,8 @@ void Position::setNextPlayer()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool Position::isCurrentPlayerHuman() bool Position::isCurrentPlayerHuman()
{ {
if (field.curPlayer->id == field.playerOne) return (playerOneKI == nullptr) ? true : false; if (field.curPlayer->id == field.playerOne) return (playerOneAI == nullptr) ? true : false;
else return (playerTwoKI == nullptr) ? true : false; else return (playerTwoAI == nullptr) ? true : false;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -293,36 +293,36 @@ bool Position::isCurrentPlayerHuman()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool Position::isOpponentPlayerHuman() bool Position::isOpponentPlayerHuman()
{ {
if (field.oppPlayer->id == field.playerOne) return (playerOneKI == nullptr) ? true : false; if (field.oppPlayer->id == field.playerOne) return (playerOneAI == nullptr) ? true : false;
else return (playerTwoKI == nullptr) ? true : false; else return (playerTwoAI == nullptr) ? true : false;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Name: setKI() // Name: setAI()
// Desc: Assigns an AI to a player. // Desc: Assigns an AI to a player.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Position::setKI(int player, millAI *KI) void Position::setAI(int player, millAI *AI)
{ {
if (player == field.playerOne) { if (player == field.playerOne) {
playerOneKI = KI; playerOneAI = AI;
} }
if (player == field.playerTwo) { if (player == field.playerTwo) {
playerTwoKI = KI; playerTwoAI = AI;
} }
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Name: getChoiceOfSpecialKI() // Name: getChoiceOfSpecialAI()
// Desc: Returns the move the passed AI would do. // Desc: Returns the move the passed AI would do.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Position::getChoiceOfSpecialKI(millAI *KI, unsigned int *pushFrom, unsigned int *pushTo) void Position::getChoiceOfSpecialAI(millAI *AI, unsigned int *pushFrom, unsigned int *pushTo)
{ {
fieldStruct theField; fieldStruct theField;
*pushFrom = field.size; *pushFrom = field.size;
*pushTo = field.size; *pushTo = field.size;
theField.createField(); theField.createField();
field.copyField(&theField); field.copyField(&theField);
if (KI != nullptr && (field.settingPhase || field.curPlayer->numPossibleMoves > 0) && winner == 0) KI->play(&theField, pushFrom, pushTo); if (AI != nullptr && (field.settingPhase || field.curPlayer->numPossibleMoves > 0) && winner == 0) AI->play(&theField, pushFrom, pushTo);
theField.deleteField(); theField.deleteField();
} }
@ -340,9 +340,9 @@ void Position::getComputersChoice(unsigned int *pushFrom, unsigned int *pushTo)
if ((field.settingPhase || field.curPlayer->numPossibleMoves > 0) && winner == 0) { if ((field.settingPhase || field.curPlayer->numPossibleMoves > 0) && winner == 0) {
if (field.curPlayer->id == field.playerOne) { if (field.curPlayer->id == field.playerOne) {
if (playerOneKI != nullptr) playerOneKI->play(&theField, pushFrom, pushTo); if (playerOneAI != nullptr) playerOneAI->play(&theField, pushFrom, pushTo);
} else { } else {
if (playerTwoKI != nullptr) playerTwoKI->play(&theField, pushFrom, pushTo); if (playerTwoAI != nullptr) playerTwoAI->play(&theField, pushFrom, pushTo);
} }
} }

View File

@ -36,8 +36,8 @@ class Position
private: private:
// Variables // Variables
unsigned int *moveLogFrom, *moveLogTo, movesDone; // array containing the history of moves done unsigned int *moveLogFrom, *moveLogTo, movesDone; // array containing the history of moves done
millAI *playerOneKI; // class-pointer to the AI of player one millAI *playerOneAI; // class-pointer to the AI of player one
millAI *playerTwoKI; // class-pointer to the AI of player two millAI *playerTwoAI; // class-pointer to the AI of player two
fieldStruct field; // current field fieldStruct field; // current field
fieldStruct initialField; // undo of the last move is done by setting the initial field und performing all moves saved in history fieldStruct initialField; // undo of the last move is done by setting the initial field und performing all moves saved in history
int winner; // playerId of the player who has won the game. zero if game is still running. int winner; // playerId of the player who has won the game. zero if game is still running.
@ -58,18 +58,18 @@ public:
// Functions // Functions
void undo_move(); void undo_move();
void beginNewGame(millAI *firstPlayerKI, millAI *secondPlayerKI, int currentPlayer); void beginNewGame(millAI *firstPlayerAI, millAI *secondPlayerAI, int currentPlayer);
void setKI(int player, millAI *KI); void setAI(int player, millAI *AI);
bool do_move(unsigned int pushFrom, unsigned int pushTo); bool do_move(unsigned int pushFrom, unsigned int pushTo);
void getComputersChoice(unsigned int *pushFrom, unsigned int *pushTo); void getComputersChoice(unsigned int *pushFrom, unsigned int *pushTo);
bool setCurrentGameState(fieldStruct *curState); bool setCurrentGameState(fieldStruct *curState);
bool compareWithField(fieldStruct *compareField); bool compareWithField(fieldStruct *compareField);
bool comparePlayers(playerStruct *playerA, playerStruct *playerB); bool comparePlayers(playerStruct *playerA, playerStruct *playerB);
void printField(); void printField();
bool startSettingPhase(millAI *firstPlayerKI, millAI *secondPlayerKI, int currentPlayer, bool settingPhase); bool startSettingPhase(millAI *firstPlayerAI, millAI *secondPlayerAI, int currentPlayer, bool settingPhase);
bool putStone(unsigned int pos, int player); bool putStone(unsigned int pos, int player);
bool settingPhaseHasFinished(); bool settingPhaseHasFinished();
void getChoiceOfSpecialKI(millAI *KI, unsigned int *pushFrom, unsigned int *pushTo); void getChoiceOfSpecialAI(millAI *AI, unsigned int *pushFrom, unsigned int *pushTo);
void setUpCalcPossibleMoves(playerStruct *player); void setUpCalcPossibleMoves(playerStruct *player);
void setUpSetWarningAndMill(unsigned int stone, unsigned int firstNeighbour, unsigned int secondNeighbour); void setUpSetWarningAndMill(unsigned int stone, unsigned int firstNeighbour, unsigned int secondNeighbour);
void calcNumberOfRestingStones(int &numWhiteStonesResting, int &numBlackStonesResting); void calcNumberOfRestingStones(int &numWhiteStonesResting, int &numBlackStonesResting);

View File

@ -6,8 +6,8 @@
https://github.com/madweasel/madweasels-cpp https://github.com/madweasel/madweasels-cpp
\*********************************************************************/ \*********************************************************************/
#ifndef RANDOM_KI_H #ifndef RANDOM_AI_H
#define RANDOM_KI_H #define RANDOM_AI_H
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>