Three-piece chess mini game (implemented in C language)


Preface

        Three-piece chess is a traditional folk game, also known as Jiugong chess, circle chess, one-dragon, tic-tac-toe, etc. The game is divided into a battle between two sides. Both sides place their chess pieces on the 9-square chessboard in turn. The first one to move their three chess pieces into a line is considered Victory, and the opponent loses, but in three-piece chess, there will be a draw situation in many cases.

1. Code implementation

1. Main menu section

int main()
{
	int input = 0;
	//电脑下棋生成随机数
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("---------------------------------\n");
		printf("请选择:)\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出成功!\n");
			break;
		default:
			printf("选择错误请重新选择。\n");
			break;
		}
	} while (input);

	return 0;
}

2.Menu part

void menu()
{
	printf("*********** 游戏菜单 ***********\n");
	printf("*********  1.开始游戏  *********\n");
	printf("*********  0.退出游戏  *********\n");
	printf("********************************\n");
}

3. Game logic part

void game()
{
	char board[ROW][COL];
	char ret;//接收Iswin的返回值
	Initboard( board,ROW,COL);
	Display(board, ROW, COL);
	//玩家下棋
	while (1)
	{
		player(board, ROW, COL);
		Display(board, ROW, COL);
		//判断游戏结果
		//玩家赢--‘*’
		//电脑赢--‘#’
		//平局----‘Q’
		//游戏继续‘C’
		ret=Iswin(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		computer(board, ROW, COL);
		Display(board, ROW, COL);
		ret = Iswin(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家胜利!\n");
	}
	else if(ret=='#')
	{
		printf("电脑胜利!\n");
	
	}
	else
	{
		printf("平局!\n");
	}
	
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

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


//展示数组
void Display(char board[ROW][COL], int row, int col)
{
	//打印数据
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ",board[i][j]);
			if(j<col-1)
			{ 
				printf("|");
			}
		}
		printf("\n");
		//打印分割行
		if(i<row-1)
		{ 
			for (int j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
		}
		printf("\n");
	}
}

//玩家下棋
void player(char board[ROW][COL], int row, int col)
{
	printf("玩家下棋\n");
	int num1 = 0;
	int num2 = 0;
	while (1)
	{
		printf("请输入要下棋的位置:\n");
		scanf("%d %d", &num1, &num2);
		if (num1 > row || num2 > col || num1 < 1 || num2 < 1)
		{
			printf("下棋位置错误,请重新输入!!!\n");
		}
		else
		{
			if (board[num1 - 1][num2 - 1] == ' ')
			{
				board[num1 - 1][num2 - 1] = '*';
				break;
			}
			else
			{
				printf("该坐标已有棋子!\n");
			}
		}//if else 
	}//while
}


//电脑下棋子
void computer(char board[ROW][COL], int row, int col)
{
	while (1)
	{
		int x = 0;
		int y = 0;
		x=rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
		
	}
}
//判断棋盘是否满了
int Isfull(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
//判断游戏结束
//玩家赢--‘*’
//电脑赢--‘#’
//平局----‘Q’
//游戏继续‘C’
char Iswin(char board[ROW][COL], int row, int col)
{
	for (int 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];
		}
		//三列判断
		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))
	{
		return 'Q';
	}
	return 'C';
}

Summarize

Let me share the three-piece chess game. The main knowledge used is the use of two-dimensional arrays.

Guess you like

Origin blog.csdn.net/m0_74058637/article/details/131944936