创建源文件mian.c写主程序
#include"game.h"
void menu()
{
printf("##############################################\n");
printf("## 欢 迎 来 到 我 的 游 戏 世 界 ##\n");
printf("##############################################\n");
printf("### 1. PLAY 2.EXIT ##\n");
printf("##############################################\n");
printf("请选择:");
}
int main()
{
int select = 0;//判断是否继续玩下去
while (!select)
{
menu();
int choose;//接受你的选项
scanf("%d", &choose);
switch (choose)
{
case 1:
game();
break;
case 2:
select = 1;
break;
default:
printf("你的选择有误,请重新选择");
break;
}
}
return 0;
}
创建头文件game.h声明所要用的子函数以及宏
#pragma once
#include<stdio.h>
#pragma warning(disable: 4996)
#define cheer 'O'//玩家所用棋子
#define computer 'X'//电脑所用棋子
#include<time.h>
#define begin '_'//没有下棋子的棋盘内初始化的内容
#define size 3//棋盘大小
#define len 3//胜出 所需要连棋子的个数
void game();
void show(char a[][size]);
int check(char a[][size],int x,int y,char s);
void init(char a[][size]);
void com(char a[][size],int* i,int* j);
int full(char a[][size]);
创建相应的源文件game.c实现子函数
#include"game.h"
void game()
{
char a[size][size];
int i, j;
init(a);
int x, y;
srand((unsigned)time(NULL));
int t = rand() % 3;
if (len > size)
{
printf("所用的棋盘太小\n");
}
if (t == 1)
{
com(a, &i, &j);
printf("电脑先走\n");
}
while (1)
{
show(a);
printf("输入你的落子位置:");
scanf("%d %d",&x,&y);
if (x > size || x<1 || y>size || y < 1)
{
printf("请输入一个合法的位置\n");
continue;
}
if (a[x - 1][y - 1] == begin)
{
a[x - 1][y - 1] = cheer;
if (1 == check(a, x - 1, y - 1,cheer))
{
show(a);
printf("玩家获胜\n");
break;
}
if (full(a) == 1)
{
break;
}
show(a);
com(a, &i, &j);
if (1 == check(a, i, j,computer))
{
show(a);
printf("电脑获胜\n");
break;
}
if (full(a) == 1)
{
break;
}
}
else
{
printf("此处已有棋子请重新输入\n");
}
}
}
int check(char a[][size],int x,int y,char s)
{
int i = x, j = y;
int count = 0;
while (i >= 0 && a[i][j] == s)
{
count++;
i--;
}
i = x;
j = y;
while (i <size && a[i][j] == s)
{
count++;
i++;
}
if (count-1 == len)
{
return 1;
}
i = x;
j = y;
count = 0;
while (i >= 0&&j>=0 && a[i][j] == s)
{
count++;
i--;
j--;
}
i = x;
j = y;
while (i <size &&j<size &&a[i][j] == s)
{
count++;
i++;
j++;
}
if (count-1 == len)
{
return 1;
}
i = x, j = y;
count = 0;
while (j >= 0 && a[i][j] == s)
{
count++;
j--;
}
i = x;
j = y;
while (j <size && a[i][j] == s)
{
count++;
j++;
}
if (count - 1 == len)
{
return 1;
}
i = x;
j = y;
count = 0;
while (i >= 0 && j <size && a[i][j] == s)
{
count++;
i--;
j++;
}
i = x;
j = y;
while (i <size &&j>=0 &&a[i][j] == s)
{
count++;
i++;
j--;
}
if (count - 1 == len)
{
return 1;
}
return 0;
}
int full(char a[][size])
{
int i;
int j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if (a[i][j] == begin)
{
return 0;
}
}
}
show(a);
printf("平局\n");
return 1;
}
void show(char a[][size])
{
int i;
int j;
for (i = 0; i <= size; i++)
{
printf("____");
}
printf("\n");
printf(" |");
for (i = 1; i <= size; i++)
{
printf(" %d |",i);
}
printf("\n");
for (i = 1; i <= size; i++)
{
printf(" %d |",i);
for (j = 0; j < size; j++)
{
printf(" %c |",a[i-1][j]);
}
printf("\n");
}
}
void com(char a[][size], int* x, int* y)
{
while (1)
{
srand((unsigned)time(NULL));
*x = rand() % size;
*y = rand() % size;
if (a[*x][*y] == begin)
{
a[*x][*y] = computer;
break;
}
}
}
void init(char a[][size])
{
int i;
int j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
a[i][j] = begin;
}
}
}