C语语言言实实现现三三子子棋棋程程序序
本文实例为大家分享了C语言实现三子棋的具体代码,供大家参考,具体内容如下
直接上代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include 2.实现三子棋游戏。
#include Sleep() RAND_MAX 的头文件
void menu() 打印菜单
{
printf("****************************\n");
printf("**** 欢迎来到三子棋游戏 ****\n");
printf("**** 1、 进入游戏 ****\n");
printf("**** 0、 退出游戏 ****\n");
printf("****************************\n");
printf("请输入:->");
}
void print_chessboard(char coord[][3]) 打印棋盘函数
{ 多维数组在传参时,接收数组的形参最多只能是第一个方括号里没有数字 (下标范围)
否则就会出错 (因为此时编译器不知道你要把传过来的数组的元素划分成几行几列,
但是当除第一个方括号的其他方括号都有值时,就可以经过计算知道第一个方括号的值是多少
int i = 0;
int index_x = 0;
int index_y = 0;
for (i = 1; i <= 153; i++)
{
char out_ch = ' ';
if ((i % 51 == 20) || (i % 51 == 26) || (i % 51 == 32) )
{
out_ch = coord[index_x][index_y];
index_x++;
if (index_x < 3)
{
index_x = 0;
index_y++;
}
}
else if ((i % 17 == 6) || (i % 17 == 12))
{
out_ch = '|';
}
else if( (i >= 35 && i <= 51 && i != 40 && i != 46) || \
(i >= 86 && i <= 102 && i != 91 && i != 97))
{
out_ch = '_ ';
}
putchar(out_ch);
if (i % 17 == 0) 每输出 17 个字符换下一行输出
{
printf("\n");
}
}
}
void winer(char coord[][3], int *flag); 赢家判断函数的声明
int computer(char coord[][3]) 电脑下棋
{
int flag = 0;
int index_x2 = 0;
int index_y2 = 0;
srand((unsigned)time(NULL));
while (1)
{
index_x2 = 2 * rand() RAND_MAX; 产生 0--2 的随机数
index_y2 = 2 * rand() RAND_MAX;
if ((coord[index_x2][index_y2] != '*') && (coord[index_x2][index_y2] != 'o '))
{ 判断该位置是否已有落子
coord[index_x2][index_y2] = 'o ';
winer(coord, &flag);
if (flag =