分为三个文件,代码都放在下面,大家可以直接下载运行,注释也都很丰富,下面是仓库,可直接下载文件,欢迎讨论。
wh yang/C-train - 码云 - 开源中国 (gitee.com)
game.c 定义了一些必要的函数
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
#include<stdio.h>
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0;j < col; j++)
{
board[i][j] = ' ';
}
}
}
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
//打印数据
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < row-1)
{
printf("|");
}
}
printf("\n");
//打印分隔符
if (i < row-1)
{
for (int j = 0; j < col; j++)
{
printf("---");
if (j<col-1)
{
printf("|");
}
}
printf("\n");
}
}
}
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:->\n");
while (1)
{
printf("请输入坐标:->");
scanf("%d %d", &x, &y);
//坐标合法性判断
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用,不能在此下棋,请选择其他位置\n");
}
}
else
{
printf("坐标非法\n");
}
}
}
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:->\n");
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % row;
if (board[x][y]==' ')
{
board[x][y] = '#';
break;
}
}
}
//判满
int IsFull(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j]==' ')
{
return 0;
break;
}
}
}
return 1;
}
//判断输赢
char IsWin(char board[ROW][COL], int row, int col)
{
//行
for (int i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
return board[i][1];
}
//列
for (int j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
return board[1][j];
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
//判别平局
if (IsFull(board,row,col))
{
return 'Q';
}
//游戏继续
return 'C';
}
game.h 定义了所需要的头文件及函数引用
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判断输赢
char IsWin(char board[ROW][COL], int row, int col);
test.c 是整个三子棋游戏逻辑实现的代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
void menu(input)
{
printf("**********************************************\n");
printf("**********************************************\n");
printf("**************1.play 0.exit****************\n");
printf("**********************************************\n");
printf("**********************************************\n");
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
//下棋
while (1)
{
PlayerMove(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret!='C')
{
break;
}
DisplayBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
}
if (ret=='*')
{
printf("\n玩家赢了\n");
}
else if (ret=='#')
{
printf("\n电脑赢了\n");
}
else
{
printf("\n平局\n");
}
DisplayBoard(board, ROW, COL);
}
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("退出");
break;
default:
printf("选择错误");
break;
}
} while (input);
return 0;
}