C language to realize three-bang chess (prenatal education version teaching) from shallow to deep

Introduction to Backgammon

Backgammon is a traditional folk game, also known as Jiugong chess, Tic Tac Toe and so on. The game is divided into two sides to play against each other. Both sides place pieces on the 9-square chessboard in turn, and the first to move their three pieces into a line is considered a victory.

insert image description here

Determining the steps to start the configuration file

We chose to use multiple files to teach everyone to complete independently. To realize the game of backgammon,
first of all, we need to prepare three files: game.h to declare the function game.c to realize the definition of the function . The general file structure of test.c It's finished
insert image description here

Implementation of the game code

Step 1. Determine several parts of the main body of the file test.c

  • 1. The menu of the game to determine whether to start and exit

Enter 1 to start the game and enter 0 to exit the game

#include “game.h”//由于我们要调用game.h头文件所以其他需要的头文件
				//就全部放在game.h里面避免代码冗余,让代码更加简洁
//菜单
void menu()
{
    
    
	printf("************************\n");
	printf("*******  1.play  *******\n");
	printf("*******  0.exit  *******\n");
	printf("************************\n");
}
int main()
{
    
    

	int input = 0;	//创建变量来接收输入的结果
	do
	{
    
       
		//打印菜单
		menu();
		printf("请输入你的选择 >");
		scanf("%d",&input);
		switch (input)
		{
    
    
			//开始游戏
		case 1:
			game();//这里先写出游戏函数后面实现
			break; 
			//退出游戏
		case 0: 
			printf("退出游戏!\n");
			break;
		default:
			printf("输入错误,请重新输入!");
			break;
		}
	} while (input);
	return 0;
}

1. Write the body of the game() function

  • 1. Initialization of the chessboard
  • 2. Print the chessboard
  • 3. When a player plays chess, the pieces are marked with *
  • 4. Computer chess pieces are represented by #
  • 5. Judging winning or losing
    • Player Win Return *****
    • computer win returns #
    • draw returns Q
    • Continue to return to C

1. Initialization of the chessboard

Observe that the chessboard is 3×3, so we use a 3×3 array to store the chess pieces and complete the initialization

void game()//三子棋游戏主体函数
{
    
    
	char board[ROW][COL] = {
    
    0};
	//初始化数组
	InitBoard(board, ROW, COL);
}
在这里插入代码片

Then first create a function that implements 5 small functions and then call it

//三子棋游戏
void game()
{
    
    
	char board[ROW][COL] = {
    
    0};
	//初始化数组
	InitBoard(board, ROW, COL);
	char ret = 0;//用来接收输赢函数返回的  值
	char play = 0;//用来接收下棋坐标被占用的返回值
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	while (1)//创建一个循环只要没有人赢就一直下
	{
    
    
		//玩家下棋
		play = PlayMove(board, ROW, COL);
		if (play == 'P')//当下棋时坐标输入错误和坐标被占用就退出循环
				continue;//不让电脑下棋
		//判断输赢
		ret = IsWin(board, ROW, COL);//接收每次判断的结果
		if (ret != 'C')//当返回的不是 C 继续的话就退出说明有人赢了
		{
    
    
			system("cls");//退出时清空所有棋盘并打印一遍
			//打印棋盘
			DisplayBoard(board, ROW, COL);		
			break;
		}		
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		//电脑下棋
		ComputerMove(board, ROW, COL);
		system("cls");//每次下完棋后清空屏幕所有内容并打印一遍棋盘
		//打印棋盘	  //让每次屏幕上只有一个棋盘
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')//当返回的不是 C 继续的话就退出说明有人赢了
		{
    
    				//就进入if里面结束本次循环
			system("cls");//退出时清空所有棋盘并打印一遍
			//打印棋盘
			DisplayBoard(board, ROW, COL);
			break;
		}
	}
	if (ret == '*')
	{
    
    
		printf("玩家赢!\n");
	}
	else if (ret == '#')
	{
    
    
		printf("电脑赢!\n");
	}
	else 
	{
    
    
		printf("平局!\n");
	}
}

Step 2. Declaration of game.h header file

The game has been split into 5 small parts, and the next step is to declare and define

  • Declaration and commonly used header files, we put it in the header file of game.h
#pragma once
#include <stdio.h> //输入输出头文件
#include <stdlib.h> //获取随机函数头文件 后面会用
#include <time.h> //srand函数所需要获取时间戳的头文件 后面会用
#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);
//玩家下棋
int PlayMove(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);
//平局
int  IsFull(char board[ROW][COL], int row, int col);

Step 3. Function implementation of game.c

Various function names and function declarations We have completed the next step is to implement these small functions in game.c

#include "game.h"//先在game.c里面调用
				//在game.h里面声明的头文件和函数

1. The realization of the initialization chess piece array function

Use a loop to traverse the chess pieces of the array so that all the spaces are stored in it

//初始化数组	
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] = ' ';
		}
	}

}

2. Realization of printing chessboard function

insert image description here

Observe the chessboard and find that it is divided into 2 parts

insert image description here

  • The first part is (space piece space | space piece space | space piece space)
  • The second part is (- - - | - - - | - - -) and each part is output three times

