Chinaunix

标题: 案例:C语言编写的控制台版本的五子棋游戏【代码】 [打印本页]

作者: bsechr    时间: 2011-04-21 13:08
标题: 案例:C语言编写的控制台版本的五子棋游戏【代码】
需求定义:
     编写程序实现两人互玩的五子棋游戏。游戏开始时要求在控制台输出以下棋盘,然后提示黑方和白方下子,玩家从命令行输入落子坐标,如:1-2,表示在第二行第三列落子,其中黑方的子用@表示,白方的子用O(大写字母O)表示,空白位置用*表示,每有一方落子,则要重新输出棋盘的状态,程序还要能判断某一方获胜,并终止游戏。
棋盘初始状态如下:

代码实现:

#include <stdio.h>

#define N 14

#include <stdbool.h>

char state[N][N];

void init(void);

void printState(void);

bool isWin(bool isBlack,int x,int y);

bool isLevelWin(bool isBlack,int x,int y);

bool isVerticalWin(bool isBlack,int x,int y);

bool isLeftInclinedWin(bool isBlack,int x,int y);

bool isRightObliqueWin(bool isBlack,int x,int y);


bool isWin(bool isBlack,int x,int y)

{

         return isLevelWin(isBlack,x,y)

                   ||isVerticalWin(isBlack,x,y)

                   ||isLeftInclinedWin(isBlack,x,y)

                   ||isRightObliqueWin(isBlack,x,y);

}



bool isLevelWin(bool isBlack,int x,int y)

{

         char c = isBlack ? '@':'O';

         int count;

         while(y>0 && state[x][y] == c)

         {

                   y--;

         }

         count =0;

         if(state[x][y] == c) count = 1;

         y++;

         while(y < N && state[x][y] == c)

         {

                   count++;

                   if(count == 5)

                   {

                            return true;

                   }

                   y++;

         }

         return false;

}

bool isVerticalWin(bool isBlack,int x,int y)

{
         char c = isBlack ? '@':'O';

         int count;

         while(x>0 && state[x][y] == c)

         {

                   x--;

         }

         count =0;

         if(state[x][y] == c) count = 1;

         x++;

         while(x < N && state[x][y] == c)

         {

                   count++;

                   if(count == 5)

                   {

                            return true;

                   }

                   x++;
         }

         return false;

}


bool isLeftInclinedWin(bool isBlack,int x,int y)

{

         char c = isBlack ? '@':'O';

         int count;

         while(x>0 && y>0 && state[x][y] == c)

         {

                   y--;

                   x--;

         }

         

         count =0;

         if(state[x][y] == c) count = 1;

         x++;

         y++;

         while(x < N && y < N && state[x][y] == c)

         {

                   count++;

                   if(count == 5)

                   {

                            return true;

                   }

                   x++;

                   y++;

         }

         return false;

}



bool isRightObliqueWin(bool isBlack,int x,int y)

{

         char c = isBlack ? '@':'O';

         int count;

         while(x>0 && y<N && state[x][y] == c)

         {

                   y++;

                   x--;

         }

         

         count =0;

         if(state[x][y] == c) count = 1;

         x++;

         y--;

         while(x < N && y >= 0 && state[x][y] == c)

         {

                   count++;

                   if(count == 5)

                   {

                            return true;

                   }

                  x++;

                   y--;

         }

         return false;

}

void init(void)

{

         int i,j;

         for(i=0;i<N;i++)

         {

                   for(j=0;j<N;j++)

                   {

                            state[j] = '*';

                   }

         }

}



void printState(void)

{

         int i,j;

         printf("%3c",' ');

         for(i=0;i<N;i++)

                   printf("%3d",i);

         printf("\n";

         printf("-----------------------------------------------------\n";

         for(i=0;i<N;i++)

         {

                   printf("%3d",i);

                   for(j=0;j<N;j++)

                   {

                            printf("%3c",state[j]);

                   }

                   printf("\n";

         }

}

int main(void)

{

         int x,y;

         bool isBlack = true;

         init();        

         printf("------------------\n";

         printState();

         while(1)

         {

                   printf("please %s quick snip\n",(isBlack?"black":"white");

                   printf("example1-2)\n";

                   scanf("%d-%d",&x,&y);

                   if(state[x][y]=='@' || state[x][y]=='O')

                   {

                            printf("this position to have pieces\n";

                            continue;

                   }        

                   state[x][y] = (isBlack?'@':'O');

                   printState();

                   if(isWin(isBlack,x,y))

                   {

                            printf("%s win\n",(isBlack?"black":"white");

                            break;

                   }

                   isBlack = !isBlack;

         }        

}

C语言、嵌入式技术专栏,欢迎交流
作者: thinhare    时间: 2011-04-23 04:50
还以为是人机玩,那就有难度了。
作者: fender0107401    时间: 2011-04-23 09:12
要是人机对战的话,需要写一个AI吧。
作者: cjaizss    时间: 2011-04-23 19:26
即使是打广告,我想你还是专业一点比较好。
用code把代码包起来。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2