C语言实现三子棋、五子棋、多子棋、以及智能算法三子棋源代码

  勤能补拙是良训,一分辛苦一分才。

  前面三个博客分别实现了简单的三子棋,五子棋,多子棋,以及算法优化后的AI三子棋。将所有的代码整理了下,方便浏览和学习。(AI五子棋对我来说有点难,暂时没有搞定,害,挖坑代填吧,等我能力到了再续)



简单的三子棋代码

//宏定义
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3

//引用库函数
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//函数声明
void menu(void);
void game(void);
void test(void);
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 Win(char board[ROW][COL], int row, int col);
char Full(char board[ROW][COL], int row, int col);
#include"game.h"

//主函数
int main(void)
{
    
    
	test();
	return 0;
}
//游戏框架测试
void test(void)
{
    
    
	int input = 0;
	do
	{
    
    
		menu();//调用菜单
		printf("\n");
		printf("系统提示:请选择:>>>");
		scanf("%d", &input);
		printf("\n");
		switch (input)
		{
    
    
		case 1:
			game();//调用游戏函数
			break;
		case 0:
			printf("破游戏,俺不玩了。\n");
			break;
		default:
			printf("电脑:FBI warning 非法输入,非法输入,请您自觉点。\n\n电脑:乖,咱重新输入!!!\n");
			break;
		}
	} while (input);
}

//游戏菜单
void menu(void)
{
    
    
	printf("****************************************\n");
	printf("*****1.开始游戏          0.退出游戏*****\n");
	printf("****************************************\n");
}

//游戏函数
void game(void)
{
    
    
	char ret = 0;
	char board[ROW][COL] = {
    
     0 };
	//初始化棋盘
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断玩家是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断电脑是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
	}

	if (ret == '*')
	{
    
    
		printf("恭喜你赢啦!!!(不过我是随机下的,我的算法没有任何技术含量(狗头狗头狗头))\n");
		printf("\n");
	}
	else if (ret == '#')
	{
    
    
		printf("你输了!!!你真笨!!!你是个憨憨吧!\n");
		printf("\n");
	}
	else
	{
    
    
		printf("平局!!!你真笨!!!略略略!\n");
		printf("\n");
	}
}
#include"game.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 < col; i++)
	{
    
    
		if (i == 0)
		{
    
    
			printf("  %d   ", i + 1);
		}
		else
		{
    
    
			printf("%d   ", i + 1);
		}
	}
	printf("\n");

	for (int i = 0; i < row; i++)
	{
    
    
		for (int j = 0; j < col; j++)
		{
    
    
			if (j == 0)
			{
    
    
				printf("%d %c |", i + 1, board[i][j]);
			}
			else if (j > 0 && j < col - 1)
			{
    
    
				printf(" %c |", board[i][j]);
			}
			else
				printf(" %c ", board[i][j]);
		}
		printf("\n");
		if (i < row - 1)
		{
    
    

			for (int k = 0; k < col; k++)
			{
    
    
				if (k == 0)
				{
    
    
					printf(" ---|");
				}
				else if (k > 0 && k < col - 1)
				{
    
    
					printf("---|");
				}
				else
				{
    
    
					printf("---");
				}
			}
		}
		printf("\n");
	}
}

//玩家输入
void PlayerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	while (1)
	{
    
    
		printf("系统提示:请输入您要下的坐标:>>>");

		scanf("%d%d", &x, &y);
		printf("\n");
		//判断xy是否合法,一个是是否溢出,一个是是否被占用
		if (x > 0 && x < row + 1 && y>0 && y < col + 1)
		{
    
    
			if (board[x - 1][y - 1] == ' ')
			{
    
    
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
    
    
				printf("电脑:这下过了,你个憨憨。\n");
				printf("\n");

			}
		}
		else
		{
    
    
			printf("电脑:FBI warning 非法输入,非法输入!!!请您自觉点。\n\n电脑:乖,咱重新输入!!!\n\n");
		}
	}
}

//电脑输入
void ComputerMove(char board[ROW][COL], int row, int col)
{
    
    
	srand((unsigned int)time(NULL));
	int x = 0;
	int y = 0;
	printf("电脑:我走过啦!!!到你啦!!!\n\n");
	while (1)
	{
    
    
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}
	}
}

