![](https://i-blog.csdnimg.cn/blog_migrate/c73ad6cace4387314ad8b8bf3fe40b6e.jpeg)
【项目介绍】:使用C语言中二维数组和函数的基本知识简单实现一个三子棋游戏,这个游戏要实现的基本功能主要有初始化棋盘、棋盘的打印、玩家下棋、电脑下棋、判断输赢(三字连珠)。本项目非常适合C语言初学者,希望有兴趣的朋友可以一起学习。
【代码框架】:
①头文件(game.h)
#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include"game.h"
#include<stdlib.h>
#include<time.h>
#include<string.h>
#pragma warning(disable:4996)
typedef unsigned int uint;
enum OPTION
{
EXIT,
START
};
#define ROW 3
#define COL 3
void menu();
void init_board(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
void paiyer_move(char board[ROW][COL], int row, int col);
int is_full(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
#endif //_GAME_H_
②测试文件(test.c)
#include"game.h"
void menu()
{
printf("***** 《三子棋游戏》*****\n");
printf("*************************\n");
printf("****** 1.play *******\n");
printf("****** 0.exit *******\n");
printf("*************************\n");
printf("*************************\n");
}
void game()
{
char ret;
char board[ROW][COL];
init_board(board, ROW, COL);
print_board(board, ROW, COL);
while (1)
{
printf("玩家走:\n");
paiyer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != ' ')
break;
printf("电脑走:\n");
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != ' ')
break;
}
if (ret == '#')
{
printf("恭喜你赢了!\n");
}
else if (ret == '*')
{
printf("很遗憾,你输了\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((uint)time(NULL));
do {
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case START:
game();
break;
case EXIT:
break;
default:
break;
}
} while (input);
return 0;
}
【函数实现】:
①初始化棋盘
void init_board(char board[ROW][COL], int row, int col)
{
memset(board, ' ', sizeof(board[0][0] )* row*col);
}
//memset函数用来对一段内存空间全部设置为某个字符
不知道memset函数作用的朋友请戳——>
memset
②打印棋盘
void print_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if (i != row-1)
{
printf("---|---|---\n");
}
}
}
打印效果图:
![](https://i-blog.csdnimg.cn/blog_migrate/6053e726623d19d0109be72c89109415.png)
③玩家下棋
void paiyer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("请输入落子的坐标:\n");
while (1)
{
scanf("%d%d", &x, &y);
//保证输入坐标的合法性,玩家输入的坐标是从1开始的
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 computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;//利用rand函数生成随机坐标
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
}
}
不知道rand函数作用的朋友请戳——>rand
⑤判断输赢
static int is_full(char board[ROW][COL], int row, int col)
//函数is_full只在is_win中被调用,可以加上static改变其链接属性
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}//判断棋盘是否下满了
char is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][2] != ' '))
{
return board[i][1];
}
}
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j]) && (board[1][j] == board[2][j]) && (board[2][j] != ' '))
{
return board[1][j];
}
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[2][2] != ' '))
{
return board[1][1];
}
else if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))
{
return board[1][1];
}
else if (is_full(board,ROW,COL) == 1)
{
return 'Q';//表示棋盘下满了
}
else
return ' ';
}
【结果实例】:
![](https://i-blog.csdnimg.cn/blog_migrate/22fe7658b7f4c4cbc32ef8736f25d9e3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b6bbb62ab46f12103762380b0f7d2060.png)
完整代码请移步——>
三子棋源码。欢迎转载,转载请声明出处。