返回首页

设计一个8*8棋盘的五子棋对弈过程,分别输入甲方和乙方的下棋位置,计算机可判断胜负的C++程序

时间:2022-12-09 02:27 点击:98 编辑:邮轮网

/*

五子棋

*/

#include <stdio.h>

#define N 8

#define M 5

char FiveChess[N + 1][N + 1] = {'0'};

int judge()

{

int i,j;

//行方向

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

{

for(j = 1; j <= N - M + 1;)

{

char buf = FiveChess[i][j];

if(buf == '0')

{

j++;

}

else

{

int num = 1;

do{

j++;

if(num == M)

return 1;

if(j <= N && buf == FiveChess[i][j])

{

num++;

}

else

break;

}while(1);

}

}

}

//列方向

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

{

for(j = 1; j <= N - M + 1;)

{

char buf = FiveChess[j][i];

if(buf == '0')

{

j++;

}

else

{

int num = 1;

do{

j++;

if(num == M)

return 1;

if(j <= N && buf == FiveChess[j][i])

{

num++;

}

else

break;

}while(1);

}

}

}

//左斜线方向

for(i = 1; i <= N - M + 1; i++)

{

for(j = 1; j <= N - M + 1;)

{

int ii = i;

int jj = j;

char buf = FiveChess[ii][jj];

if(buf == '0')

{

j++;

}

else

{

int num = 1;

do{

ii++;

jj++;

if(num == M)

return 1;

if(ii <= N && jj <= N && buf == FiveChess[ii][jj])

{

num++;

}

else

{

j++;

break;

}

}while(1);

}

}

}

//右斜线方向

for(i = 1; i <= N - M + 1; i++)

{

for(j = M; j <= N;)

{

int ii = i;

int jj = j;

char buf = FiveChess[ii][jj];

if(buf == '0')

{

j++;

}

else

{

int num = 1;

do{

ii++;

jj--;

if(num == M)

return 1;

if(ii <= N && jj >= 1 && buf == FiveChess[ii][jj])

{

num++;

}

else

{

j++;

break;

}

}while(1);

}

}

}

return 0;

}

void show()

{

int i,j;

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

{

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

{

if(FiveChess[i][j] == '0')

printf( .);

else

printf( %c,FiveChess[i][j]);

}

printf(\n);

}

}

int update(int x,int y,int T)

{

if(0 == ((x > 0 && x <= N && y > 0 && y <= N) && FiveChess[x][y] == '0'))

return -1;

if(T == 0)

FiveChess[x][y] = '#';

else

FiveChess[x][y] = '*';

show();

int buf = judge();

if(buf == 1)

{

if(T == 0)

printf(甲获胜\n);

else

printf(乙获胜\n);

}

return buf;

}

void MainLoop()

{

int T = 0;

do{

if(T == 0)

{

int buf;

printf(甲输入坐标:);

int x,y;

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

buf = update(x , y , T);

if(buf == 1)

break;

if(buf == -1)

{

do{

printf(输入有错,请重新输入:);

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

buf = update(x , y , T);

}while(buf == -1);

if(buf == 1)

break;

}

T = 1;

}

else

{

int buf;

printf(乙输入坐标:);

int x,y;

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

buf = update(x , y , T);

if(buf == 1)

break;

if(buf == -1)

{

do{

printf(输入有错,请重新输入:);

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

buf = update(x , y , T);

}while(buf == -1);

if(buf == 1)

break;

}

T = 0;

}

}while(1);

}

int main(int argc, const char * argv[])

{

int i,j;

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

{

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

FiveChess[i][j] = '0';

}

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

{

FiveChess[0][i] = '0' + i;

FiveChess[i][0] = '0' + i;

}

MainLoop();

return 0;

}

顶一下
(0)
0%
踩一下
(0)
0%
必看十大热文

请选择遇到的问题

观点错误
内容与标题不符
内容陈旧
内容质量差
内容不够全面
已收到你的问题反馈