//判断胜负
char Win(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	//判断行
	for (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 (i = 0; i < col; i++)
	{
    
    
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
    
    
			return board[1][i];
		}
	}
	//判断对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
    
    
		return board[1][1];
	}
	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
	{
    
    
		return board[1][1];
	}
	//判断是否平局
	if (1 == Full(board, ROW, COL))
	{
    
    
		return 'Q';
	}
	return 'C';
}
//平局算法函数
char Full(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;
		}
	}
	return 1;
}

简单的第一类五子棋代码

//宏定义
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 5
#define COL 5

//引用库函数
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//函数声明
void menu(void);
void game(void);
void test(void);
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 Win(char board[ROW][COL], int row, int col);
char Full(char board[ROW][COL], int row, int col);
#include"game.h"

//主函数
int main(void)
{
    
    
	test();
	return 0;
}

//游戏测试函数
void test(void)
{
    
    
	int input = 0;
	do
	{
    
    
		menu();
		printf("\n");
		printf("系统提示:请选择:>>>");
		scanf("%d", &input);
		printf("\n");
		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("破游戏,俺不玩了。\n");
			break;
		default:
			printf("电脑:FBI warning 非法输入,非法输入,请您自觉点。\n\n电脑:乖,咱重新输入!!!\n");
			break;
		}
	} while (input);
}

//游戏菜单
void menu(void)
{
    
    
	printf("****************************************\n");
	printf("*****1.开始游戏            0.退出游戏*****\n");
	printf("****************************************\n");
}

//游戏函数
void game(void)
{
    
    
	char ret = 0;
	char board[ROW][COL] = {
    
     0 };
	//初始化棋盘
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断玩家是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断电脑是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
	}

	if (ret == '*')
	{
    
    
		printf("恭喜你赢啦!!!(不过我是随机下的,我的算法没有任何技术含量(狗头狗头狗头))\n");
		printf("\n");
	}
	else if (ret == '#')
	{
    
    
		printf("你输了!!!你真笨!!!你是个憨憨吧!\n");
		printf("\n");
	}
	else
	{
    
    
		printf("平局!!!你真笨!!!略略略!\n");
		printf("\n");
	}
}
#include"game.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 < col; i++)
	{
    
    
		if (i == 0)
		{
    
    
			printf("  %d   ", i + 1);
		}
		else
		{
    
    
			printf("%d   ", i + 1);
		}
	}
	printf("\n");

	for (int i = 0; i < row; i++)
	{
    
    
		for (int j = 0; j < col; j++)
		{
    
    
			if (j == 0)
			{
    
    
				printf("%d %c |", i + 1, board[i][j]);
			}
			else if (j > 0 && j < col - 1)
			{
    
    
				printf(" %c |", board[i][j]);
			}
			else
				printf(" %c ", board[i][j]);
		}
		printf("\n");
		if (i < row - 1)
		{
    
    

			for (int k = 0; k < col; k++)
			{
    
    
				if (k == 0)
				{
    
    
					printf(" ---|");
				}
				else if (k > 0 && k < col - 1)
				{
    
    
					printf("---|");
				}
				else
				{
    
    
					printf("---");
				}
			}
		}
		printf("\n");
	}
}

//玩家输入
void PlayerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	while (1)
	{
    
    
		printf("系统提示:请输入您要下的坐标:>>>");

		scanf("%d%d", &x, &y);
		printf("\n");
		//判断xy是否合法,一个是是否溢出,一个是是否被占用
		if (x > 0 && x < row + 1 && y>0 && y < col + 1)
		{
    
    
			if (board[x - 1][y - 1] == ' ')
			{
    
    
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
    
    
				printf("电脑:这下过了,你个憨憨。\n");
				printf("\n");

			}
		}
		else
		{
    
    
			printf("电脑:FBI warning 非法输入,非法输入!!!请您自觉点。\n\n电脑:乖,咱重新输入!!!\n\n");
		}
	}
}

//电脑输入
void ComputerMove(char board[ROW][COL], int row, int col)
{
    
    
	srand((unsigned int)time(NULL));
	int x = 0;
	int y = 0;
	printf("电脑:我走过啦!!!到你啦!!!\n\n");
	while (1)
	{
    
    
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}
	}
}

