資源簡介
丼字棋,里面有人機和人人版,是計算機博弈入門最好的例子,我傾情大奉獻。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#define?x?‘x‘
#define?o?‘o‘
#define?empty?‘\0‘
#define?INFINITY?100
#define?INPROGRESS?1
#define?DRAW?0
#define?WIN??(-INFINITY)
#define?LOSE?(+INFINITY)
#define?DOUBLE_CONNECTED?50
int?maxSearch(?char?_board[9]?);
void?PrintBoard(char?_board[9]);
int?gameState(char?_board[9])
{
int?statei;
static?int?table[][3]?=?
{
{0?1?2}
{3?4?5}
{6?7?8}
{0?3?6}
{1?4?7}
{2?5?8}
{0?4?8}
{2?4?6}
};
char?chess?=?_board[0];
for?(?i?=?1;?i?9;?++i)
{
chess?&=?_board[i];
}
bool?isFull?=?0?!=?chess;
bool?isFind?=?false;
for?(?i?=?0;?i? {
chess?=?_board[table[i][0]];
int?j;
for?(j?=?1;?j?3;?++j)
if?(_board[table[i][j]]?!=?chess)
break;
if?(chess?!=?empty?&&?j?==?3)
{
isFind?=?true;
break;
}
}
if?(isFind)
//got?win?or?lose
state?=?chess?==?o???WIN?:?LOSE;
else
{
if?(isFull)
//all?position?has?been?set?without?win?or?lose
return?DRAW;
else
{
//finds[0]?->?‘o‘?finds[1]?->?‘x‘
int?finds[2]?=?{0?};
for?(?i?=?0;?i? {
bool?findEmpty?=?false;
chess?=?0xff;
int?j;
for?(j?=?0;?j?3;?++j)
if?(_board[table[i][j]]?==?empty?&&?!findEmpty)
findEmpty?=?true;
else
chess?&=?_board[table[i][j]];
if?((chess?==?o?||?chess?==?x)?&&?findEmpty)
{
isFind?=?true;
if?(o?==?chess)
++finds[0];
else
++finds[1];
}
}
if?(finds[0]?>?1?&&?finds[1]?1)
//2?‘o‘?has?been?founded?twice?in?row?column?or?diagonal?direction
state?=?-DOUBLE_CONNECTED;
else?if?(finds[1]?>?1?&&?finds[0]?1)
//2?‘x‘?has?been?founded?twice?in?row?column?or?diagonal?direction
state?=?DOUBLE_CONNECTED;
else
//need?to?search?more.
state?=?INPROGRESS;
}
}
return?state;
}
int?minSearch(?char?_board[9]?)
{
short?int?i;
int?positionValue?=?gameState(_board);
if(?positionValue?==?DRAW?)?return?0;
if(?positionValue?!=?INPROGRESS?)?return?positionValue;
int?bestValue?=?+INFINITY;
for(?i?=?0;?i?9;?i++?)
{
if(?_board[i]?==?empty?)
{
_board[i]?=?o;
int?value?=?maxSearch(?_board?);
if(?value? bestValue?=?value;
_board[i]?=?empty;
}
}
return?bestValue;
}
int?maxSearch(?char?_board[9]?)
{
short?int?i;
int?positionValue?=?gameState(_board);
if(?positionValue?==?DRAW?)?return?0;
if(?positionValue?!=?INPROGRESS?)?return?positionValue;
int?bestValue?=?-INFINITY;
for(?i?=?0;?i?9;?i++?)
{
if(?_board[i]?==?empty?)
{
_board[i]?=?x;
int?value?=?minSearch(?_board?);
if(?value?>?bestValue?)
bestValue?=?value;
_board[i]?=?empty;
}
}
return?bestValue;
}
int?minimax(?char?_board[9]?)
{
short?int?i;
int?bestValue?=?+INFINITY?index?=?0;
char?bestMoves[9]?=?{0};
for(?i?=?0;?i?9;?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-10-23?14:54??井字棋\
?????目錄???????????0??2014-04-17?18:53??井字棋\Tic-Tac-Toe\
?????文件???????38912??2013-10-23?14:54??井字棋\Tic-Tac-Toe.doc
?????目錄???????????0??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\
?????文件?????????624??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\cl.command.1.tlog
?????文件????????2508??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\CL.read.1.tlog
?????文件?????????240??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\CL.write.1.tlog
?????文件???????????2??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\li
?????文件???????????2??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\li
?????文件???????????2??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\li
?????文件???????????2??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\li
?????文件????????1294??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\li
?????文件????????2900??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\li
?????文件?????????576??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\li
?????文件???????25600??2013-06-24?22:37??井字棋\Tic-Tac-Toe\Debug\main.bsc
?????文件??????430592??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.exe
?????文件?????????406??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.exe.em
?????文件?????????472??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.exe.em
?????文件?????????381??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.exe.intermediate.manifest
?????文件??????953088??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.ilk
?????文件??????????68??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.lastbuildstate
?????文件????????1775??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.log
?????文件???????14284??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.obj
?????文件?????1707008??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main.pdb
?????文件???????????0??2013-04-14?12:49??井字棋\Tic-Tac-Toe\Debug\main.sbr
?????文件?????????204??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\main_manifest.rc
?????文件?????????342??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\mt.command.1.tlog
?????文件?????????346??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\mt.read.1.tlog
?????文件?????????242??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\mt.write.1.tlog
?????文件?????????446??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\rc.command.1.tlog
?????文件?????????318??2014-04-17?18:53??井字棋\Tic-Tac-Toe\Debug\rc.read.1.tlog
............此處省略74個文件信息
- 上一篇:VC仿Xp計算器
- 下一篇:C++STL幫助文檔
評論
共有 條評論