#include <stdio.h>
#include <stdlib.h>
#define m 30
int main (void)
{
int count;//计数器算横纵行的结果
int w,h;
int u;
int l;
int i,size;//i声明步数。size声明
int r[m][m] = {0};//数组声明(棋子位置)
int x, y;//声明落子坐标
int n;//声明棋盘大小n
char a[20],b[20];
printf ("请输入棋盘大小n\n");//编辑棋盘直到棋盘长度宽度大于4小于30
scanf ("%d", &n);
if (n<=4 || n>m)
{
do
{
printf ("输入的棋盘大小:4<n<%d\n", m);
scanf ("%d", &n);
}while (n<=4 || n>m);
}
getchar ();//声明玩家
printf ("请输入玩家1姓名:\n");
gets(a);
printf ("请输入玩家2姓名:\n");
gets(b);
for ( i = 1, size = n*n;i <= size; i++)//编辑棋盘
{
if (i%2 == 1)//如果i能被2整除,为玩家a相关信息
{
do//玩家a棋子信息
{
printf ("%s该你下棋了,第%d个棋子\n", a, i);
scanf ("%d%d", &x, &y);
if (x > n || x < 0)//判断坐标是否在棋盘内,如果不是则重新输入
{
do
{
printf ("0<=横坐标<=%d请重新输入横坐标\n", n);
scanf ("%d", &x);
}
while (x>m || x<0);
}
if (y > n || y < 0)//判断坐标是否在棋盘内,如果不是则重新输入
{
do
{
printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);
游戏免费源码分享网站 scanf ("%d", &y);
}while (y < 0 || y > n);
}
}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );
r[x][y] = 1;
for (u = 0;u < n; u++)//不同情况下判断玩家a获胜方式
{
for (l = 0;l < n;l++)
{
count = 0;
for (w = u,h = l;r[w][h] == 1 && h < n; h++)
count++;
if (count == 5)
{
printf ("%s是胜利者\n", a);
goto e;//直接跳转,其余代码不在运行
count = 0;
for (w = u, h = l; r[w][h] == 1 && w < n; w++)
count ++;
if (count == 5)
{
printf ("%s是胜利者\n", a);
goto e;
}
count = 0;
for (w = u,h = l; r[w][h] == 1 && w < n && h<n;w++,h++)
count++;
if (count == 5)
{
printf ("%s是胜利者\n", a);
goto e;
}
count = 0;
for (w =u ,h =l;r[w][h] == 1 && h > 0;h--)
count++;
if (count == 5)
{
printf ("%s是胜利者\n", a);
goto e;
}
}
}
}
system("cls");
for (int j = n;j>=0;j--)
{
printf ("%-2d", j);
for (int k = 0;k < n;k++)//画棋盘,声明两玩家棋子图片
{
if (r[k][j] == 0)
printf ("╋");
else if(r[k][j] == 1)
printf ("○");
else if (r[k][j] == 2)
printf ("●"); }
printf ("\n");
}
printf (" ");
for (int k = 0;k < n;k++)
printf ("%-2d", k);
}
else if (i%2 == 0)//如果i不能被2整除,为玩家b相关信息
{
do
{
printf ("\n%s该你下棋了,第%d个棋子\n", b, i);
scanf ("%d%d", &x, &y);
if (x > n || x < 0)
{
do
{
printf ("0<=横坐标<=%d请重新输入横坐标\n", n);
scanf ("%d", &x);
}while (x>n || x<0);
}
if (y >n|| y < 0)
{
do
{
printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);
scanf ("%d", &y);
}while (y < 0 || y > n);
}
}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );
r[x][y] = 2;
system("cls");
for (int j = n;j>=0;j--)
{
printf ("%-2d", j);
for (int k = 0;k < n;k++)
{
if (r[k][j] == 0)
printf ("╋");
else if(r[k][j] == 1)
printf ("○");
else if (r[k][j] == 2)
printf ("●");
}
printf ("\n");
}
printf (" ");
for (int k = 0;k < n;k++)
printf ("%-2d", k); printf ("\n");
count = 0;
for (u = 0;u < n; u++)
{
for (l = 0;l < n;l++)
{
count = 0;
for (w = u,h = l;r[w][h] == 2 && h < n; h++)
count++;
if (count == 5)
{
printf ("%s是胜利者\n", b);
goto e;
}
count = 0;
for (w = u, h = l; r[w][h] == 2 && w < n; w++)
count ++;
if (count == 5)
{
printf ("%s是胜利者\n", b);
goto e;
}
count = 0;
for (w = u,h = l; r[w][h] == 2 && w < n && h<n;w++,h++)
count++;
if (count == 5)
{
printf ("%s是胜利者\n", b);
goto e;
}
count = 0;
for (w =u ,h =l;r[w][h] == 2 && h > 0;h--)
count++;
if (count == 5)
{
printf ("%s是胜利者\n", b);
goto e;
}
}
}
}
}
e: for (int j = n;j>=0;j--)//游戏结束界面 棋盘固定 重新显示
{
printf ("%-2d", j);
for (int k = 0;k < n;k++)
{
if (r[k][j] == 0)
printf ("╋");
else if(r[k][j] == 1)
printf ("○");
else if (r[k][j] == 2)
printf ("●");
}
printf ("\n");
}
printf (" ");
for (int k = 0;k < n;k++)
printf ("%-2d", k); printf ("\n");
printf ("\a游戏愉快 ,Powered by Techmessager\n");//结束语句
return 0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论