//判断胜负
char Win(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	//判断行
	for (i = 0; i < row; i++)
	{
    
    
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] == board[i][3] && board[i][3] == board[i][4] && board[i][1] != ' ')//行元素一样且不是空格
		{
    
    
			return board[i][1];
		}
	}
	//判断列
	for (i = 0; i < col; i++)
	{
    
    
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] == board[3][i] && board[3][i] == board[4][i] && board[1][i] != ' ')//列元素一样且不是空格
		{
    
    
			return board[1][i];
		}
	}
	//判断对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[1][1] != ' ')//对角线元素一样且不是空格
	{
    
    
		return board[1][1];
	}
	if (board[4][0] == board[3][1] && board[3][1] == board[2][2] && board[2][2] == board[1][3] && board[1][3] == board[0][4] && board[2][2] != ' ')
	{
    
    
		return board[1][1];
	}
	//判断是否平局
	if (1 == Full(board, ROW, COL))//平局判断,函数在下面,已在头文件中声明,位置就无所谓了
	{
    
    
		return 'Q';
	}
	return 'C';
}

//平局算法函数
char Full(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;
		}
	}
	return 1;
}

简单第二类五子棋代码/多子棋

//宏定义
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 15
#define COL 15
#define CHESS 5//这里改成想要的获胜棋子就是多子棋

//引用库函数
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//函数声明
void menu(void);
void game(void);
void test(void);
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 Win(char board[ROW][COL], int row, int col);
char Full(char board[ROW][COL], int row, int col);
#include"game.h"

//主函数
int main(void)
{
    
    
	test();
	return 0;
}

//游戏测试函数
void test(void)
{
    
    
	int input = 0;
	do
	{
    
    
		menu();
		printf("\n");
		printf("系统提示:请选择:>>>");
		scanf("%d", &input);
		printf("\n");
		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("破游戏,俺不玩了。\n");
			break;
		default:
			printf("电脑:FBI warning 非法输入,非法输入,请您自觉点。\n\n电脑:乖,咱重新输入!!!\n");
			break;
		}
	} while (input);
}

//游戏菜单
void menu(void)
{
    
    
	printf("****************************************\n");
	printf("*****1.开始游戏            0.退出游戏*****\n");
	printf("****************************************\n");
}

//游戏主函数
void game(void)
{
    
    
	char ret = 0;
	char board[ROW][COL] = {
    
     0 };
	//初始化棋盘
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断玩家是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断电脑是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
	}

	if (ret == '*')
	{
    
    
		printf("恭喜你赢啦!!!(不过我是随机下的,我的算法没有任何技术含量(狗头狗头狗头))\n");
		printf("\n");
	}
	else if (ret == '#')
	{
    
    
		printf("你输了!!!你真笨!!!你是个憨憨吧!\n");
		printf("\n");
	}
	else
	{
    
    
		printf("平局!!!你真笨!!!略略略!\n");
		printf("\n");
	}
}
#include"game.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 < col; i++)
	{
    
    
		if (i == 0)
		{
    
    
			printf("  %02d  ", i + 1);
		}
		else
		{
    
    
			printf("%02d  ", i + 1);
		}
	}
	printf("\n");

	for (int i = 0; i < row; i++)
	{
    
    
		for (int j = 0; j < col; j++)
		{
    
    
			if (j == 0)
			{
    
    
				printf("%02d %c |", i + 1, board[i][j]);
			}
			else if (j > 0 && j < col - 1)
			{
    
    
				printf(" %c |", board[i][j]);
			}
			else
				printf(" %c ", board[i][j]);
		}
		printf("\n");
		if (i < row - 1)
		{
    
    

			for (int k = 0; k < col; k++)
			{
    
    
				if (k == 0)
				{
    
    
					printf("  ---|");
				}
				else if (k > 0 && k < col - 1)
				{
    
    
					printf("---|");
				}
				else
				{
    
    
					printf("---");
				}
			}
		}
		printf("\n");
	}
}

