C language implements three-piece chess!

 Preface: Implementing three-piece chess in C language can be divided into six steps!

1: First, the preparations for starting the mini-game -----

      First of all, we have to choose at least once when entering the mini-game. We use a do--while statement to achieve this!

       Then create a simple menu-----

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

 Then use a selection statement for the player to choose -----

	do
	{
		menu();
		printf("请输入选项->\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1: 
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入->\n");
			break;
		}
	} while (input);

 Now it has taken shape, just go to the next step!

2: Create a chessboard (a two-dimensional array) and initialize it----  

//初始化棋盘
	init(board, ROW, COL);

Give spaces to all chess pieces on the chessboard

void init(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] = ' ';
		}
	}
}

Take the next step!

3: Print the chessboard----

//打印棋盘
	display(board, ROW, COL);

As the name suggests, you need to print a 3*3 "well" pattern and assign values ​​to its pieces----

Table example----

         |   |   
	  ---|---|---
	     |   |   
	  ---|---|---
		 |   |   
	     

The code implementation is as shown in the figure:

void display(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");
		}
	}
}

Go to the next step!

4: The player starts playing chess and prints the chess board ---

	//玩家下棋
		playermove(board, ROW, COL);
		display(board, ROW, COL);

The code implementation is as shown in the figure -----

void playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("玩家下棋->\n");
		scanf("%d%d", &x, &y);
		if (x <= row && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else printf("位子被占,请重新输入->\n");
		}
		else printf("输入非法,请重新输入->\n");
	}
}

Go to the next step!

5: It’s the computer’s turn to start playing chess and print the chess board ---

	//电脑下棋
		compmove(board, ROW, COL);
		display(board, ROW, COL);

The code implementation is as shown in the figure----

void compmove(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;
		}
	}
}

Because computers play chess without rules, "timestamp" is used!

rand()----will generate random numbers.

The use of rand function requires ---- "srand"!

srand((unsigned int)time(NULL));

Using the "rand" function you only need to reference "srand" once -- so put it first!

int main()
{
	srand((unsigned int)time(NULL));

At the same time, a header file is required----

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

Once completed, proceed to the next step!

6: The player or the computer must determine the winner or loser every time after playing chess ---

//判断输赢
		ret=iswin(board, ROW, COL);

There are 4 results---Draw---Player wins---Computer wins---Continue---

Use different return values ​​to make judgments---

while (1)
	{
		//玩家下棋
		playermove(board, ROW, COL);
		display(board, ROW, COL);
		//判断输赢
		ret=iswin(board, ROW, COL);
		if (ret != 'c')
			break;
		//电脑下棋
		compmove(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");

The function code implementation is shown in the figure--

int flat(char board[ROW][COL], 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 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][1];
	}
	//列
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
			return board[1][j];
	}
	//对角
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
		return board[1][1];
	else if (flat(board, ROW, COL) == 1)
		return 'q';
	else return 'c';
}

Then it’s ok!

The main thing is to be careful, step by step, and remember not to make mistakes!

You can use several more files during the writing process to make the division clearer!

I divided this code into three files!

The following is the implementation of the total code---

-----test.c---File name--Start file-

#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 };
	//初始化棋盘
	init(board, ROW, COL);
	//打印棋盘
	display(board, ROW, COL);
	int ret = 0;
	while (1)
	{
		//玩家下棋
		playermove(board, ROW, COL);
		display(board, ROW, COL);
		//判断输赢
		ret=iswin(board, ROW, COL);
		if (ret != 'c')
			break;
		//电脑下棋
		compmove(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");
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请输入选项->\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1: 
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入->\n");
			break;
		}
	} while (input);
	return 0;
}

----game.h----Header file name--Responsible for the declaration of functions

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void init(char board[ROW][COL],int row,int col);
void display(char board[ROW][COL], int row, int col);
void playermove(char board[ROW][COL], int row, int col);
void compmove(char board[ROW][COL], int row, int col);
char iswin(char board[ROW][COL], int row, int col);

-----game.c----File name---Responsible for the implementation of functions

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init(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 display(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("玩家下棋->\n");
		scanf("%d%d", &x, &y);
		if (x <= row && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else printf("位子被占,请重新输入->\n");
		}
		else printf("输入非法,请重新输入->\n");
	}
}

void compmove(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;
		}
	}
}


int flat(char board[ROW][COL], 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 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][1];
	}
	//列
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
			return board[1][j];
	}
	//对角
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
		return board[1][1];
	else if (flat(board, ROW, COL) == 1)
		return 'q';
	else return 'c';
}

The above is the implementation of three-piece chess in C language---if it inspires you------give me a one-click three-piece game!

Goodbye!

Guess you like

Origin blog.csdn.net/m0_71676870/article/details/130544107