资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

教你使用C语言写一个五子棋游戏-创新互联

这期内容当中小编将会给大家带来有关教你使用C语言写一个五子棋游戏,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

专注于为中小企业提供成都网站制作、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业香河免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

采用二维数组制作五子棋,用坐标下棋。

请看代码。

教你使用C语言写一个五子棋游戏

include
#include
#include
#include
#include
#include
 
void menu()//目录
{
 char xuanze = 0,xuanze2;
 printf("\n");
 printf("\n");
 printf("       #############################################\n");
 printf("           *五子棋小游戏*\n");
 printf("           *1.双人游戏*\n");
 printf("           *2.人机对战(暂未开放)\n");
 printf("           *3.游戏须知\n");
 printf("       #############################################\n");
 printf("          请选择选项,目前仅有 1 选项\n");
 printf("\n");
 printf("           ");
 xuanze = _getch();
 switch (xuanze)
 {
 case 1:
 break;
 case 3:
 printf("本游戏是键盘输入坐标来落子的,白棋先落,第二次落子为黑色,依次落子。\n");
 printf("若落子坐标非法则需重新落子。\n");
 printf("键盘输入1直接进入游戏\n");
 
 }
}
int t = 2;
int pan[20][20] =
{
 {1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
 {7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9}
};
void qp()//棋盘
{
 int i, j, k = 0;
 printf(" ");
 for (i = 0; i < 10; i++)
 printf(" %d", i + 1);
 for (i = 10; i < 20; i++)
 printf("%d", i + 1);
 printf("\n");
 printf("1 ");
 //printf("┌ ┬ ┐├ ┼ ┤└ ┴ ┘│ ");©○●┼
 for (i = 0; i < 20; i++)
 for (j = 0; j < 20; j++)
 {
 switch (pan[i][j])
 {
 case 1:
 printf("┌ ");
 k++;
 if (k % 20 == 0)
 {
  printf("\n");
 }  
 break;
 case 2:
 printf("┬ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 3:
 printf("┐ ");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
  printf("%d ", i + 1);
 }
 break;
 case 4:
 printf("├ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 5:
 printf("┼ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 6:
 printf("┤ ");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
  if(i<8)
  printf("%d ", i + 2);
  if(i>7)
  printf("%d", i + 2);
 }  
 break;
 case 7:
 printf("└ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 8:
 printf("┴ ");
 k++;
 if (k % 20 == 0)
  printf("\n");
 break;
 case 9:
 printf("┘ ");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i + 1);
  printf("\n");  
 }  
 break;
 case 10:
 printf("○");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
 } 
 break;
 case 11:
 printf("●");
 k++;
 if (k % 20 == 0)
 {
  printf("%d", i+1);
  printf("\n");
 } 
 break;
 }
 }
 printf(" ");
 for (i = 0; i < 10; i++)
 printf(" %d", i + 1);
 for (i = 10; i < 20; i++)
 printf("%d", i + 1);
 printf("\n");
 printf("\n");
}
void play()
{
 int i, j;
 while (1)
 {
 printf("*请选择落子坐标(x,y): ");
 scanf_s("%d %d", &j, &i);
 printf("\n");
 if(i>0&&i<21&&j>0&&j<21)
 {
 if (pan[i - 1][j - 1] != 10 && pan[i - 1][j - 1] != 11)
 {
  if (t % 2 == 1)
  {
  t++;
  pan[i - 1][j - 1] = 11;
  break;
  }
  else if (t % 2 == 0)
  {
  t++;
  pan[i - 1][j - 1] = 10;  
  break;
  }
 }
 else if (pan[i-1][j-1] == 10 || pan[i-1][j-1] == 11)
 {
  printf("此处已经有棋子,请重新输入\n");
  continue;
 }
 } 
 else if (i < 1 || i > 21 || j < 1 || j > 21)
 {
 printf("坐标不合法,请重新输入\n");
 continue;
 }
 } 
}
void test()
{
 int i, j;
 for(i=0;i<20;i++)
 for (j = 0; j < 20; j++)
 {
 if (pan[i][j] != 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9)
 {
 if (pan[i][j] == pan[i][j + 1]&&pan[i][j] == pan[i][j + 2]&&pan[i][j] == pan[i][j + 3]&&pan[i][j] == pan[i][j + 4])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n");  
  }  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n");  
  }  
 }
 if (pan[i][j] == pan[i + 1][j]&&pan[i][j] == pan[i + 2][j]&&pan[i][j] == pan[i + 3][j]&&pan[i][j] == pan[i + 4][j])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n");  
  }  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n");  
  }  
 }
 if (pan[i][j] == pan[i + 1][j + 1]&&pan[i][j] == pan[i + 2][j + 2]&&pan[i][j] == pan[i + 3][j + 3] &&pan[i][j]== pan[i + 4][j + 4])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n");  
  }  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n");  
  }  
 }
 if (pan[i][j] == pan[i - 1][j + 1]&&pan[i][j] == pan[i - 2][j + 2] &&pan[i][j]== pan[i - 3][j + 3]&&pan[i][j] == pan[i - 4][j + 4])
 {
  if (pan[i][j] == 11)
  {
  printf("白棋获胜\n");  
  }  
  if (pan[i][j] == 10)
  {
  printf("黑棋获胜\n");  
  } 
 }
 }
 }
}
int main()
{
 system("mode con cols=98 lines=48");
 SetConsoleTitleA("五子棋小游戏"); //设置窗口
 menu();
 system("cls");
 while (1)
 {
 qp();
 test(); 
 play();
 system("cls");
 }
 system("pause");
 return 0;
}

当前名称:教你使用C语言写一个五子棋游戏-创新互联
当前网址:http://www.cdkjz.cn/article/dgoosd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220