//玩家输入
void PlayerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	while (1)
	{
    
    
		printf("系统提示:请输入您要下的坐标:>>>");

		scanf("%d%d", &x, &y);
		printf("\n");
		//判断xy是否合法,一个是是否溢出,一个是是否被占用
		if (x > 0 && x < row + 1 && y>0 && y < col + 1)
		{
    
    
			if (board[x - 1][y - 1] == ' ')
			{
    
    
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
    
    
				printf("电脑:这下过了,你个憨憨。\n");
				printf("\n");

			}
		}
		else
		{
    
    
			printf("电脑:FBI warning 非法输入,非法输入!!!请您自觉点。\n\n电脑:乖,咱重新输入!!!\n\n");
		}
	}
}

//电脑输入
void ComputerMove(char board[ROW][COL], int row, int col)
{
    
    
	srand((unsigned int)time(NULL));
	int x = 0;
	int y = 0;
	printf("电脑:我走过啦!!!到你啦!!!\n\n");
	while (1)
	{
    
    
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}
	}
}

char Win(char board[ROW][COL], int row, int col)//五子棋
{
    
    
	int x = 0;
	int y = 0;
	int i = 0;
	for (x = 0; x < row; x++)
	{
    
    
		for (y = 0; y < col; y++)
		{
    
    
			//纵向
			for (i = 0; i < CHESS - 1; i++)
			{
    
    
				if (board[x][y] == board[x][y + i] && board[x][y] != ' ')
				{
    
    
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x][y + i] && board[x][y] != ' ')
			{
    
    
				return board[x][y];
			}

			//横向
			for (i = 0; i < CHESS - 1; i++)
			{
    
    
				if (board[x][y] == board[x + i][y] && board[x][y] != ' ')
				{
    
    
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x + i][y] && board[x][y] != ' ')
			{
    
    
				return board[x][y];
			}

			//捺 斜向
			for (i = 0; i < CHESS - 1; i++)
			{
    
    
				if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ')
				{
    
    
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x + i][y + i] && board[x][y] != ' ')
			{
    
    
				return board[x][y];
			}

			//撇 斜向
			for (i = 0; i < CHESS - 1; i++)
			{
    
    
				if (board[x][y] == board[x - i][y + i] && board[x][y] != ' ')
				{
    
    
					continue;
				}
				else
					break;
			}
			if (board[x][y] == board[x - i][y + i] && board[x][y] != ' ')
			{
    
    
				return board[x][y];
			}
		}
	}

	if (Full(board, ROW, COL))
	{
    
    
		return 'Q';
	}
	return 'C';
}

//平局算法函数
char Full(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;
		}
	}
	return 1;
}

较难的三子棋代码

//宏定义
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3

//引用库函数
#include<stdio.h>
#include<stdlib.h>

//函数声明
void menu(void);
void game(void);
void test(void);
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 Win(char board[ROW][COL], int row, int col);
char Full(char board[ROW][COL], int row, int col);
char judge_player(char board[ROW][COL], int row, int col,int k);
int check_computer(char board[ROW][COL], int row, int col);
#include"game.h"

//主函数
int main(void)
{
    
    
	test();
	return 0;
}

//游戏测试函数
void test(void)
{
    
    
	int input = 0;
	do
	{
    
    
		menu();
		printf("\n");
		printf("系统提示:请选择:>>>");
		scanf("%d", &input);
		printf("\n");
		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("破游戏,俺不玩了。\n");
			break;
		default:
			printf("电脑:FBI warning 非法输入,非法输入,请您自觉点。\n\n电脑:乖,咱重新输入!!!\n");
			break;
		}
	} while (input);
}

//游戏菜单
void menu(void)
{
    
    
	printf("****************************************\n");
	printf("*****1.开始游戏          0.退出游戏*****\n");
	printf("****************************************\n");
}

