C语言学习之路 -- 三子棋与五子棋

发布于:2022-11-09 ⋅ 阅读:(14) ⋅ 点赞:(0) ⋅ 评论:(0)

一、前言

  • 在系统学习完C语言中的一些内容后,想自己写出来一个类似于三子棋/五子棋的代码。
  • 在这个项目中,我们分为”test.c"“game.c”"game.h"三个文件

二、三子棋分函数讲解

1、主函数

  • 在主函数中,我们希望少放一些代码,因此只调用一个函数
int main()
{
	test();
	return 0;
}

2、menu()

  • 打印菜单
void menu()
{
	printf("**********************\n");
	printf("******1.play   *******\n");
	printf("******2.exit   *******\n");
	printf("**********************\n");
}

3、test()中

  • 初始化后面的随机值time()
  • 引入菜单
  • 让玩家输入选择进行游戏还是退出
void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	//游戏菜单
	do
	{
		menu();
	    printf("请输入(1/0): ");
	    scanf("%d", &input);
		printf("\n");
		switch (input)
		{
		case 1:
			//进入游戏
			game();
			break;
		case 0:
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	}while (input);
}

4、game()

  • 设置玩家输入的数组
  • 设置棋谱
  • 玩家下棋
  • 电脑下棋
  • 判断输赢
void game()
{
	//设置玩家输入,初始化
	char board[HAN][LIE];
	init_board(board, HAN, LIE);
	//设置棋谱
	print_board(board, HAN, LIE);
	char ret = 0;
	while (1)
	{
		//玩家下棋,输入*
	player_move(board,HAN,LIE);
	print_board(board, HAN, LIE);
	//判断
	ret = is_win(board, HAN, LIE);
	if (ret != 'C')
	{
		break;
	}
		//电脑下棋,输入#
	computerr_move(board, HAN, LIE);
	print_board(board, HAN, LIE);
	if (ret != 'C')
	{
		break;
	}
	}
	if (ret == '*')
		printf("玩家赢");
	else if (ret == '#')
		printf("电脑赢");
	else
		printf("平局");
}

5、init_board()

  • 初始化玩家输入
void init_board(char board[HAN][LIE], int han, int lie)
{
	int p = 0;
	for (p = 0; p <= han - 1; p++)
	{
		int q = 0;
		for (q = 0; q <= lie - 1; q++)
		{
			board[p][q] = ' ';
		 }
	}
}

6、print_board()

  • 打印棋盘
void print_board(char board[HAN][LIE], int han, int lie)
{
	int a = 0;
	for (a = 0; a <= han - 1; a++)
	{
		int b = 0;
		for (b = 0; b <= lie - 1; b++)
		{
			printf(" %c |",board[a][b]);
		}
		printf("\n");
		int c = 0;
		for (c = 0; c <= lie - 1;c++)
		{
			printf("---|");
		}
		printf("\n");
	}
}

7、player_move()

  • 玩家下棋
void player_move(char board[HAN][LIE], int han, int lie)
{   
	
	while(1)
	{
		printf("玩家下棋\n");
		int i = 0;
		int j = 0;
		printf("请输入想下的坐标,比如第一行第一列:1 1\n");
		printf("请输入:");
		scanf("%d %d", &i, &j);
		if (i >= 1 && i <= han && j >= 1 && j <= lie)
		{
			if (board[i - 1][j - 1] == ' ')
			{
				board[i - 1][j - 1] = '*';
				break;
			}
			else
			{
				printf("已填写过,请重新输入");
			}
		}
		else
		{
			printf("输入错误,请重新输入");
		}
	}
	count++;
}

8、computerr_move()

  • 电脑下棋
void computerr_move(char board[HAN][LIE], int han, int lie)
{
	printf("\n");
	printf("电脑下棋\n");
	int x = rand() % han;
	int y = rand() % lie;
	while (1)
	{
		if (board[x][y] == ' ');
		{
			board[x][y] = '#';
			break;
		}
	}
	count++;
}

9、is_win()

//判断输赢
//判断输赢的代码要告诉我:电脑赢了?玩家赢了?玩家赢?游戏继续?
//电脑赢:#
//玩家赢:*
//平局:Q
//游戏继续:C

//我希望is_full这个函数只是为了支持is_win函数的,只是在is_win函数内部使用
//那我们就没必要在头文件中声明
// 
//判断棋盘是否满了
static int is_full(char board[HAN][LIE], 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[HAN][LIE], 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][0] != ' ')
		{
			return board[i][0];
		}
	}
	//判断三列
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][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 (is_full(board, row, col) == 1)
	{
		return 'Q';
	}

	//继续
	//没有玩家或者电脑赢,也没有平局,游戏继续
	return 'C';
}

