自定义的头文件threechessgame.h
#define _CRT_SECURE_NO_WARNINGS_
#ifndef _THREECHESSGAME_H_
void menu();//菜单,供玩家选择开始与退出
void play();
void initboundary();//初始化棋盘
void player();//玩家下棋
void boundary(char a[3][3]);//展示棋盘
void computer(char a[3][3]);//电脑下棋
int judgement(char a[3][3]);//判断是否输赢已定
int draw(char a[3][3]);
#endif
源文件test.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "threechessgame.h"
char a[3][3] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do{
menu();
scanf("%d", &input);
switch (input)
{
case(1) : memset(a, '\0', 9);
play();
break;
case(0) : break;
default:printf("无效,请重新输入:\n"); break;
}
}
while (input);
system("pause");
return 0;
}
void menu()
{
printf("*********请选择:**********\n");
printf("*********1.play************\n");
printf("*********0.exit************\n");
printf("***************************\n");
}
void play()
{initboundary(a);
while (1)
{player(a);
boundary(a);
if (judgement(a) == 1) { printf("游戏结束,玩家赢!\n"); break; }
else if (judgement(a) == 2) { printf("游戏结束,电脑赢!\n"); break; }
else if (draw(a) == 1) { printf("本轮棋盘已满,平局!\n"); break; }
computer(a);
boundary(a);
if (judgement(a) == 1) { printf("游戏结束,玩家赢!\n"); break; }
else if (judgement(a) == 2) { printf("游戏结束,电脑赢!\n"); break; }
else if (draw(a) == 1) { printf("本轮棋盘已满,平局!\n"); break; }
}
}
源文件game.c
#define _CRT_SECURE_NO_WARNINGS_
#include <stdio.h>
#include <stdlib.h>
#include "threechessgame.h"
#include <time.h>
void initboundary()
{
int i;
int j;
for (i = 0; i < 3; i++)
{
for(j = 0; j< 3; j++)
{
printf("___|");
}
printf("\n");
}
}
void player(char a[3][3])
{
int x = 0; int y = 0;
while (1)
{
printf("请玩家输入下棋坐标x,y\n");
scanf_s("%d,%d", &x, &y);
if (a[x - 1][y - 1] == '\0') {
a[x - 1][y - 1] = '*'; break;
}
else printf("无效坐标,请重新输入!\n");
}
}
void boundary(char a[3][3])
{
int i;
int j;
for (i = 0; i < 3; i++)
{for (j = 0; j < 3; j++)
{
printf("_%c_|",a[i][j]);
}
printf("\n");
}
}
void computer(char a[3][3])
{
int x = 0; int y = 0;
printf("电脑下棋'$'\n");
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (a[x][y] != '*'&&a[x][y]!='$') { a[x][y] = '$'; break; }
}
}
int judgement(char a[3][3])
{
int num = 0;
if ((a[0][0] == a[0][1] && a[0][1] == a[0][2] && a[0][2] == '*') || (a[1][0] == a[1][1] && a[1][1] == a[1][2] && a[1][2] == '*') || (a[2][0] == a[2][1] && a[2][1] == a[2][2] && a[2][2] == '*') || (a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[2][2] == '*') || (a[0][2] == a[1][1] && a[1][1] == a[2][0] && a[2][0] == '*') || (a[0][0] == a[1][0] && a[1][0] == a[2][0] && a[2][0] == '*') || (a[0][1] == a[1][1] && a[1][1] == a[2][1] && a[2][1] == '*') || (a[0][2] == a[1][2] && a[1][2] == a[2][2] && a[2][2] == '*'))
return 1;
else if ((a[0][0] == a[0][1] && a[0][1] == a[0][2] && a[0][2] == '$') || (a[1][0] == a[1][1] && a[1][1] == a[1][2] && a[1][2] == '$') || (a[2][0] == a[2][1] && a[2][1] == a[2][2] && a[2][2] == '$') || (a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[2][2] == '$') || (a[0][2] == a[1][1] && a[1][1] == a[2][0] && a[2][0] == '$') || (a[0][0] == a[1][0] && a[1][0] == a[2][0] && a[2][0] == '$') || (a[0][1] == a[1][1] && a[1][1] == a[2][1] && a[2][1] == '$') || (a[0][2] == a[1][2] && a[1][2] == a[2][2] && a[2][2] == '$'))
return 2;
}
int draw(char a[3][3])
{
int i = 0; int j = 0; int num = 0;
for (i = 0; i < 3;i++)
for (j = 0; j < 3;j++)
if (a[i][j] != '\0') num++;
if (num == 9) return 1;
else return 0;
}
运行结果为:
gag
反思问题:只适合三子棋,太过单调,后续应优化至多子棋模式,而实现多子棋,较难的问题在于判断输赢上,优化时应考虑效率高的判断方式。
虽然只是最简单的游戏和界面,但我在完成它后感到了小小的成就感,大的项目正是由若干个小项目构成的,只有不轻视小项目,并不断实现优化,才能为将来完成大项目奠定坚实的基础。