/*
* 文件名:#.c
* 用途:井字棋程序
* 编程环境:WinXP SP2+CL 8.0
* 完成日期: 2006.8 Ver 0.01
* 作者: 88250
* 联系方式: E-mail: DL88250@gmail.com QQ:845765
*/
#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: ");
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]. ", row, col);
dsply();
break;
}
else
printf("Error : You put the chess to a wrong location ");
}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 [1][1]. ");
dsply();
return;
}
else{
chsman[1][1] = P2;
printf("The computer located chessman at [2][2]. ");
dsply();
return;
}
}
/*note the number of the step.*/
stepFlg++;
if(stepFlg == 2){
if((chsman[0][0] == P1 && chsman[2][2] == P1) || (chsman[0][2] == P1 && chsman[2][0] == P1)){
chsman[0][1] = P2;
printf("The computer located chessman at [1][2]. ");
dsply();
return;
}
}
/*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(" ");
/*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(" ");
/*print the floor.*/
for(i = 0; i < SIZE * 4 + 1; i++)
printf("-");
printf(" ");
}
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;
}
发表于 @ 2007年05月05日 14:53:00|评论(loading...)|编辑