资源简介
井字棋又称一字棋、三字棋.此资源非原创.
代码片段和文件信息
#include
#define P1 1
#define P2 -1
#define SIZE 3
#define WIN -1
#define UNWIN 0
#define PEACE 1
#define chkAndPutDwnRow(row col){\
for(col = 0; col < SIZE; col++){\
if(chsman[row][col] == 0){\
chsman[row][col] = P2;\
dsply();\
return;\
}\
}\
}
#define chkAndPutDwnCol(row col){\
for(col = 0; col < SIZE; col++){\
if(chsman[col][row] == 0){\
chsman[col][row] = P2;\
dsply();\
return;\
}\
}\
}
#define chkAndPutDwn_Slsh(row col){\
if(chsman[row][col] == 0){\
chsman[row][col] = P2;\
dsply();\
return;\
}\
}
int chsman[SIZE][SIZE] = {0};
/*function prototype (declaration).*/
int enterChsman(int int);
void dsply(void);
void input(void);
void judge(void);
int chkWin(void);
int chkPeace(void);
/*create a global variable named stepFlg use to note the step.*/
int stepFlg = 0;
int enterChsman(int row int col)
{
/*out of size.*/
if(row >= SIZE || col >= SIZE)
return 0;
/*the pionted location is not empty.*/
if(chsman[row][col] != 0)
return 0;
/*okay put down the chessman.*/
chsman[row][col] = P1;
return 1;
}
/*user input.*/
void input(void)
{
int row col;
do{
printf(“Please locate your chessman:\n“);
printf(“Location row: “);
scanf(“%d“ &row);
printf(“Location column: “);
scanf(“%d“ &col);
if(enterChsman(row - 1 col - 1) == 1){
printf(“You located chessman at [%d][%d].\n“ row col);
dsply();
break;
}
else
printf(“Error : You put the chess to a wrong location\n“);
}while(1);
return;
}
/*computer judge and input.*/
void judge(void)
{
int row col;
int i;
/*the risk level status of the chessboard.*/
/*the attack level status of the chessboard.*/
int rskAndAtkLevlRow[SIZE] = {0} rskAndAtkLevlCol[SIZE] = {0} rskAndAtkLevlSlsh[2] = {0};
/*obviate the special satus of the first chessman */
if(stepFlg == 0){
/*now flag the first chessman had down.*/
stepFlg = 1;
if(chsman[1][1] == P1){
chsman[0][0] = P2;
printf(“The computer located chessman at [0][0].\n“);
dsply();
return;
}
else{
chsman[1][1] = P2;
printf(“The computer located chessman at [1][1].\n“);
dsply();
return;
}
}
/*note the number of the step.*/
stepFlg++;
/*evaluate the risk level and attack level of every row column and slash.*/
for(row = 0; row < SIZE; row++){
for(col = 0; col < SIZE; col++){
rskAndAtkLevlRow[row] += chsman[row][col];
}
}
for(col = 0; col < SIZE; col++){
for(row = 0; row < SIZE; row++){
rskAndAtkLevlCol[col] += chsman[row][col];
}
}
rskAndAtkLevlSlsh[0] = chsman[0][0] + chsman[1][1] + chsman[2][2];
rskAndAtkLevlSlsh[1] = chsman[0][2] + chsman[1][1] + chsman[2][0];
/*attck!*/
/*attck a row.*/
for(i = 0; i < SIZE; i++){
if(rskAndAtkLe
评论
共有 条评论