C语言实现三子棋【人工智障版】

编程环境:【Visual Studio 2019】

编程准备

由于代码量较多,所以采用的是分文件,显得代码不那么杂乱。想写人工智能但是实力不允许,只能写个人工智障版的 |ू・ω・` )。

game.h

放头文件,函数声明和全局常量

#pragma once
#include
#include
#include
#include

//方便对棋盘大小的修改
#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 player_move(char board[ROW][COL],int row,int col);

//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);

//判断输赢
char is_win(char board[ROW][COL], int row, int col);

game.c

放函数功能的实现。

#include"game.h"


void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0;i < row;i++)
	{
		for (j = 0;j < col;j++)
		{
			//每行初始化空格
			board[i][j] = ' ';
		}
	}
}

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0;i < row;i++)
	{
		int j = 0;
		//数据行
		for (j = 0;j < row;j++)
		{
			printf(" %c ",board[i][j]);
			if (j < col - 1)
			{
				//最后一列不打印
				printf("|");
			}
		}
		printf("\n");

		//打印分割线
		if (i < row - 1)//判断最后一行不打印
		{
			for (j = 0;j < col;j++)
			{
				printf("---");
				if (j < col - 1)
				{
					//最后一列不打印
					printf("|");
				}
			}
		}
		printf("\n");
	}
}

void player_move(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 && y >= 1 && x <= row && 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)
{
	printf("电脑下棋\n");
	while (1)
	{
		int x = rand() % ROW;
		int y = rand() % COL;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

int is_full(char board[ROW][COL],int row,int col)
{
	int i = 0;
	int j = 0;
	for (i = 0;i < row;i++)
	{
		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][1] != ' ')
//		{
//			return board[i][1];
//		}
//	}
//
//	//判断列
//	for (i = 0;i < row;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[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
//	{
//		return board[1][1];
//	}
//
//
//	if (1 == is_full(board, row, col))
//	{
//		//平局
//		return 'Q';
//	}
//	//继续
//	return 'C';
//}


//优化版判定
char is_win(char board[ROW][COL], int row, int col)
{
	int flag = 0;
	int i = 0;
	int j = 0;
	//行判断
	for (i = 0;i < row;i++)
	{
		for (j = 0;j < col-1;j++)
		{
			if (board[i][j] == board[i][j + 1] && board[i][j] != ' ')
			{
				flag = 1;
			}
			else
			{
				flag = 0;
				break;
			}
		}
		if (flag == 1)
		{
			return board[i][i];
		}
	}

	//列判断
	for (j = 0;j < col;j++)
	{
		for (i = 0;i < row - 1;i++)
		{
			if (board[i][j] == board[i + 1][j] && board[i][j] != ' ')
			{
				flag = 1;
			}
			else
			{
				flag = 0;
				break;
			}
		}
		if (flag == 1)
		{
			return board[i][j];
		}
	}

	//对角线
	for (i = 0, j = 0;i < row - 1 && j < col - 1;i++,j++)
	{
		if (board[i][i] == board[i + 1][i + 1] && board[i][i] != ' ')
		{
			flag = 1;
		}
		else
		{
			flag = 0;
			break;
		}
	}
	if (flag == 1)
	{
		return board[i][i];
	}

	//j 等于数组一行的最后一个元素下标
	for (i = 0, j = col - 1;i < row - 1&& j > 0;i++, j--)
	{
		if (board[i][j] == board[i + 1][j - 1] && board[i][j]!=' ')
		{
			flag = 1;
		}
		else
		{
			flag = 0;
			break;
		}
	}
	if (flag == 1)
	{
		return board[i][j];
	}

	if (1 == is_full(board, row, col))
	{
	//平局
		return 'Q';
	}
	//继续
	return 'C';
}

main.c

放主函数、打印菜单和游戏函数的调用、判定输赢

#include"game.h"

void meun()
{
	printf("************************************\n");
	printf("********      1. play       ********\n");
	printf("********      0. exit       ********\n");
	printf("************************************\n");
}

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	char ret = 0;
	while (1)
	{
		//玩家下棋
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}

		//电脑下棋
		computer_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			 break;
		}
		system("cls");
		DisplayBoard(board, ROW, COL);

	}
	if (ret == '*')
	{
		printf("玩家胜利!\n");
	}
	else if (ret == '#')
	{
		printf("电脑胜利!\n");
	}
	else
	{
		printf("平局\n");
	}
	system("pause");
	system("cls");//清屏
}

void test()
{
	int input = 0;
	//随机数
	srand((unsigned int)time(NULL));
	do
	{
		meun();//菜单
		printf("请输入你的选择:");
		scanf("%d", &input);
		system("cls");
		switch (input)
		{
		case 1:
			printf("三子棋\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);//输入0为假,循环结束
}
int main()
{
	test();

	return 0;
}

演示效果


C语言实现三子棋【人工智障版】_第1张图片

你可能感兴趣的:(C语言,c语言)