游戏例图如下
1.test.c(游戏逻辑的实现)
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("+++++++++++++++++++++\n");
printf("++++1.play 0.exit++++\n");
printf("+++++++++++++++++++++\n");
}
void game()
{
char ret;
//玩家赢 *
//电脑赢 #
//平局 q
//继续 c
char chessboard[LINE][COLUME] = { 0 };
board(chessboard, LINE, COLUME);//初始化棋盘
displayboard(chessboard, LINE, COLUME);//打印棋盘
while (1)
{
playerboard(chessboard, LINE, COLUME);//玩家下棋
ret = IsWin(chessboard, LINE, COLUME);//判断输赢
if (ret != 'c')
{
break;
}
displayboard(chessboard, LINE, COLUME);//打印棋盘
computerboard(chessboard, LINE, COLUME);//电脑下棋
ret = IsWin(chessboard, LINE, COLUME);//判断输赢
if (ret != 'c')
{
break;
}
displayboard(chessboard, LINE, COLUME);//打印棋盘
}
if (ret == '*')
{
printf("恭喜你赢了\n");
displayboard(chessboard, LINE, COLUME);//打印棋盘
}
else if (ret == '#')
{
printf("菜鸟\n");
displayboard(chessboard, LINE, COLUME);//打印棋盘
}
else
{
printf("势均力敌\n");
displayboard(chessboard, LINE, COLUME);//打印棋盘
}
}
int main()
{
srand((unsigned int)time(NULL));
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);
return 0;
}
2.game.h(头文件)
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
#define LINE 3
#define COLUME 3
//初始化棋盘,棋盘每个空格上是一个字符
void board(char chessboard[LINE][COLUME], int line, int colume);
//打印棋盘
void displayboard(char chessboard[LINE][COLUME], int line, int colume);
//玩家下棋
void playerboard(char chessboard[LINE][COLUME], int line, int colume);
//电脑下棋
void computerboard(char chessboard[LINE][COLUME], int line, int colume);
//判断输赢
char IsWin(char chessboard[LINE][COLUME], int line, int colume);
3.game.c(游戏运行主代码)
#include"game.h"
void board(char chessboard[LINE][COLUME], int line, int colume)//初始化棋盘
{
int i = 0;//i表示行
int j = 0;//j表示列
for (i = 1; i <= LINE; i++)
{
for (j = 1; j <= COLUME; j++)
{
chessboard[i-1][j-1] = ' ';//将数组中每一个元素都初始化为空格
}
printf("\n");
}
}
void displayboard(char chessboard[LINE][COLUME], int line, int colume)//打印棋盘
{
int i = 0;
int j = 0;
for (i = 1; i <= LINE; i++)
{
for (j = 1; j <= COLUME; j++)
{
printf(" %c ",chessboard[i-1][j-1]);
if (j < COLUME)
{
printf("|");
}
}
printf("\n");
if (i < LINE)
{
for (j = 1; j <= COLUME; j++)
{
printf("---");
if (j < COLUME)
printf("|");
}
}
printf("\n");
}
}
void playerboard(char chessboard[LINE][COLUME],int line,int colume)//玩家下棋
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家下棋,请输入坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= LINE && y >= 1 && y <= COLUME)
{
chessboard[x-1][y-1] = '*';//将数组中的指定元素改为 * ,当作玩家下的棋
break;
}
else
{
printf("输入非法,请重新输入");
}
}
}
void computerboard(char chessboard[LINE][COLUME], int line, int colume)//电脑下棋
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % LINE;
y = rand() % COLUME;
if (chessboard[x][y] == ' ')
{
chessboard[x][y] = '#'; //将数组中的指定元素改为 # ,当作电脑下的棋
break;
}
}
}
int isfull(char chessboard[LINE][COLUME], int line, int colume)//判断是否平局的函数
{
int i = 0;
int j = 0;
for (i = 0; i < LINE; i++)
{
for (j = 0; j < COLUME; j++)
{
if (chessboard[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char IsWin(char chessboard[LINE][COLUME], int line, int colume)//判断输赢,一共有玩家赢,电脑赢,平局,棋没下完继续下 四种情况
{
int i = 0;
int j = 0;
for (i = 0; i < LINE; i++)
{
if (chessboard[i][0] == chessboard[i][1] && chessboard[i][1] == chessboard[i][2] && chessboard[i][1] != ' ')
{
return chessboard[i][1];//返回 * 时玩家赢,返回 # 时电脑赢
}
}
for (j = 0; j < LINE; j++)
{
if (chessboard[0][j] == chessboard[1][j] && chessboard[1][j] == chessboard[2][j] && chessboard[1][j] != ' ')
{
return chessboard[j][1];//返回 * 时玩家赢,返回 # 时电脑赢
}
}
if (chessboard[0][0] == chessboard[1][1] && chessboard[1][1] == chessboard[2][2] && chessboard[1][1] != ' ')
{
return chessboard[1][1];//返回 * 时玩家赢,返回 # 时电脑赢
}
if (chessboard[0][2] == chessboard[1][1] && chessboard[1][1] == chessboard[2][0] && chessboard[1][1] != ' ')
{
return chessboard[1][1];//返回 * 时玩家赢,返回 # 时电脑赢
}
if (isfull(chessboard, LINE, COLUME))
{
return 'q';//返回 q 代表平局
}
return 'c';//返回 c 代表棋没下完继续下
}