Chciałem sobie napisać grę w kółko i krzyżyk. Ściągnęłem sobie skądś pracę magisterską o szachach i implementuje algorytmy minimaksowe na tej grze. Jednak nie do końca mnie to wychodzi:
[code]
#include "person.h"
#include "board.h"
#include "game.h"
int humanperson::getMove (board b)
{
cout << b;
cout << "Ruch człowieka: " << endl;
int nr = 0;
cout << "Podaj nr ruchu: ";
cin >> nr;
if (nr<1 || nr>9) throw new except(FILE,FUNCTION, LINE, "Nieprawidłowy zakres ruchu");
return nr;
}
int machineperson::getMove (board b)
{
int bestmove = 0;
int bestvalue = -1000000;
thinking_time = time(0);
max_thinking_time = 10000;
try
{
for (int i = 1; i<=9; i++)
{
if (b[i] == NO_FIGURE)
{
// analizuje tylko puste pola
int price = getValueMove(i, 1, b);
if (price>bestvalue)
{
bestmove = i;
bestvalue = price;
}
}
}
if (bestmove < 1 || bestmove > 9)
{
throw new except(__FILE__,__FUNCTION__, __LINE__, "Nie znalazlem kandydata na ruch");
}
}
catch (except *e)
{
cout << *e << endl;
delete e;
}
catch (...)
{
except e(__FILE__, __FUNCTION__, __LINE__, "Blad systemowy");
cout << e << endl;
}
return bestmove;
}
std::istream& operator>> (std::istream& c, person& p)
{
std::cout << "Podaj nazwe gracza: ";
std::cin >> p.name;
return c;
}
figurestype humanperson::getType () { return CIRCLE; }
figurestype machineperson::getType () { return HASH; }
int machineperson::getValueMove (int cont, int nr, board bo)
{
const int WON = 100000;
const int LOSE = -WON;
const int DRAW = 0;
int bestmove = 0;
int bestprice = 0;
board t(bo);
t[cont] = getType();
switch (t.check())
{
case GAME_WON:
if (nr%2 == 1) return WON; else return LOSE;
break;
case GAME_END:
return DRAW;
break;
case GAME_CONT:
for (int i = 1; i<=9; i++)
if (t[i] == NO_FIGURE)
{
int price = getValueMove (i, nr+1, t);
if (bestmove == 0)
{
bestmove = i;
bestprice = price;
} else
{
if (nr%2 == 1) // computer
{
if (price>bestprice)
{
bestprice = price;
bestmove = i;
}
}
else
{ // human
if (price<bestprice)
{
bestprice = price;
bestmove = i;
}
}
}
}
break;
default: throw new except(__FILE__,__FUNCTION__, __LINE__, "Nieoczekiwany stan");
};
return bestprice;
}
[/code]
Objawia się niepoprawność przez to, że czasami nie wykrywa przegranej, nie widzi możliwości wygranej itd.
Co robie nie tak w funkcj getValueMove?