The realization of three chess in C language [super detailed explanation]

Table of contents

1. A brief idea of ​​realizing three-bang chess

Two, the specific implementation steps of the code

 3. Complete code

1. Sanziqi.h part

2. Sanziqi.c part

 3. part of test.c



1. A brief idea of ​​realizing three-bang chess

1. First of all, if you want to play the game or exit the game, you need a simple selection menu

2. After the menu is created, realize the switch, case statement, and choose to play the game or exit

3. If the game is selected in the switch statement, the code of the game part will be executed

4. In the game part, first initialize a two-dimensional array with 3 rows and 3 columns , and initialize each element in the array as a space , so that the subsequent players can replace the spaces when playing chess with the computer.

5. Print the chessboard. Every time the player or the computer finishes playing chess, the chessboard will be printed once , so that it is convenient to observe the positions of the chess pieces on both sides after playing chess.

6. Realize the specific steps of players and computers playing chess respectively

The player plays chess: the player enters a coordinate, such as 1 1, but the coordinate of the 1 1 element in the array is 0 0, so it is necessary to subtract one from the coordinate entered by the player , which is the actual position of the player in the array

Computer chess: The computer needs to use rand (header file: stdlib.h) and time (header file: time.h) to play chess, which can make the computer play chess randomly

7. After implementing the code for the player and the computer to play chess, after the player and the computer play chess, it is necessary to implement the code for judging whether the game is over. There are four situations for judging, namely, the player wins, the computer wins, a draw, and continue playing chess

8. After the game is over, continue to print the selected menu, and the player decides whether to continue the game


 Before starting to write, please note that when we usually implement some small programs, we write them in modules . For example, the implementation of Sanziqi.h, sanziqi.c, and test.c will be used to implement three parts. To achieve specific requirements, not all codes are written in the test.c section, please note that this method will be used in future work or writing codes


Two, the specific implementation steps of the code

1. Create three parts as shown below

2. Refer to the header file in sanziqi.h , just refer to sanziqi.h in test.c and sanziqi.c



 

3. Create the switch case statement of the main function and the test function in test.c

4. Create a simple selection menu

5. Since it is 3 rows and 3 columns, macro definitions can be used to facilitate subsequent changes

 

6. Implementation of the game function in test.c ( complete the framework first, and then implement it )

7. Implement the requirements of the game function in sanziqi.c ( declare in sanziqi.h and implement in sanziqi.c ), the first picture below is the declaration, and the second picture is the realization of the function

(1), the realization of the initialization function 


 (2), print chessboard


 (3), the player plays chess


(4), computer chess

 Add in the test function:



(5) Judging the winning and losing function




 3. Complete code

1. Sanziqi.h part

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

#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 Playermove(char board[ROW][COL], int row, int col);

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

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

2. Sanziqi.c part

#include "sanziqi.h"



//初始化函数,数组元素都初始化为空格
void Initboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		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 < 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 Playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("请输入想要下棋的坐标:");
		scanf("%d %d", &x, &y);
		printf("玩家下棋:\n");
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3 && board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '*';
			break;
		}
		else
		{
			printf("输入坐标错误,请重新输入!\n");
		}
	}
}



//电脑下棋
void Computermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;

	while (1)
	{
		x = rand() % row;//rand()函数会随机生成0~32767之间的数字
		y = rand() % col;//rand()%row和col即随机生成0~2,满足二维数组坐标要求
		if (board[x][y] == ' ')
		{
			printf("电脑下棋:\n");
			board[x][y] = '#';
			break;
		}
	}
}


//判断是否平局
//下满棋盘返回1,未下满则返回0
int Isfull(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		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;
	//判断三行中每一行是否相同
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
		{
			return board[i][0];
		}
	}

	//判断三列中每一列是否相同
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][i];
		}
	}

	//判断第一条对角线是否相同
	if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
	{
		return board[0][0];
	}

	//判断第二条对角线是否相同
	if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
	{
		return board[0][2];
	}

	//判断是否平局
	if (Isfull(board, ROW, COL) == 1)//由于Isfull函数,不是功能型函数
		                             //只需要在sanziqi.c中实现即可
	{
		return 'P';
	}

	//若都不满足上方条件语句,则表示继续游戏
	return 'A';
}

 3. part of test.c

#include "sanziqi.h"


void game()
{
	char ret = 0;
	char board[ROW][COL] = { 0 };
	Initboard(board, ROW, COL);//初始化函数

	Displayboard(board, ROW, COL);//打印棋盘
	while (1)
	{
		Playermove(board, ROW, COL);//玩家下棋
		Displayboard(board, ROW, COL);//打印棋盘
		ret = Iswin(board, ROW, COL);//判断输赢,并用ret接受
		if (ret != 'A')
		{
			break;//这里的ret为判断输赢函数的返回值,A是自己设定的
			      //在判断输赢函数中,返回A则继续下棋,返回P则平局
			      //返回 # 则电脑赢,返回 * 则玩家赢
			      //不返回A说明游戏结束,则在下方判断具体结果
		}

		Computermove(board, ROW, COL);//电脑下棋
		Displayboard(board, ROW, COL);//打印棋盘
		ret = Iswin(board, ROW, COL);//判断输赢,并用ret接受
		if (ret != 'A')
		{
			break;
		}
	}

	if (ret == 'P')
	{
		printf("平局!\n");
	}

	if (ret == '*')
	{
		printf("玩家赢!\n");
	 }

	if (ret == '#')
	{
		printf("电脑赢!\n");
	}
}


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

void test()
{
	int input = 0;
	do
	{
		menu();//菜单
		srand((unsigned int)time(NULL));
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出三子棋程序!\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			break;
		}
	} while (input);
}

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


This is the end of the C language implementation of three chess, see you in the next blog ♪(^∇^*)

Guess you like

Origin blog.csdn.net/m0_64411530/article/details/125569110
Recommended