一个头文件game.h,两个c文件game.c和test.c
头文件game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 5
#define COL 5
//初始化棋盘函数
void InitBoard(int row, int col, char arr[ROW][COL]);
//打印棋盘函数
void DisplayBoard(int row, int col, char arr[ROW][COL]);
//下棋函数
void PlayGame(int row, int col, char arr[ROW][COL]);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//初始化棋盘函数
void InitBoard(int row, int col,char arr[ROW][COL])
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}
//打印棋盘函数
void DisplayBoard(int row, int col, char arr[ROW][COL])
{
int i = 0;
int j = 0;
printf(" ");
for (j = 0; j < col; j++)
{
printf(" %2d ",j);
}
printf("\n");
for (i = 0; i < row; i++)
{
printf(" ");
for (j = 0; j < col; j++)
{
if (j < col - 1)
printf("|---");
else
printf("|---|");
}
printf("\n");
printf("%2d ", i);
for (j = 0; j < col; j++)
{
if (j < col - 1)
printf("| %c ", arr[i][j]);
else
printf("| %c |",arr[i][j]);
}
printf("\n");
}
printf(" ");
for (j = 0; j < col; j++)
{
if (j < col - 1)
printf("|---");
else
printf("|---|");
}
printf("\n");
}
//判断输赢函数
int Judge(int row, int col, char arr[ROW][COL])
{
int i = 1;
int j = 1;
int ret = 0;
int isfull = 1;
for (i = 1; i < row - 1; i++)
{
for (j = 1; j < col - 1; j++)
{
if ((arr[i][j] == '*' && arr[i - 1][j] == '*' && arr[i + 1][j] == '*') || (arr[i][j] == '*' && arr[i][j - 1] == '*' && arr[i][j + 1] == '*') || (arr[i][j] == '*' && arr[i - 1][j - 1] == '*' && arr[i + 1][j + 1] == '*') || (arr[i][j] == '*' && arr[i - 1][j + 1] == '*' && arr[i + 1][j - 1] == '*'))ret = 1;
else if ((arr[i][j] == '#' && arr[i - 1][j] == '#' && arr[i + 1][j] == '#') || (arr[i][j] == '#' && arr[i][j - 1] == '#' && arr[i][j + 1] == '#') || (arr[i][j] == '#' && arr[i - 1][j - 1] == '#' && arr[i + 1][j + 1] == '#') || (arr[i][j] == '#' && arr[i - 1][j + 1] == '#' && arr[i + 1][j - 1] == '#'))ret = 2;
}
}
for (i = 0; i < row; i++)
{
for (j=0;j<col;j++)
{
if (arr[i][j] == ' ')
{
isfull = 0;
}
}
}
if (isfull == 1)
ret = 3;
return ret;
}
//下棋函数
void PlayGame(int row,int col,char arr[ROW][COL])
{
srand((unsigned int)time(NULL));
int result = 0 ;
int player = 0 ;
while (result == 0)
{
switch (player)
{
//玩家选择
case 0:
printf("请选择您的坐标:");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (arr[x][y] == ' ')
{
arr[x][y] = '*';
player = !player;
}
else
{
printf("选择错误,请重新选择\n");
Sleep(1000);
}
break;
case 1:
//机器选择
x = rand() % row;
y = rand() % col;
if (arr[x][y] == ' ')
{
arr[x][y] = '#';
player = !player;
}
break;
}
system("cls");
DisplayBoard(row, col, arr);
result =Judge(row, col, arr);
}
if (result == 1) printf("You Win\n");
else if (result == 2) printf("You Lost\n");
else if (result == 3) printf("平局\n");
}
主函数test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("************************\n");
printf("********* 1.start*******\n");
printf("********** 0.esit ******\n");
printf("************************\n");
}
void game()
{
printf("三子棋\n");
//定义棋盘
char Board[ROW][COL] = { 0 };
//初始化棋盘
InitBoard(ROW, COL, Board);
//打印棋盘
DisplayBoard(ROW,COL,Board);
//下棋函数
PlayGame(ROW, COL, Board);
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch (input)
{
case 1: game(); break;
case 0:printf("您已退出\n"); break;
default:printf("选择错误,请重新选择\n"); break;
}
} while (input);
}
int main()
{
test();
return 0;
}