三、三子棋总代码

1、test.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//3子棋

void menu()
{
	printf("**********************\n");
	printf("******1.play   *******\n");
	printf("******2.exit   *******\n");
	printf("**********************\n");
}

void game()
{
	//设置玩家输入
	char board[HAN][LIE];
	init_board(board, HAN, LIE);
	//设置棋谱
	print_board(board, HAN, LIE);
	char ret = 0;
	while (1)
	{
		//玩家下棋,输入*
	player_move(board,HAN,LIE);
	print_board(board, HAN, LIE);
	//判断
	ret = is_win(board, HAN, LIE);
	if (ret != 'C')
	{
		break;
	}
		//电脑下棋,输入#
	computerr_move(board, HAN, LIE);
	print_board(board, HAN, LIE);
	if (ret != 'C')
	{
		break;
	}
	}
	if (ret == '*')
		printf("玩家赢");
	else if (ret == '#')
		printf("电脑赢");
	else
		printf("平局");
}

//判断输赢
//判断输赢的代码要告诉我:电脑赢了?玩家赢了?玩家赢?游戏继续?
//电脑赢:#
//玩家赢:*
//平局:Q
//游戏继续:C

void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	//游戏菜单
	do
	{
		menu();
	    printf("请输入(1/0): ");
	    scanf("%d", &input);
		printf("\n");
		switch (input)
		{
		case 1:
			//进入游戏
			game();
			break;
		case 0:
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	}while (input);
}

int count;
int main()
{
	test();
	return 0;
}

2、game.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void init_board(char board[HAN][LIE], int han, int lie)
{
	int p = 0;
	for (p = 0; p <= han - 1; p++)
	{
		int q = 0;
		for (q = 0; q <= lie - 1; q++)
		{
			board[p][q] = ' ';
		 }
	}
}

void print_board(char board[HAN][LIE], int han, int lie)
{
	int a = 0;
	for (a = 0; a <= han - 1; a++)
	{
		int b = 0;
		for (b = 0; b <= lie - 1; b++)
		{
			printf(" %c |",board[a][b]);
		}
		printf("\n");
		int c = 0;
		for (c = 0; c <= lie - 1;c++)
		{
			printf("---|");
		}
		printf("\n");
	}
}

void player_move(char board[HAN][LIE], int han, int lie)
{   
	
	while(1)
	{
		printf("玩家下棋\n");
		int i = 0;
		int j = 0;
		printf("请输入想下的坐标,比如第一行第一列:1 1\n");
		printf("请输入:");
		scanf("%d %d", &i, &j);
		if (i >= 1 && i <= han && j >= 1 && j <= lie)
		{
			if (board[i - 1][j - 1] == ' ')
			{
				board[i - 1][j - 1] = '*';
				break;
			}
			else
			{
				printf("已填写过,请重新输入");
			}
		}
		else
		{
			printf("输入错误,请重新输入");
		}
	}
	count++;
}

void computerr_move(char board[HAN][LIE], int han, int lie)
{
	printf("\n");
	printf("电脑下棋\n");
	int x = rand() % han;
	int y = rand() % lie;
	while (1)
	{
		if (board[x][y] == ' ');
		{
			board[x][y] = '#';
			break;
		}
	}
	count++;
}

//我希望is_full这个函数只是为了支持is_win函数的,只是在is_win函数内部使用
//那我们就没必要在头文件中声明
// 
//判断棋盘是否满了
static int is_full(char board[HAN][LIE], 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[HAN][LIE], 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][0] != ' ')
		{
			return board[i][0];
		}
	}
	//判断三列
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][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 (is_full(board, row, col) == 1)
	{
		return 'Q';
	}

	//继续
	//没有玩家或者电脑赢,也没有平局,游戏继续
	return 'C';
}

3、game.h

#define  _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define HAN 3
#define LIE 3

void init_board(char board[HAN][LIE], int han, int lie);

void print_board(char board[HAN][LIE], int han, int lie);

void player_move(char board[HAN][LIE], int han, int lie);

void computerr_move(char board[HAN][LIE], int han, int lie);

char is_win(char board[HAN][LIE], int , int );

int count;

4、运行结果

在这里插入图片描述

四、五子棋代码分享

1、test.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//五子棋

void menu()
{
	printf("**********************\n");
	printf("******1.play   *******\n");
	printf("******2.exit   *******\n");
	printf("**********************\n");
}