So we use a for loop to write
| there are only two in each line, so we need to write an if judgment

//打印棋盘
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");
		for (j = 0; j < col; j++)
		{
    
    
			printf("---");
			if (j < col - 1)
				printf("|");
		}
		printf("\n");

		
	}

}

3. Realization of the player's chess function

What is received here is that the coordinates entered by the player usually start from 1 to 3
, but the array subscript starts from 0, so it is enough to put the row and column entered by the player - 1 every time.

//玩家下棋
int  PlayMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	printf("玩家下棋");
	printf("请输入坐标,中间用空格隔开 ->");
	
	scanf("%d %d",&x,&y);
	//检查坐标是否合法
	if (x >= 1 && y >= 1 && x <= row  && y <= col )
	{
    
    
		if (board[x - 1][y - 1] == ' ')
		{
    
    
			 board[x - 1][y - 1] = '*';
		}
		else
		{
    
    
			printf("坐标被占用请重新输入!\n");
			return 'P';
		}
	}
	else
	{
    
    
		printf("输入错误请重新输入!\n");
		return 'P';
	}
	
}

4. Realization of computer chess function

We let the computer generate random number coordinates each time to generate rows and columns to play chess

//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
    
    
	//随机下棋
	int x = 0;
	int y = 0;
	printf("电脑下棋:>\n");
	while (1)
	{
    
    
		x = rand() % row;//产生的随机数模除我们需要的行 产生的数就在行0~3之内了
		y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}
	}
	
}

5. Realization of judging win or lose function

//玩家赢返回 *
//电脑赢返回 #
//平局返回   Q
//继续返回	 C
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][1] == board[i][2] && board[i][1] != ' ')
		{
    
    
			return board[i][0];
		}
		//判断列
		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 (IsFull(board, row, col) == 1)
	{
    
    
		return 'Q';
	}
	else 
	{
    
    
		return 'C';
	}
	//继续 C
}

6. Realization of judgment draw function

Traverse the array once and when there is no space in the array, it means that the chessboard is full of draws

//平局
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;
}

The overall code is as follows:

test.c code

#define _CRT_SECURE_NO_WARNINGS 1
#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);
	char ret = 0;
	char play = 0;
	//打印棋盘
	 DisplayBoard(board, ROW, COL);
	while (1)
	{
    
    
		//玩家下棋
		play = PlayMove(board, ROW, COL);
		if (play == 'P')
			continue;
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
		{
    
    
			system("cls");
			//打印棋盘
			DisplayBoard(board, ROW, COL);		
			break;
		}		
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		//电脑下棋
		ComputerMove(board, ROW, COL);
		system("cls");
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
		{
    
    	
			system("cls");
			//打印棋盘
			DisplayBoard(board, ROW, COL);
			
			break;
		}
	}
	if (ret == '*')
	{
    
    
		printf("玩家赢!\n");
	}
	else if (ret == '#')
	{
    
    
		printf("电脑赢!\n");
	}
	else 
	{
    
    
		printf("平局!\n");
	}
}
int main()
{
    
    

	int input = 0;	
	do
	{
    
       
		srand((unsigned int)time(NULL));
		//打印菜单
		menu();
		printf("请输入你的选择 >");
		scanf("%d",&input);
		switch (input)
		{
    
    
			//开始游戏
		case 1:
			game();
			break; 
			//退出游戏
		case 0: 
			printf("退出游戏!");
			break;
		default:
			printf("输入错误,请重新输入!");
			break;
		}
	} while (input);
	return 0;
}

game.h code

#pragma once
#include <stdio.h>
#include <stdlib.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);
//玩家下棋
int PlayMove(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);
//平局
int  IsFull(char board[ROW][COL], int row, int col);

game.c code

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.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");
		for (j = 0; j < col; j++)
		{
    
    
			printf("---");
			if (j < col - 1)
			{
    
    
				
				printf("|");
			}
				
		}
		printf("\n");

		
	}

}

//玩家下棋
int  PlayMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	printf("玩家下棋");
	printf("请输入坐标,中间用空格隔开 ->");
	
	scanf("%d %d",&x,&y);
	//检查坐标是否合法
	if (x >= 1 && y >= 1 && x <= row  && y <= col )
	{
    
    
		if (board[x - 1][y - 1] == ' ')
		{
    
    
			 board[x - 1][y - 1] = '*';
		}
		else
		{
    
    
			printf("坐标被占用请重新输入!\n");
			return 'P';
		}
	}
	else
	{
    
    
		printf("输入错误请重新输入!\n");
		return 'P';
	}
	
}

//电脑下棋
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;
		}
	}
	
}
//玩家赢 *
//电脑赢 #
//平局   Q
//继续	 C
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][1] == board[i][2] && board[i][1] != ' ')
		{
    
    
			return board[i][0];
		}
		//判断列
		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 (IsFull(board, row, col) == 1)
	{
    
    
		return 'Q';
	}
	else 
	{
    
    
		return 'C';
	}
	//继续 C

	

}
//平局
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;
}

Guess you like

Origin blog.csdn.net/qq_57761637/article/details/130672990