//游戏主函数
void game(void)
{
    
    
	char ret = 0;
	char board[ROW][COL] = {
    
     0 };
	//初始化棋盘
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断玩家是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断电脑是否胜利
		ret = Win(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			break;
		}
	}

	if (ret == '*')
	{
    
    
		printf("恭喜你赢啦!!!哇哦,太棒了!没几个人能战胜我呢!)\n");
		printf("\n");
	}
	else if (ret == '#')
	{
    
    
		printf("你输了!!!emmm,不过没关系,毕竟你是人类哇!\n");
		printf("\n");
	}
	else
	{
    
    
		printf("平局!!!嗯嗯,你已经不错了,但还要加油哦!\n");
		printf("\n");
	}
}
#include"game.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 < col; i++)
	{
    
    
		if (i == 0)
		{
    
    
			printf("  %d   ", i + 1);
		}
		else 
		{
    
    
			printf("%d   ", i + 1);
		}
	}
	printf("\n");

	for ( int i = 0; i < row; i++)
	{
    
    
		for (int j = 0; j < col; j++)
		{
    
    
			if (j == 0)
			{
    
    
				printf("%d %c |",i+1,board[i][j]);
			}
			else if (j>0 && j < col - 1)
			{
    
    
				printf(" %c |", board[i][j]);
			}
			else
				printf(" %c ", board[i][j]);
		}
		printf("\n");
		if (i < row - 1)
		{
    
    

			for (int k = 0; k < col; k++)
			{
    
    
				if (k == 0)
				{
    
    
					printf(" ---|");
				}
				else if (k>0 && k < col - 1)
				{
    
    
					printf("---|");
				}
				else
				{
    
    
					printf("---");
				}
			}
		}
		printf("\n");
	}
}

//玩家输入
void PlayerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	while (1)
	{
    
    
		printf("系统提示:请输入您要下的坐标:>>>");

		scanf("%d%d", &x, &y);
		printf("\n");
		//判断xy是否合法,一个是是否溢出,一个是是否被占用
		if (x > 0 && x < row + 1 && y>0 && y < col + 1)
		{
    
    
			if (board[x - 1][y - 1] == ' ')
			{
    
    
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
    
    
				printf("电脑:这我下过了,你个憨憨。\n");
				printf("\n");

			}
		}
		else
		{
    
    
			printf("电脑:FBI warning 非法输入,非法输入!!!请您自觉点\n\n电脑:乖,咱重新输入!!!\n\n");
		}
	}
}

//电脑输入
void ComputerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	int z = 0;
	printf("电脑:我走过啦!!!到你啦!!!\n\n");

	z = check_computer(board, row, col);

	while (0 == z)
	{
    
    
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}
	}
}

//电脑判断自己是否会赢
int check_computer(char board[ROW][COL], int row, int col) 
{
    
    
	int i = 0;
	int j = 0;
	int k = 0;
	while (0 == k)
	{
    
    
		//判断电脑横行上是否会赢
		for (i = 0; i < row; i++)
		{
    
    
			if (board[i][0] == board[i][1] && board[i][1] == '#' && board[i][2] == ' ')
			{
    
    
				board[i][2] = '#';
				k = 1;
				break;
			}
			if (board[i][0] == board[i][2] && board[i][0] == '#' && board[i][1] == ' ')
			{
    
    
				board[i][1] = '#';
				k = 1;
				break;
			}

			if (board[i][1] == board[i][2] && board[i][1] == '#' && board[i][0] == ' ')
			{
    
    
				board[i][0] = '#';
				k = 1;
				break;
			}
		}
		if (k != 0)
			break;
		//判断电脑列上是否有机会赢
		for (j = 0; j < col; j++)
		{
    
    
			if (board[0][j] == board[1][j] && board[1][j] == '#' && board[2][j] == ' ')
			{
    
    
				board[2][j] = '#';
				k = 1;
				break;
			}

			if (board[0][j] == board[2][j]	&& board[2][j] == '#' && board[1][j] == ' ')
			{
    
    
				board[1][j] = '#';
				k = 1;
				break;
			}
			if (board[1][j] == board[2][j] && board[2][j] == '#' && board[0][j] == ' ')
			{
    
    
				board[0][j] = '#';
				k = 1;
				break;
			}
		}
		break;
	}
	//判断电脑在对角线上是否会赢,又加了一个while是为了让判断对角线的代码成块。
	while (0 == k)
	{
    
    
		if (board[0][0] == board[1][1] && board[1][1] == '#' && board[2][2] == ' ')
		{
    
    
			board[2][2] = '#';
			k = 1;
			break;
		}

		if (board[0][0] == board[2][2] && board[2][2] == '#' && board[1][1] == ' ')
		{
    
    
			board[1][1] = '#';
			k = 1;
			break;
		}

		if (board[1][1] == board[2][2] && board[1][1] == '#' && board[0][0] == ' ')
		{
    
    
			board[0][0] = '#';
			k = 1;
			break;
		}

		if (board[0][2] == board[1][1] && board[0][2] == '#' && board[2][0] == ' ')
		{
    
    
			board[2][0] = '#';
			k = 1;
			break;
		}

		if (board[0][2] == board[2][0] && board[2][0] == '#' && board[1][1] == ' ')
		{
    
    
			board[1][1] = '#';
			k = 1;
			break;
		}
		if (board[1][1] == board[2][0] && board[2][0] == '#' && board[0][2] == ' ')
		{
    
    
			board[0][2] = '#';
			k = 1;
			break;
		}
		break;
	}
	k = judge_player(board, row, col, k);
	return k;
}

