用C语言实现简单的三子棋游戏!!!

简单三子棋游戏

在此篇博客,博主介绍的是一个关于C语言中简单的小游戏——三子棋,代码就在如下,写的比较简单!大家仅供参考!!!

Demo:

#define  _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

#define ROW 3
#define COL 3


#define  _CRT_SECURE_NO_WARNINGS
#include "game.h"


//初始化棋盘
void InitChessBoard(char chessboard[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			chessboard[i][j] = ' ';
		}
	}
}

/*
    //打印棋盘版本1.0(比较low)
void DisplayBorad(char chessboard[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//打印数据
		printf(" %c | %c | %c \n", chessboard[i][0], chessboard[i][1], chessboard[i][2]);
		//打印分隔符
		if (i < row - 1)
		{
			printf("---|---|---\n");
		}
	}
}
*/

//打印棋盘2.0版本
void DisplayBorad(char chessboard[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//打印数据
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", chessboard[i][j]);
			if (j < col - 1)
				{
					printf("|");
				}
			
		}
		printf("\n");
		//打印分隔符
		if (i < row - 1)
		{
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

//玩家下棋
void PlayerMove(char chessboard[ROW][COL], int row, int col)
{
	//坐标
	int x = 0;
	int y = 0;
	printf("玩家下棋:\n");
	while (1)
	{
		printf("请输入坐标:>\n");
		scanf("%d %d", &x, &y);
		//坐标范围合法的判断,左上角的坐标:(1,1)
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (chessboard[x - 1][y - 1] == ' ')
			{
				chessboard[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("此处已经有棋子,不能下棋,请重新选择!\n");
			}
		}
		else
		{
			printf("坐标非法,请重新输入:\n");
		}
	}
}

//电脑下棋
void ComputerMove(char chessboard[ROW][COL], int row, int col)
{
	//设置随机数的生成起点
	srand((unsigned int)time(NULL));

	printf("电脑下棋:\n");

	int x = 0;
	int y = 0;

	while (1)
	{
		x = rand() % row;//0~2
		y = rand() % col;//0~2

		if (chessboard[x][y] == ' ')
		{
			chessboard[x][y] = '#';
			break;
		}
	}
}

//判断输赢
/*
ret = * 说明玩家赢了
ret = # 说明电脑赢了
ret = Q 说明平局
ret = C 说明游戏继续
*/
char Is_Win(char chessboard[ROW][COL], int row, int col)
{
	//行相等
	int i = 0;
	for (i = 0; i < row; i++)
	{
		if (chessboard[i][0] == chessboard[i][1] && chessboard[i][1] == chessboard[i][2] && chessboard[i][0] != ' ')
		{
			return chessboard[i][0];
		}
	}

	//列相等
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if (chessboard[1][j] == chessboard[2][j] && chessboard[0][j] == chessboard[1][j] && chessboard[0][j] != ' ')
		{
			return chessboard[0][j];
		}
	}

	//对角线相等
	if (chessboard[0][0] == chessboard[1][1] && chessboard[1][1] == chessboard[2][2] && chessboard[0][0] != ' ')
	{
		return chessboard[0][0];
	}
	if (chessboard[0][2] == chessboard[1][1] && chessboard[1][1] == chessboard[2][0] && chessboard[0][2] != ' ')
	{
		return chessboard[0][2]; 
	}

	//当走到这里,说明没有谁赢,就要判断棋盘是否是满的,即为平局
	if (Is_Full(chessboard,row,col))
	{
		return 'Q';
	}

	return 'C';
}

//判断棋盘是否是满的
//是满的就返回1,否则返回0
int Is_Full(char chessboard[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	for (x = 0; x < row; x++)
	{
		for (y = 0; y < col; y++)
		{
			if (chessboard[x][y] == ' ')
			{
				return 0;
			}
		}
	}
	return 1;
}

void menu()//菜单
{
	printf("**********************************\n");
	printf("*****----欢迎来到三子棋游戏----*****\n");
	printf("******   1.play    0.exit   ******\n");
	printf("**********************************\n");
}



void StartGame()//开始游戏
{
	char ret = 0;
	char chessboard[ROW][COL] = { 0 };
	InitChessBoard(chessboard,ROW,COL);//初始化棋盘
	DisplayBorad(chessboard, ROW, COL);//打印棋盘

	//下棋
	while (1)
	{
		//玩家下棋
		PlayerMove(chessboard,ROW,COL);
		//判断输赢
		ret = Is_Win(chessboard,ROW,COL);
		if (ret != 'C')
		{
			break;
		}

		DisplayBorad(chessboard, ROW, COL);//打印棋盘
		//电脑下棋
		ComputerMove(chessboard,ROW,COL);
		//判断输赢
		ret = Is_Win(chessboard, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		DisplayBorad(chessboard, ROW, COL);//打印棋盘
	}
	if (ret == '*')
	{
		printf("恭喜玩家胜利!\n");
	}
	else if (ret == '#')
	{
		printf("恭喜电脑胜利!\n");
	}
	else
	{
		printf("平局!\n");
	}
	DisplayBorad(chessboard, ROW, COL);//打印棋盘
}

int main()
{
	int selectNumber = 0;
	do
	{
		menu();//打印菜单
		printf("请选择:>\n");
		scanf("%d", &selectNumber);

		switch (selectNumber)
		{
			case 1:
			{
				StartGame();//开始游戏
				break;
			}
			case 0:
			{
				printf("退出游戏!\n");
				break;
			}
			default:
			{
				printf("选择错误!\n");
				break;
			}
		}
	} while (selectNumber!=0);

	
	return 0;
}

由于博主使用随机数操控电脑下棋,所以电脑赢的机率不大,感兴趣的小伙伴可以将电脑下棋变得更加聪明点!!!!

Youth means limitless possibilities.

你可能感兴趣的:(C语言,c语言,游戏,开发语言)