void game()
{
	//设置玩家输入
	char board[HAN][LIE];
	init_board(board, HAN, LIE);
	//设置棋谱
	print_board(board, HAN, LIE);
	char ret = 0;
	while (1)
	{
		//玩家下棋,输入*
	player_move(board,HAN,LIE);
	print_board(board, HAN, LIE);
	//判断
	ret = is_win(board, HAN, LIE);
	if (ret != 'C')
	{
		break;
	}
		//电脑下棋,输入#
	computerr_move(board, HAN, LIE);
	print_board(board, HAN, LIE);
	if (ret != 'C')
	{
		break;
	}
	}
	if (ret == '*')
		printf("玩家赢");
	else if (ret == '#')
		printf("电脑赢");
	else
		printf("平局");
}

//判断输赢
//判断输赢的代码要告诉我:电脑赢了?玩家赢了?玩家赢?游戏继续?
//电脑赢:#
//玩家赢:*
//平局:Q
//游戏继续:C

void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	//游戏菜单
	do
	{
		menu();
	    printf("请输入(1/0): ");
	    scanf("%d", &input);
		printf("\n");
		switch (input)
		{
		case 1:
			//进入游戏
			game();
			break;
		case 0:
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	}while (input);
}

int count;
int main()
{
	test();
	return 0;
}

2、game.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void init_board(char board[HAN][LIE], int han, int lie)
{
	int p = 0;
	for (p = 0; p <= han - 1; p++)
	{
		int q = 0;
		for (q = 0; q <= lie - 1; q++)
		{
			board[p][q] = ' ';
		 }
	}
}

void print_board(char board[HAN][LIE], int han, int lie)
{
	int a = 0;
	for (a = 0; a <= han - 1; a++)
	{
		int b = 0;
		for (b = 0; b <= lie - 1; b++)
		{
			printf(" %c |",board[a][b]);
		}
		printf("\n");
		int c = 0;
		for (c = 0; c <= lie - 1;c++)
		{
			printf("---|");
		}
		printf("\n");
	}
}

void player_move(char board[HAN][LIE], int han, int lie)
{   
	
	while(1)
	{
		printf("玩家下棋\n");
		int i = 0;
		int j = 0;
		printf("请输入想下的坐标,比如第一行第一列:1 1\n");
		printf("请输入:");
		scanf("%d %d", &i, &j);
		if (i >= 1 && i <= han && j >= 1 && j <= lie)
		{
			if (board[i - 1][j - 1] == ' ')
			{
				board[i - 1][j - 1] = '*';
				break;
			}
			else
			{
				printf("已填写过,请重新输入");
			}
		}
		else
		{
			printf("输入错误,请重新输入");
		}
	}
	count++;
}

void computerr_move(char board[HAN][LIE], int han, int lie)
{
	printf("\n");
	printf("电脑下棋\n");
	int x = rand() % han;
	int y = rand() % lie;
	while (1)
	{
		if (board[x][y] == ' ');
		{
			board[x][y] = '#';
			break;
		}
	}
	count++;
}



char is_win(char board[HAN][LIE], int han, int lie)
{
	int i = 0;
	int j = 0;
	//每行
	for(i=0;i<=han-1;i++)
	{
		for (j = 0; j <= (lie - 5 - 1); j++)
		{
			if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j + 2] == board[i][j + 3] && board[i][j + 3] == board[i][j + 4] && board[i][j] != ' ')
			{
				return board[i][j];
			}
		}
	}
	//每列
	for (j = 0; j <= lie - 1; j++);
	{
		for (i = 0; i <= han - 5 - 1; i++)
		{
			if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i + 2][j] == board[i + 3][j] && board[i + 3][j] == board[i + 4][j]&&board[i][j] != ' ')
			{
				return board[i][j];
			}
		}
	}
	//"\"
	for (i = 0; i <= han - 5 - 1; i++)
	{
		for (j = 0; j <= (lie - 5 - 1); j++)
		{
			if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i + 2][j + 2] == board[i + 3][j + 3] == board[i + 4][j + 4] && board[i][j] != ' ')
			{
				return board[i][j];
			}
		}
	}
	//"/"
	for (i = 0; i <= han - 5 - 1; i++) 
	{
		for (j = lie - 1; j >= lie - 1 - 5; j--)
		{
			if (board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i + 2][j - 2] && board[i + 2][j - 2] == board[i + 3][j - 3] && board[i + 3][j - 3] == board[i + 4][j - 4] && board[i][j] != ' ')
			{
				return board[i][j];
			}
		}
	}
	if (count == 100)
	{
		return 'Q';
	}
	return 'C';
}

3、game.h

#define  _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define HAN 10
#define LIE 10

void init_board(char board[HAN][LIE], int han, int lie);

void print_board(char board[HAN][LIE], int han, int lie);

void player_move(char board[HAN][LIE], int han, int lie);

void computerr_move(char board[HAN][LIE], int han, int lie);

char is_win(char board[HAN][LIE], int , int );

int count;

4、运行结果

在这里插入图片描述