c语言三子棋游戏代码

代码复现


学了一周多c语言了 现在遇到三子棋 2小时的视频 看了2遍在加代码复现。
下边有3部分代码

test.c 调用函数。


#include "game.h"
void menu()
{
	printf("*********************\n");
	printf("****1.play 2.exzit***\n");
	printf("*********************\n");
}
//游戏整个算法实现
void game()
{
	char ret = 0;
	char board[ROW][COL];
	//初始化棋盘
	initBoard(board, ROW, COL);
	//打印棋盘
	displayBoard(board,ROW,COL); 
	while (1)
	{
		//玩家下棋
		playermove(board,ROW,COL);
		displayBoard(board, ROW, COL);
		//判断输赢
		ret=iswin(board,ROW,COL);
		if (ret != 'c')
		{
			break;
		}
		//电脑下棋
		computermove(board, ROW, COL);
		displayBoard(board, ROW, COL);
		ret=iswin(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_s("%d", &input);
		switch (input)
		{
		case 1:
			printf("进入游戏\n");
			game();
			break;
		case 2:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
}
int main()
{
	test();



	return 0;
}

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++)
	{	
		//1.打印数据
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if(j < col - 1)
				printf("|");
		}
		printf("\n");
		//2.打印分割线
		if (i < row-1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}
void playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("请输入坐标\n");
		scanf_s("%d%d", &x, &y);
		//判断xy坐标是否符合
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("坐标被占用"); 
			}
		}
		else
		{
			printf("坐标不合适,重新输入\n");
		}
	}
}
void computermove(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 isfull(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 iswin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
		for (i = 0; i < row; i++)
		{
			//横3行
			if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
			{
				return board[i][0];
			}
		}
		for (j = 0; j < col; j++)
		{
			//竖三列
			if (board[0][j] == board[1][j] && board[0][j] == board[2][j] && board[0][j] != ' ')
			{
				return board[0][j];
			}

		}
		//斜着
		if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
		{
			return board[0][0];
		}
		if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != ' ')
		{
			return board[2][0];
		}
		//判断平局
		if (1 == isfull(board, row, col))
		{
			return 'q';
		}
		else
		{
			return 'c';
		}


}

game.h里边主要声明函数。

#define ROW  3//#define的作用就是定义符号的,在这里定义row这个符号
#define COL  3
#include
#include
#include
//函数声明
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);
int isfull(char board[ROW][COL], int row, int col);
char iswin(char board[ROW][COL],int row,int col);

你可能感兴趣的:(C语言,开发语言,算法,c++,c语言)