[C language] Simple version of three-bang chess (with source code)

Article directory

foreword

1. What is backgammon?

2. Function realization

1. Print menu

2. Print the chessboard

3. Play chess

4. Judge win or lose

3. Game testing

4. Source code

1.game.h (header file, function definition, macro definition)

2.game.c (function implementation)

3.test.c (main function, function call)

Summarize


foreword

        As we learn more about the C language, after mastering the theoretical knowledge, we need some "small projects" or "small games" to practice our hands, and three-man chess is a good choice. Below I will briefly introduce the game of three games and how to implement it in C language.


1. What is backgammon?

        Backgammon is a traditional folk game, also known as Jiugong chess, circle chacha chess, one dragon, 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-grid chessboard in turn. It is considered a victory if they are the first to line up their three pieces in a line. Even if the opponent loses, but in many cases there will be a draw Chess situation.

        game rules:

        If two people have mastered the skills, then generally speaking, it is a draw. Generally speaking, it is most beneficial to place the second step in the middle (because the first step cannot be placed in the middle), followed by the next step on the corner, and thirdly on the side. The biggest advantage is that you can play this simple and fun game anywhere you want.

2. Function realization

1. Print menu

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

2. Print the chessboard

#define ROW 3	//棋盘的行数
#define COL 3	//棋盘的列数

char board[ROW][COL];    //演示用,不是全局变量

Here, a two-dimensional array "board" is regarded as a chessboard. "ROW" represents the number of rows of the chessboard, and "COL" represents the number of columns of the chessboard, which is convenient for customizing the number of rows and columns in the future.

//初始化棋盘为空格
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

//打印棋盘
void PrintBoard(char board[ROW][COL], int row, int col)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		//1.打印数据
		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");
		}
	}
}

3. Play chess

Let the player (in the form of input coordinates) play chess first, and then the computer randomly plays chess.

//玩家输入坐标下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x, y;
	printf("玩家下棋\n");
	while (1)
	{
		printf("请输入下棋的坐标,中间使用空格隔开:");
		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] = 'X';
				break;
			}
			//不能落子
			else
			{
				printf("坐标被占有,不能落子,请重新输入坐标\n");
			}
		}
		//坐标非法
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
}

//电脑随机下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x, y;
	printf("电脑下棋\n");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = 'O';
			break;
		}
	}
}

4. Judge win or lose

After each chess game, it is necessary to judge whether to win or lose (there are 3 identical pieces on the row, column, and diagonal), as well as the situation of a tie (the winner has not yet been determined after the board is full).

//判断棋盘是否被占满
//未占满 --- 0
//占  满 --- 1
int IsFull(char board[ROW][COL], int row, int col)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

//判断输赢
//玩家赢 --- 'X'
//电脑赢 --- 'O'
//平  局 --- 'Q'
//继  续 --- 'C'
char IsWin(char board[ROW][COL], int row, int col)
{
	//赢
	int i;
	//行
	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[0][0] != ' ')
		return board[0][0];
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
		return board[0][2];
	//平局
	if (IsFull(board, row, col) == 1)
	{
		return 'Q';
	}
	//继续
	return 'C';
}

3. Game testing

 

4. Source code

Compiler: Visual Studio 2022

1.game.h (header file, function definition, macro definition)

#pragma once

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

#define ROW 3	//棋盘的行数
#define COL 3	//棋盘的列数

//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void PrintBoard(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);
//判断输赢
//玩家赢 --- 'X'
//电脑赢 --- 'O'
//平  局 --- 'Q'
//继  续 --- 'C'
char IsWin(char board[ROW][COL], int row, int col);

2.game.c (function implementation)

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//初始化棋盘为空格
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

//打印棋盘
void PrintBoard(char board[ROW][COL], int row, int col)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		//1.打印数据
		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, y;
	printf("玩家下棋\n");
	while (1)
	{
		printf("请输入下棋的坐标,中间使用空格隔开:");
		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] = 'X';
				break;
			}
			//不能落子
			else
			{
				printf("坐标被占有,不能落子,请重新输入坐标\n");
			}
		}
		//坐标非法
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
}

//电脑随机下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x, y;
	printf("电脑下棋\n");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = 'O';
			break;
		}
	}
}

//判断棋盘是否被占满
//未占满 --- 0
//占  满 --- 1
int IsFull(char board[ROW][COL], int row, int col)
{
	int i, j;
	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;
	//行
	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[0][0] != ' ')
		return board[0][0];
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
		return board[0][2];
	//平局
	if (IsFull(board, row, col) == 1)
	{
		return 'Q';
	}
	//继续
	return 'C';
}

3.test.c (main function, function call)

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

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

void game()
{
	char board[ROW][COL];
	InitBoard(board, ROW, COL);		//初始化棋盘
	PrintBoard(board, ROW, COL);	//打印棋盘
	//下棋
	char ret;
	while (1)
	{
		PlayerMove(board, ROW, COL);	//玩家下棋
		PrintBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		ComputerMove(board, ROW, COL);	//电脑下棋
		PrintBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == 'X')
		printf("玩家赢\n");
	else if (ret == 'O')
		printf("电脑赢\n");
	else
		printf("平局\n");
}

int main()
{
	int input;
	srand((unsigned int)time(NULL));
	do
	{
		menu();		//菜单
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
	} while (input);
	return 0;
}

Summarize

        The above is all the content of the simple version of backgammon implemented in C language. There is still a lot of room for improvement in this code. I will update it later: "Upgrading backgammon from backgammon" and "smarter AI", so stay tuned!

Guess you like

Origin blog.csdn.net/m0_73156359/article/details/130932205