总结:
1、此次写三子棋小游戏,应用了头文件和源文件的写法,使整个项目的层次更加清晰;
2、环境:vs2013;
3、游戏介绍:
此版三子棋是玩家和电脑进行依次轮流下棋,玩家先开始;
若是玩家输入的坐标不符合该游戏的设计,则会提示,直至输入正确为止;
在此过程中,电脑会拦截玩家棋子,但在这之前,它会先自己判断自己是否有一步胜利的情况,若是有,那么你就输了;
在下棋的过程中,每轮下来会更新屏幕;
此游戏的结果会出现三种情况:玩家赢;玩家输;平局。

//threeChess.h
#ifndef _THREECHESS_H_
#define _THREECHESS_H_
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#pragma warning(disable:4996)
#define ROW 3
#define COL 3
void game();
#endif
//main.c
#include"threeChess.h"
void tipMassage()
{
printf("******************************************\n");
printf("**********欢迎访问三子棋人机版!**********\n");
printf("***** input 1:play! input 0:exit! *****\n");
printf("please select:");
}
int main()
{
int select;
do{
tipMassage();
scanf("%d", &select);
switch (select)
{
case 0:
exit(0);//终止调用进程
case 1:
game();
break;
default:
printf("input error!");
break;
}
} while (1);
system("pause");
return 0;
}
//threeChess.c
#include"threeChess.h"
char triChe[ROW][COL];
static void showCheckerBoard()
{
for (int i = 0; i < ROW; i++){
printf(" %c | %c | %c ", triChe[i][0], triChe[i][1], triChe[i][2]);
if (i < ROW - 1){
printf("\n---|---|---\n");
}
}
printf("\n");
}
static int isFull()
{
char *p = &triChe[0][0];
for (int i = 0; i < ROW*COL; i++){
if (*(p + i) == ' '){
return 0;
}
}
return 1;
}
static char judge()
{
//判断行
int i = 0;
for (; i < ROW; i++){
if ((triChe[i][0] == triChe[i][1]) && (triChe[i][1] == triChe[i][2]) && \
(triChe[i][0] != ' ')){
return triChe[i][0];
}
}
//判断列
for (i = 0; i < COL; i++){
if ((triChe[0][i] == triChe[1][i]) && (triChe[1][i] == triChe[2][i]) && \
(triChe[0][i] != ' ')){
return triChe[0][i];
}
}
//判断斜线
if ((triChe[0][0] == triChe[1][1]) && (triChe[1][1] == triChe[2][2]) && \
(triChe[0][0] != ' ')){
return triChe[0][0];
}
if ((triChe[0][2] == triChe[1][1]) && (triChe[1][1] == triChe[2][0]) && \
(triChe[0][2] != ' ')){
return triChe[0][2];
}
//是否平局
if (isFull()){
return 'p';
}
return ' ';
}
static void playerMan()
{
int x, y;
do{
printf("please input the position <x y> of your chess pieces!\n");
scanf("%d%d", &x, &y);
if (x < 0 || x > 2 || y < 0 || y > 2){
printf("the position out of bounds!\n");
}else if (triChe[x][y] != ' '){
printf("There are already pieces in this position!\n");
}else{
triChe[x][y] = 'x';
break;
}
} while (1);
}
static void playMachine()
{
//遵循先己后拦
//行:' ''o''o' or 'o'' ''o' or 'o''o'' '
int i = 0;
int n = 2;
char ch = 'o';
while (n){//先判断自己要否一步可以胜利若没有再拦截对方
for (i = 0; i < ROW; i++){
if ((triChe[i][0] == ' ') && (triChe[i][1] == triChe[i][2]) && \
(triChe[i][1] == ch)){
triChe[i][0] = 'o';
return;
}
if ((triChe[i][0] == triChe[i][2]) && (triChe[i][1] == ' ') && \
(triChe[i][0] == ch)){
triChe[i][1] = 'o';
return;
}
if ((triChe[i][0] == triChe[i][1]) && (triChe[i][2] == ' ') && \
(triChe[i][0] == ch)){
triChe[i][2] = 'o';
return;
}
}
//列
for (i = 0; i < COL; i++){
if ((triChe[0][i] == ' ') && (triChe[1][i] == triChe[2][i]) && \
(triChe[1][i] == ch)){
triChe[0][i] = 'o';
return;
}
if ((triChe[0][i] == triChe[2][i]) && (triChe[1][i] == ' ') && \
(triChe[0][i] == ch)){
triChe[1][i] = 'o';
return;
}
if ((triChe[0][i] == triChe[1][i]) && (triChe[2][i] == ' ') && \
(triChe[0][i] == ch)){
triChe[2][i] = 'o';
return;
}
}
//斜线反斜
if ((triChe[0][0] == ' ') && (triChe[1][1] == triChe[2][2]) && \
(triChe[1][1] == ch)){
triChe[0][0] = 'o';
return;
}
else if ((triChe[0][0] == triChe[2][2]) && (triChe[1][1] == ' ') && \
(triChe[0][0] == ch)){
triChe[1][1] = 'o';
return;
}
else if ((triChe[0][0] == triChe[1][1]) && (triChe[2][2] == ' ') && \
(triChe[1][1] == ch)){
triChe[2][2] = 'o';
return;
};
//正斜
if ((triChe[0][2] == ' ') && (triChe[1][1] == triChe[0][2]) && \
(triChe[1][1] == ch)){
triChe[0][2] = 'o';
return;
}
else if ((triChe[0][2] == triChe[2][0]) && (triChe[1][1] == ' ') && \
(triChe[0][2] == ch)){
triChe[1][1] = 'o';
return;
}
else if ((triChe[0][2] == triChe[1][1]) && (triChe[2][0] == ' ') && \
(triChe[1][1] == ch)){
triChe[2][0] = 'o';
return;
}
n--;
ch = 'x';
}
int x, y;
do{
srand((unsigned)time(NULL));
x = rand() % 3;
y = rand() % 3;
} while (triChe[x][y] != ' ');
triChe[x][y] = 'o';
}
void game()
{
memset(triChe, ' ', ROW*COL);
char ret;
do{
system("cls");
showCheckerBoard();
playerMan();
ret = judge();
if (ret != ' '){
break;
}
playMachine();
ret = judge();
} while (ret == ' ');
system("cls");
showCheckerBoard();
switch (ret){
case 'x':
printf("恭喜你,赢了!\n");
break;
case 'o':
printf("你输了!\n");
break;
case 'p':
printf("平局!\n");
break;
default:
break;
}
}
本文介绍了一个使用C语言实现的三子棋小游戏,通过玩家与电脑的交互进行游戏。游戏运用了头文件和源文件分离的方式,提高了代码的可读性和可维护性。玩家和电脑轮流落子,电脑具备简单的策略,能够自我判断并阻止玩家获胜。
1829

被折叠的 条评论
为什么被折叠?



