1.15、C语言设计简易五子棋
程序员文章站
2024-03-16 22:30:28
...
实现一个20*20的简易五子棋,双方使用键盘输入落子的坐标,刷新输出当前棋局状态,并且判断有无五子连心,裁决输赢。
/*****************************************************************
* Copyright (C) 2018 Sangfor Ltd. All rights reserved.
*
* 文件名称:wuziqi.c
* 创 建 者:yinfei-hu
* 创建日期:2018年08月07日 星期2 14时39分14秒
* 功能描述:简易五子棋
*****************************************************************/
#include <stdio.h>
#define N 20
#define M 20
int main()
{
int x,y,e = 0;
char a[N][M];
for(int i = 0;i < N;i++)
for(int j = 0;j < M;j++)
a[i][j] = ' ';
for(int i = 0;i <= N;i++) printf("%2d",i);
printf("\n");
for(int i = 0;i < N;i++){
printf("%2d",i+1);
for(int j = 0;j < M;j++)
printf("%2c",a[i][j]);
printf("\n");
}
while(e < 400){
if(e % 2 == 0){
printf("请选手A输入红棋棋子坐标(1-20)X,Y:\n");
scanf("%d%d",&x,&y);
while(x > 20 || y > 20){
printf("棋子坐标超出范围,请选手A重新输入红棋棋子坐标(1-20)X,Y:\n");
scanf("%d%d",&x,&y);
}
while(a[x-1][y-1] != ' '){
printf("该位置已有棋子,请选手A重新输入红棋棋子坐标(1-20)X,Y:\n");
scanf("%d%d",&x,&y);
}
a[x-1][y-1] = 'O';
}
else {
printf("请选手B输入黑棋棋子坐标(1-20)X,Y:\n");
scanf("%d%d",&x,&y);
while(x > 20 || y > 20){
printf("棋子坐标超出范围,请选手B重新输入黑棋棋子坐标(1-20)X,Y:\n");
scanf("%d%d",&x,&y);
}
while(a[x-1][y-1] != ' '){
printf("该位置已有棋子,请选手B重新输入黑棋棋子坐标(1-20)X,Y:\n");
scanf("%d%d",&x,&y);
}
a[x-1][y-1] = 'X';
}
for(int i = 0;i <= N;i++) printf("%2d",i);
printf("\n");
for(int i = 0;i < N;i++){
printf("%2d",i+1);
for(int j = 0;j < M;j++)
printf("%2c",a[i][j]);
printf("\n");
}
e++;
int q = 0;
if(e >= 9){
for(int i = 0;i < N;i++){
for(int j = 0;j < M;j++){
if(a[i][j] == 'O'&&q == 0){
if(a[i][j+1] == 'O'&&a[i][j+2] == 'O'&&a[i][j+3] == 'O'&&a[i][j+4] == 'O')
q = 1;
else if(a[i+1][j] == 'O'&&a[i+2][j] == 'O'&&a[i+3][j] == 'O'&&a[i+4][j] == 'O')
q = 1;
else if(a[i+1][j+1] == 'O'&&a[i+2][j+2] == 'O'&&a[i+3][j+3] == 'O'&&a[i+4][j+4] == 'O')
q = 1;
else if(a[i+1][j-1] == 'O'&&a[i+2][j-2] == 'O'&&a[i+3][j-3] == 'O'&&a[i+4][j-4] == 'O')
q = 1;
}
}
}
}
if(q == 1){
printf("选手A红棋赢了!!Game over!!!\n");
break;
}
int w = 0;
if(e >= 10){
for(int i = 0;i < N;i++){
for(int j = 0;j < M;j++){
if(a[i][j] == 'X'&&q == 0){
if(a[i][j+1] == 'X'&&a[i][j+2] == 'X'&&a[i][j+3] == 'X'&&a[i][j+4] == 'X')
w = 1;
else if(a[i+1][j] == 'X'&&a[i+2][j] == 'X'&&a[i+3][j] == 'X'&&a[i+4][j] == 'X')
w = 1;
else if(a[i+1][j+1] == 'X'&&a[i+2][j+2] == 'X'&&a[i+3][j+3] == 'X'&&a[i+4][j+4] == 'X')
w = 1;
else if(a[i+1][j-1] == 'X'&&a[i+2][j-2] == 'X'&&a[i+3][j-3] == 'X'&&a[i+4][j-4] == 'X')
w = 1;
}
}
}
}
if(w == 1){
printf("选手B黑棋赢了!!Game over!!!\n");
break;
}
}
return 0;
}
上一篇: 扫雷游戏
下一篇: A*寻路算法Java实现