//下面这个函数就是判断玩家是否有机会赢,如果能赢就堵住他
char judge_player(char board[ROW][COL], int row, int col, int k)
{
    
    
	int i = 0;
	int j = 0;
	while (0 == k)
	{
    
    
		//判断玩家在横行上是否会赢
		for (i = 0; i < row; i++)  
		{
    
    
			if (board[i][0] == board[i][1] && board[i][1] == '*' && board[i][2] == ' ')
			{
    
    
				board[i][2] = '#';
				k = 1;
				break;
			}

			if (board[i][0] == board[i][2] && board[i][0] == '*' && board[i][1] == ' ')
			{
    
    
				board[i][1] = '#';
				k = 1;
				break;
			}

			if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
			{
    
    
				board[i][0] = '#';
				k = 1;
				break;
			}
		}
		if (k != 0)
			break;

		//判断玩家在竖列上是否会赢
		for (j = 0; j < col; j++)  
		{
    
    
			if (board[0][j] == board[1][j] && board[1][j] == '*' && board[2][j] == ' ')
			{
    
    
				board[2][j] = '#';
				k = 1;
				break;
			}

			if (board[0][j] == board[2][j] && board[2][j] == '*' && board[1][j] == ' ')
			{
    
    
				board[1][j] = '#';
				k = 1;
				break;
			}

			if (board[1][j] == board[2][j] && board[2][j] == '*' && board[0][j] == ' ')
			{
    
    
				board[0][j] = '#';
				k = 1;
				break;
			}
		}
		break;
	}

	//判断玩家在对角线上是否会赢,又加了一个while是为了让判断对角线的代码成块。
	while (0 == k)  
	{
    
    
		if (board[0][0] == board[1][1] && board[1][1] == '*' &&  board[2][2] == ' ')
		{
    
    
			board[2][2] = '#';
			k = 1;
			break;
		}

		if (board[0][0] == board[2][2] && board[2][2] == '*' && board[1][1] == ' ')
		{
    
    
			board[1][1] = '#';
			k = 1;
			break;
		}

		if (board[1][1] == board[2][2] && board[1][1] == '*' && board[0][0] == ' ')
		{
    
    
			board[0][0] = '#';
			k = 1;
			break;
		}

		if (board[0][2] == board[1][1] && board[0][2] == '*' && board[2][0] == ' ')
		{
    
    
			board[2][0] = '#';
			k = 1;
			break;
		}

		if (board[0][2] == board[2][0] && board[2][0] == '*' && board[1][1] == ' ')
		{
    
    
			board[1][1] = '#';
			k = 1;
			break;
		}

		if (board[1][1] == board[2][0]&& board[2][0] == '*' && board[0][2] == ' ')
		{
    
    
			board[0][2] = '#';
			k = 1;
			break;
		}
		break;
	}
	return k;
}

//判断胜负
char Win(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	//判断行
	for (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 (i = 0; i < col; i++)
	{
    
    
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')//列元素一样且不是空格
		{
    
    
			return board[1][i];
		}
	}
	//判断对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')//对角线元素一样且不是空格
	{
    
    
		return board[1][1];
	}
	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
	{
    
    
		return board[1][1];
	}
	//判断是否平局
	if (1 == Full(board, ROW, COL))//平局判断,函数在下面,已在头文件中声明,位置就无所谓了
	{
    
    
		return 'Q';
	}
	return 'C';
}

//平局算法函数
char Full(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;
		}
	}
	return 1;
}

源代码已经测试过了,但不可避免还会存在bug,仅供参考。如有bug,欢迎各位大佬批评指正哈!

猜你喜欢

转载自blog.csdn.net/weixin_50915462/article/details/113802405