C语言实现三子棋小游戏

首先来梳理一下实现三子棋的代码分区:
为了使代码干净整洁,结构完整,可读性及可移植性增强,我们将整个代码分为三个板块,分别是game.h(用于存放代码需要的头文件及各个实现游戏功能的函数声明),game.c(这里存放的是游戏实现过程中函数的定义),test.c(这里主要是与用户的交互实现)
其次,就是实现游戏的具体逻辑:
①棋类游戏,首先需要一个棋盘,可以使用一些电脑键盘自带的分隔符以及空格来实现,实现代码如下:

//初始化棋盘
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;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		//数据
		for (j = 0; j < col; 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)
{
	printf("玩家下棋->\n");
	int x = 0;
	int y = 0;
	
	while (1)
	{
		
		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 computer_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋->\n");
	while (1)
	{
		x = rand() % ROW;
		y = rand() % COL;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}


}

③最后就是游戏赢家的判断,通过判断二维数组的行,列,对角线是否相等来判断输赢

//判断赢家
char is_win(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	int P = 0;
	int C = 0;
	int win = 0;
	while (i <= row)//判断行
	{
		
		while (j < (col - 1))
		{
			if (board[i][j] == board[i][j + 1] && board[i][j] == '*')
				P++;
			if (board[i][j] == board[i][j + 1] && board[i][j] == '#')
				C++;
			if (P == (row - 1))
				return '*';
			else if (C == (row - 1))
				return '#';
			j++;
		}
		i++;
	}
	
	while (j <= row)//判断列
	{
		P = 0;
		C = 0;
		i = 0;
		while (i < (col - 1))
		{
			if (board[i][j] == board[i + 1][j] && board[i][j] == '*')
				P++;
			if (board[i][j] == board[i + 1][j] && board[i][j] == '#')
				C++;
			if (P == (row - 1))
				return '*';
			else if (C == (row - 1))
				return '#';
			i++;
		}
		j++;
	}
	P = 0;
	C = 0;
	
	for (i = 0; i < row; i++)//主对角线
	{
		if (board[i][i] == board[i + 1][i + 1] && board[i][i] == '*')
			P++;
		if (board[i][i] == board[i + 1][i + 1] && board[i][i] == '#')
			C++;
		if (P == (row - 1))
			return '*';
		else if (C == (row - 1))
			return '#';
	}
	P = 0;
	C = 0;
	for (i = 0, j = (col - 1); i < row, j > 0; i++, j--)//副对角线
	{
		if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '*')
			P++;
		if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '#')
			C++;
		if (P == (row - 1))
			return '*';
		else if (C == (row - 1))
			return '#';
	}


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


上面这些就是实现这样一个三子棋游戏最主要的代码逻辑,最后附上完整代码。注意是在源文件中还是头文件中哦!
game1.h

#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(int board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(int 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);

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;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		//数据
		for (j = 0; j < col; 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)
{
	printf("玩家下棋->\n");
	int x = 0;
	int y = 0;
	
	while (1)
	{
		
		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 computer_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋->\n");
	while (1)
	{
		x = rand() % ROW;
		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;
	int P = 0;
	int C = 0;
	int win = 0;
	while (i <= row)//判断行
	{
		
		while (j < (col - 1))
		{
			if (board[i][j] == board[i][j + 1] && board[i][j] == '*')
				P++;
			if (board[i][j] == board[i][j + 1] && board[i][j] == '#')
				C++;
			if (P == (row - 1))
				return '*';
			else if (C == (row - 1))
				return '#';
			j++;
		}
		i++;
	}
	
	while (j <= row)//判断列
	{
		P = 0;
		C = 0;
		i = 0;
		while (i < (col - 1))
		{
			if (board[i][j] == board[i + 1][j] && board[i][j] == '*')
				P++;
			if (board[i][j] == board[i + 1][j] && board[i][j] == '#')
				C++;
			if (P == (row - 1))
				return '*';
			else if (C == (row - 1))
				return '#';
			i++;
		}
		j++;
	}
	P = 0;
	C = 0;
	
	for (i = 0; i < row; i++)//主对角线
	{
		if (board[i][i] == board[i + 1][i + 1] && board[i][i] == '*')
			P++;
		if (board[i][i] == board[i + 1][i + 1] && board[i][i] == '#')
			C++;
		if (P == (row - 1))
			return '*';
		else if (C == (row - 1))
			return '#';
	}
	P = 0;
	C = 0;
	for (i = 0, j = (col - 1); i < row, j > 0; i++, j--)//副对角线
	{
		if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '*')
			P++;
		if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '#')
			C++;
		if (P == (row - 1))
			return '*';
		else if (C == (row - 1))
			return '#';
	}


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

test.c

#include 
#include "game.h"


void menu()
{
	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;
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢\n");
	}
	else
	{
		printf("平局\n");
	}
}
void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择->");
		scanf("%d", &input);
		switch (input)
		{case 1:
			game();
			break;
		case 2:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;

		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

对了,修改头文件中的宏定义可以实现N子棋哦!

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