井字棋人机和人人版C语言程序


丼字棋,里面有人机和人人版,是计算机博弈入门最好的例子,我倾情大奉献。
资源截图
代码片段和文件信息
#include 
#include 
#include 
#include 

#define x ‘x‘
#define o ‘o‘
#define empty ‘‘

#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 < sizeof(table) / sizeof(int[3]); ++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 < sizeof(table) / sizeof(int[3]); ++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 )
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-ToeDebug
     文件         624  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugcl.command.1.tlog
     文件        2508  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugCL.read.1.tlog
     文件         240  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugCL.write.1.tlog
     文件           2  2014-04-17 18:53  井字棋Tic-Tac-ToeDebuglink-cvtres.read.1.tlog
     文件           2  2014-04-17 18:53  井字棋Tic-Tac-ToeDebuglink-cvtres.write.1.tlog
     文件           2  2014-04-17 18:53  井字棋Tic-Tac-ToeDebuglink.4896.read.1.tlog
     文件           2  2014-04-17 18:53  井字棋Tic-Tac-ToeDebuglink.4896.write.1.tlog
     文件        1294  2014-04-17 18:53  井字棋Tic-Tac-ToeDebuglink.command.1.tlog
     文件        2900  2014-04-17 18:53  井字棋Tic-Tac-ToeDebuglink.read.1.tlog
     文件         576  2014-04-17 18:53  井字棋Tic-Tac-ToeDebuglink.write.1.tlog
     文件       25600  2013-06-24 22:37  井字棋Tic-Tac-ToeDebugmain.bsc
     文件      430592  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.exe
     文件         406  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.exe.embed.manifest
     文件         472  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.exe.embed.manifest.res
     文件         381  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.exe.intermediate.manifest
     文件      953088  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.ilk
     文件          68  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.lastbuildstate
     文件        1775  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.log
     文件       14284  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.obj
     文件     1707008  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain.pdb
     文件           0  2013-04-14 12:49  井字棋Tic-Tac-ToeDebugmain.sbr
     文件         204  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmain_manifest.rc
     文件         342  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmt.command.1.tlog
     文件         346  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmt.read.1.tlog
     文件         242  2014-04-17 18:53  井字棋Tic-Tac-ToeDebugmt.write.1.tlog
     文件         446  2014-04-17 18:53  井字棋Tic-Tac-ToeDebug
c.command.1.tlog
     文件         318  2014-04-17 18:53  井字棋Tic-Tac-ToeDebug
c.read.1.tlog
............此处省略74个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)