要创建一个game.h的头文件,把程序所用到的头文件,函数声明都放进去。看着比较清晰,当然不是最一开始就写全的,是写的过程中往里面添加的。
这是我最后写好后的头文件内容
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __GAME_H__
#define __GAME_H__
#define Row 3 //行
#define Col 3 //列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
void menu();
void game();
void PlayerGo(char show[Row][Col], int row, int col);
void Display(char arr[Row][Col], int row, int col);
void Init(char arr[Row][Col], int row, int col, char f);
char IsWin(char show[Row][Col], int row, int col);
void ComputerGo(char show[Row][Col], int row, int col);
#endif // __GAME_H__
最开始写的应该是主函数,放在tast.c的测试源文件中,根据三子棋格式,存储应该是一个二维数组3×3的。
创建完,要给他初始化赋值,我调用的是memset函数,因为我的数组的char类型而memset函数正好是按字符初始化。
然后打印显示函数,对应着在头文件声明,在game.c源文件中定义。
我写的是简单的玩家先走的函数,通过iswin函数判断输赢。返回。
#include"game.h"
void game() //游戏
{
char show[Row][Col];
Init(show, Row, Col, ' ');
Display(show, Row, Col);
while (1)
{
char fin=0;
PlayerGo(show, Row, Col);
fin=IsWin(show, Row, Col);
if (fin != 0)break;
ComputerGo(show, Row, Col);
fin = IsWin(show, Row, Col);
if (fin != 0)break;
}
if (IsWin(show, Row, Col) == 'Y')
{
system("cls");
system("color F2");
Display(show, Row, Col);
printf("\n\n\n\t\t恭喜您,您赢了哦*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。\n");
}
else if (IsWin(show, Row, Col) == 'N')
printf("\n\n\n\t\t很遗憾您输了。\n");
else printf("\n\n\n\t\t平局!\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
printf("请输入您的选择:>");
scanf("%d", &input);
if (input == 1)
{
game(); break;
}
else if (input == 0) printf("\t退出游戏\n");
} while (input != 0);
return 0;
}
对应实现test函数中的一些具体步骤的函数。
#include"game.h"
void menu()//菜单
{
system("cls");
system("color F9");
printf("\n\n\t****************************\n");
printf("\t**** 1.开始游戏 *****\n");
printf("\t**** 0.退出 *****\n");
}
int Sum_Meb(char arr[Row][Col], int row, int col, char f)
{
int count = 0;
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
if (arr[i][j] == f)
count++;
return count;
}
//返回玩家是否赢了
int Ret_Play(char arr[Row][Col], int row, int col, int f)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (arr[i][0] == arr[i][1] &&arr[i][0] == arr[i][2]&&arr[i][0] == f)
{
return 1;
}
else if (arr[0][i] ==arr[1][i]&& arr[1][i]== arr[2][i]&&arr[2][i] == f)
{
return 1;
}
}
if (arr[0][0] ==arr[1][1]&& arr[1][1] == arr[2][2]&&arr[1][1] == f)return 1;
if (arr[0][2] == arr[1][1]&&arr[1][1] == arr[2][0]&&arr[1][1] == f)return 1;
return 0;
}
void Init(char arr[Row][Col], int rows, int cols, char f)//数组初始化
{
memset(arr, f, rows*cols);
}
void Display(char arr[Row][Col], int row, int col)//显示
{
int i = 0;
int j = 0;
printf("\t\t\t");
for (i = 0; i < row; i++)
{
printf(" ___");
}
for (i = 0; i < row; i++)
{
printf("\n\t\t\t|");
for (j = 0; j < col; j++)
{
printf("_%c_|", arr[i][j]);
}
}
}
//玩家在合适位置落子
void PlayerGo(char show[Row][Col], int row, int col)
{
int r = 0;
int c = 0;
while (1)
{
printf("\n\n\t\t请玩家走(如 1 1):>");
scanf("%d %d", &r, &c);
if (show[r-1][c-1] == ' ')
{
show[r-1][c-1] = 'X';
Display(show, row, col);
break;
}
else printf("输入错误\n");
}
}
//判断输赢
char IsWin(char show[Row][Col], int row, int col)
{
if (Sum_Meb(show, row, col, ' ') == 0)
return 'D';
else if (Ret_Play(show, row, col, 'X') == 1)
return 'Y';
else if (Ret_Play(show, row, col, 'O') == 1)
return 'N';
return 0;
}
//电脑在合适位置随机生成随机值,
void ComputerGo(char show[Row][Col], int row, int col)
{
int r = 0;
int c = 0;
printf("\n\n\t\t请电脑走:>\n");
while (1)
{
r = rand() % row + 1;
c = rand() % col + 1;
if (show[r][c] == ' ')
{
show[r][c] = 'O';
Display(show, row, col);
break;
}
}
}