简约设计の艺术

讨论软件制造过程中的艺术与工程,软件哲学

原创 “井”字棋收藏

以前写的一个程序,#字棋。。。。

#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;
}
 

发表于 @ 2006年12月26日 00:05:00|评论(loading...)

新一篇: 关于选课 | 旧一篇: C++ 书单

用户操作
[即时聊天] [发私信] [加为好友]
丁亮
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
丁亮的公告




My Gmail & MSN & QQ:

文章分类
收藏
    Code snips
    C++代码示例
    HTML代码示例
    Java Code examples
    CSDN experts
    88250 @ Google(RSS)
    孟岩的专栏
    袁萌的专栏
    E-books
    CSDN下载频道
    e 书时空
    IT e Book
    中华电脑书库
    中国 E 书网
    中国 IT 认证实验室
    中文电子书网
    偶要雷锋 - 分享社区
    我爱 e 书
    网络中国 - E 书
    Linux/Ubuntu
    ChinaUnix
    Compiz Themes
    Compiz-Fusion
    deviantART Search
    GetDeb
    Gnome-Look
    KDE-Look
    LinuxToy
    Linux桌面中文网
    Ubuntu中文官方论坛
    Ubuntu桌面中文网
    My friends
    Eleven@Hand
    Eleven的专栏
    Eric.Gao的空间
    Meteor的专栏
    mmchsusan的主页
    solonote的专栏
    Vanessa的小窝
    ZhiBaoDeng的专栏
    zyofprogrammer的学习历程
    光光的Blog~
    师傅dorainm的Blog
    皮皮的空间
    秋歌的专栏
    金秋风采
    阿明的专栏
    My projects
    BeyondTrack @ Java.net
    BeyondTrack @ Jinfonet
    LivaPlayer
    YOYOPlayer
    Super stars :-)
    Alan Turing
    Bjarne Stroustrup's Homepage
    Don Knuth's Home Page
    Martin Fowler
    Richard Stallman's Home Page
    Uncle Bob (Robert C. Martin)
    Technologies
    Apache Software
    CSDN
    Eclipse.org
    Extreme Programming
    Facelets DevDoc
    hibernate.org
    IBM软件技术
    InfoQ
    JavaEye
    JavaFX Home
    JavaFX Script Reference
    java-source
    JavaWorld@TW
    Java开源大全
    JBoss Seam
    JBoss.org
    LEX & YACC Page
    NetBeans Dream Team
    NetBeans 中文社区
    Open Source Initiative
    OpenEBS
    PHP 官方
    Ruby on Rails
    Ruby中文社区论坛
    SOURCEFORGE.NET
    Springframework.org
    Struts Framework
    Sun中国技术社区
    Testng.org
    UML官方
    水族馆(Aquarium 中文版)
    存档
    Csdn Blog version 3.1a
    Copyright © 丁亮