目录
1.主函数的设计
2.头文件的设计
3.游戏的具体实现
1)游戏流程的设计
2)棋盘的初始化
3)打印棋盘
4)下棋
5)判断输赢
1)创建test.c文件
int main()
{
test();
return 0;
}
2)test.c的实现
void test()
{
int input = 0;
//是否进入游戏,先进入循环再判断,do-while
do
{
menu(); //打印菜单
printf("please input a number:(0/1)->");
scanf("%d", &input);
switch (input)
{
case 1:
printf("Let's WinWin !\n"); //开始游戏
game(); //先想好怎样使用函数,再去想如何实现(game)函数
break;
case 0:
printf("Bye!\n"); //退出游戏
break;
default:
printf("Wrong!\n"); //非0/1
break;
}
} while (input); //input为0时退出程序
}
创建头文件game.c。
头文件的主要作用在于多个代码文件全局变量 (函数)的重用、防止定义的冲突,对各个被调用函数给出一个描述,其本身不需要包含程序的逻辑实现代码,它只起描述性作用,用户程序只需要按照头文件中的接口声明来调用相关函数或变量,链接器会从库中寻找相应的实际定义代码。
头文件是用户应用程序和函数库之间的桥梁和纽带。
代码的实现:
#define _CRT_SECURE_NO_WARNINGS
#define ROW 9 //便于以后修改棋盘大小
#define COL 9
#include
#include //time(NULL)的头文件
#include //srand((unsigned int)time(NULL)),rand()的头文件
#include //system("cls")的头文件,用于清空屏幕
void InitBoard(char arr[ROW][COL], int row, int col);
void DisplayBoard(char arr[ROW][COL], int row, int col);
void PlayerMove(char arr[ROW][COL], int row, int col);
void ComputerMove(char arr[ROW][COL], int row, int col);
char Is_Win(char arr[ROW][COL], int row, int col);
#include"game.h"
void game()
{
char ret = 0;
char arr[ROW][COL] = { 0 };
srand((unsigned int)time(NULL));
InitBoard(arr, ROW, COL);
DisplayBoard(arr, ROW, COL);
while (1)
{
printf("It's your turn.\n");
PlayerMove(arr, ROW, COL);
ret = Is_Win(arr, ROW, COL);
if (ret != 'c')
break;
DisplayBoard(arr, ROW, COL);
printf("Please wait for you turn.\n");
ComputerMove(arr, ROW, COL);
ret = Is_Win(arr, ROW, COL);
if (ret != 'c')
break;
DisplayBoard(arr, ROW, COL);
}
DisplayBoard(arr, ROW, COL);
if (ret == '#')
printf("You win!\n");
else if (ret == '*')
printf("Lose! To be continued...\n");
else
printf("WinWin!\n");
}
#include"game.h"
void InitBoard(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}
void DisplayBoard(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
system("cls");
for (i = 0; i <= row; i++)
{
if (i == 0)
{
for (j = 0; j < col; j++)
{
if (j == 0)
{
printf(" ");
}
else
{
printf(" ");
}
if (j < col)
printf("%d", j+1);
}
printf("\n");
}
for (j = 0; j <= col; j++)
{
if (j == 0)
{
printf(" ");
}
else
{
printf(" ");
}
if (j < col)
printf("|");
}
printf("\n");
if (i < row )
{
for (j = 0; j <= col; j++)
{
if (j == 0)
{
printf("%d---", i+1);
}
else
{
printf("---");
}
if (j < col)
printf("%c", arr[i][j]);
}
}
printf("\n");
}
}
打出来是这个亚子
void PlayerMove(char arr[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("please input the coordinates->");
scanf("%d%d", &x, &y);
if (x > 0 & x <= row & y > 0 & y <= col)
{
if (arr[x - 1][y - 1] == ' ')
{
arr[x - 1][y - 1] = '#';
break;
}
else
{
printf("The position has been occupied.\n");
}
}
else
{
printf("Wrong!\n");
}
}
}
void ComputerMove(char arr[ROW][COL], int row, int col)
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (arr[x - 1][y - 1] == ' ')
{
arr[x - 1][y - 1] = '*';
break;
}
}
}
int Is_Full(char arr[ROW][COL], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
return 0;
}
return 1;
}
}
char Is_Win(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)//行
{
for (j = 0; j + 4 < col; j++)
{
if (arr[i][j] == arr[i][j + 1] && arr[i][j + 1] == arr[i][j + 2] && arr[i][j + 2] == arr[i][j + 3] && arr[i][j + 3] == arr[i][j + 4] && arr[i][j] != ' ')
return arr[i][j];
}
}
for (j = 0; j < row; j++)//列
{
for (i = 0; i + 4 < row; i++)
{
if (arr[i][j] == arr[i+1][j] && arr[i+1][j] == arr[i + 2][j] && arr[i + 2][j] == arr[i + 3][j] && arr[i + 3][j] == arr[i + 4][j] && arr[i][j] != ' ')
return arr[i][j];
}
}
for (i = 0; i+4 < row; i++)//'\'
{
for (j = 0; j+4 < col; j++)
{
if (arr[i][j] == arr[i + 1][j + 1] && arr[i + 1][j + 1] == arr[i + 2][j + 2] && arr[i + 2][j + 2] == arr[i + 3][j + 3] && arr[i + 3][j + 3] == arr[i + 4][j + 4] && arr[i][j] != ' ')
return arr[i][j];
}
}
for (i = 4; i < row; i++)//'/'
{
for (j = 0; j+4 < col; j++)
{
if (arr[i][j] == arr[i - 1][j + 1] && arr[i - 1][j + 1] == arr[i - 2][j + 2] && arr[i - 2][j + 2] == arr[i - 3][j + 3] && arr[i - 3][j + 3] == arr[i - 4][j + 4] && arr[i][j] != ' ')
return arr[i][j];
}
}
if (Is_Full(arr, row, col))
return 'Q'; //打成平手
else
return 'C'; //继续游戏
}