以前写的一个程序,#字棋。。。。
#include <stdio.h>
#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(rskAndAtkLevlRow[i] == -2){
chkAndPutDwnRow(i, col)
}
}
/*attck a column.*/
for(i = 0; i< SIZE; i++){
if(rskAndAtkLevlCol[i] == -2){
chkAndPutDwnCol(i, col)
}
}
/*attack slash(\).*/
if(rskAndAtkLevlSlsh[0] == -2){
for(row = 0, col = 0; row < SIZE; row++, col++){
chkAndPutDwn_Slsh(row, col)
}
}
/*attack slash(/).*/
if(rskAndAtkLevlSlsh[1] == -2){
for(row = 0, col = 2; row < SIZE; row++, col--){
chkAndPutDwn_Slsh(row, col)
}
}
/*locate the risk grid and put down one chessman to resolve it.*/
/*resolve the risk of a Row.*/
for(i = 0; i < SIZE; i++){
if(rskAndAtkLevlRow[i] == 2){
chkAndPutDwnRow(i, col)
}
}
/*resolve the risk of a column.*/
for(i = 0; i< SIZE; i++){
if(rskAndAtkLevlCol[i] == 2){
chkAndPutDwnCol(i, col)
}
}
/*resolve the risk of a slash(\).*/
if(rskAndAtkLevlSlsh[0] == 2){
for(row = 0, col = 0; row < SIZE; row++, col++){
chkAndPutDwn_Slsh(row, col)
}
}
/*resolve the risk of a slash(/).*/
if(rskAndAtkLevlSlsh[1] == 2){
for(row = 0, col = 2; row < SIZE; row++, col--){
chkAndPutDwn_Slsh(row, col)
}
}
/*if there is no risk exist, put down the chessman in a blank(is not the best blank, may be).*/
for(row = 0; row < SIZE; row++){
for(col = 0; col < SIZE; col++){
if(chsman[row][col] == 0 && ((row == 0 && col == 0) || (row == 0 && col == 2) ||
(row == 2 && col == 0) || (row == 2 && col == 2))){
chsman[row][col] = P2;
dsply();
return;
}
}
}
}
/*display the current chessman board.*/
void dsply(void)
{
int row, col, i;
/*print the head.*/
for(i = 0; i < SIZE * 4 + 1; i++)
printf("-");
printf("\n");
/*print the contect.*/
for(row = 0; row < SIZE; row++){
printf("|");
for(col = 0; col < SIZE; col++){
if(chsman[row][col] == P1) printf(" o |");
else if(chsman[row][col] == P2) printf(" x |");
else printf(" |");
}
printf("\n");
/*print the floor.*/
for(i = 0; i < SIZE * 4 + 1; i++)
printf("-");
printf("\n");
}
return;
}
/*check whether win this game.*/
int chkWin(void)
{
int i;
for(i = 0; i < SIZE; i++){
if(chsman[i][0] + chsman[i][1] + chsman[i][2] == -3 || chsman[0][i] + chsman[1][i] + chsman[2][i] == -3 ||
chsman[0][0] + chsman[1][1] + chsman[2][2] == -3 || chsman[0][2] + chsman[1][1] + chsman[2][0] == -3){
return WIN;
}
}
return UNWIN;
}
/*check whether peace with user.*/
int chkPeace(void)
{
int row, col;
int sum = 0;
for(row = 0; row < SIZE; row++){
for(col = 0; col < SIZE; col++){
if(sum += chsman[row][col] == PEACE){
return PEACE;
}
}
}
return 0;
}
int main(char* args[])
{
/*display the chess board.*/
dsply();
do{
/*user's turn of input.*/
input();
/*computer says: it is my turn of input.*/
judge();
if(chkWin() == WIN) break;
if(stepFlg == 5 && chkPeace() == PEACE){
printf("Peace!");
return 0;
}
}while(1);
printf("Hehe....I win this game~ :-p");
return 0;
}








