主题:[原创]案例:控制台版本的五子棋游戏【代码】
[size=4]需求定义:
编写程序实现两人互玩的五子棋游戏。游戏开始时要求在控制台输出以下棋盘,然后提示黑方和白方下子,玩家从命令行输入落子坐标,如:1-2,表示在第二行第三列落子,其中黑方的子用@表示,白方的子用O(大写字母O)表示,空白位置用*表示,每有一方落子,则要重新输出棋盘的状态,程序还要能判断某一方获胜,并终止游戏。
棋盘初始状态如下:
[img]http://www.bsechr.com.cn/UploadFile/201147930475394.jpg[/img]
代码实现:
#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[i][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[i][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("example:(1-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;
}
}
[url=http://www.bsechr.com.cn/news.asp?anclassid=58&mnclassid=165]嵌入式、C语言基础知识专栏[/url],欢迎交流![/size]
编写程序实现两人互玩的五子棋游戏。游戏开始时要求在控制台输出以下棋盘,然后提示黑方和白方下子,玩家从命令行输入落子坐标,如:1-2,表示在第二行第三列落子,其中黑方的子用@表示,白方的子用O(大写字母O)表示,空白位置用*表示,每有一方落子,则要重新输出棋盘的状态,程序还要能判断某一方获胜,并终止游戏。
棋盘初始状态如下:
[img]http://www.bsechr.com.cn/UploadFile/201147930475394.jpg[/img]
代码实现:
#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[i][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[i][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("example:(1-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;
}
}
[url=http://www.bsechr.com.cn/news.asp?anclassid=58&mnclassid=165]嵌入式、C语言基础知识专栏[/url],欢迎交流![/size]