C language to realize small games of three chess and five chess

The realization of three-piece chess and five-piece chess C are the same, and they must be divided into the following parts to realize respectively. The following mainly introduces the realization of three chess.

1. Initialization of the
board 2. Display of the board
3. Player input
4. Determine whether there is a three-piece situation
5. Automatic input by the computer
6. Determine whether there is a three-piece situation
 
 

1. Board initialization:

void InitialArr(char Arr[][COL], int row, int col)
{
    
    
	int i = 0;
	for (; i < row; i++)
	{
    
    
		int j = 0;
		for (; j < col; j++)
		{
    
    
			Arr[i][j] = ' ';
		}
	}
}

Set all the characters of the first board ' '.
 
 

2. Checkerboard display:

void ShowArr(char Arr[][COL], int row, int col)
{
    
    
	int i = 0;
	printf("-----------\n");
	printf("   1| 2| 3|\n");
	printf("-----------\n");
	for (; i < row; i++)
	{
    
    
		printf("%d| %c| %c| %c|\n", i+1, Arr[i][0], Arr[i][1], Arr[i][2]);
		printf("-----------\n");
	}
}

Insert picture description here
Here is a 3*3 chessboard. It doesn't need to be complicated, just simply display it.
 
 

3. Player input:

int PlayerArr(char Arr[][COL], int row, int col)
{
    
    
	printf("Please input your row and col:>");
	int i = 0;
	int j = 0;
	scanf("%d %d", &i, &j);
	if (i >= 1 && i <= 3 && j >= 1 && j <= 3)
	{
    
    
		if (Arr[i-1][j-1] != ' ')
		{
    
    
			return 2;  //该位置已经有点了,不能再输入
		}
		else
		{
    
    
			Arr[i - 1][j - 1] = BLACK;
			return 1;  //输入正确
		}
	}
	return -1;  //坐标输入错误
}

Input by the player is displayed: ‘X’.
Insert picture description here
Here the player input method, I choose to enter the coordinates from the keyboard. If the input is wrong (the range is incorrect) or the coordinates are already a little bit, an error will be prompted and the player will be asked to enter again.
 
 

4. Determine whether there are three sub-conditions:

int JudgeArr(char Arr[][COL], int row, int col)
{
    
    
	int i = 0;
	for (;i<row;i++)//每行三子,且不为空
	{
    
    
		if (Arr[i][0]!=' '&& Arr[i][0] == Arr[i][1] && Arr[i][1] == Arr[i][2])
		{
    
    
			return Arr[i][0];
		}
	}
	for (i=0; i < row; i++)//每列三子,且不为空
	{
    
    
		if (Arr[0][i] != ' '&&Arr[0][i] == Arr[1][i] && Arr[1][i] == Arr[2][i])
		{
    
    
			return Arr[0][i];
		}
	}
	if (Arr[0][0] != ' '&&Arr[0][0] == Arr[1][1] && Arr[1][1] == Arr[2][2])
	{
    
    
		return Arr[0][0];
	}//主对角线,且不为空
	if (Arr[0][2] != ' '&&Arr[0][2] == Arr[1][1] && Arr[1][1] == Arr[2][0])
	{
    
    
		return Arr[0][2];
	}//副对角线,且不为空
	for (i = 0; i < row; i++)
	{
    
    
		int j = 0;
		for (; j < col; j++)
		{
    
    
			if (Arr[i][j] == ' ')
			{
    
    
				return 'E'; //Empty,棋盘还有空,继续下棋
			}
		}
	}
	return 'D';//Draw,棋盘满了,未有输赢,平局
}

The judgment is made after the player and the computer input. There are 8 situations for winning or losing: three children in each row, three children in each column, main and vice diagonals, and none of them are empty .
Two other situations:

  1. The board is free, continue to play
  2. The chessboard is full, no wins or losses, a draw
     
     

5. Computer automatic input:

void ComputerArr(char Arr[][COL], int row, int col)
{
    
    
	while (1)
	{
    
    
		int i = rand() % row;
		int j = rand() % col;
		if (Arr[i][j] == ' ')
		{
    
    
			Arr[i][j] = WHITE;
			break;
		}
	}
	Sleep(500);
	printf("Computer done!\n");

}

Here, the srand and rand functions are used to generate random numbers. When the generated rows and columns, when the corresponding chessboard is empty at this time, they fall into the chess piece, and the computer chess piece is ‘O’. A little delay is set here.
Insert picture description here
 
 

6. Determine whether there are three sons

This is the same as the step 4 above.

 
 

Game procedure:

void Game()
{
    
    
	printf("\n游戏开始!\n");
	char Arr[ROW][COL];
	InitialArr(Arr, ROW, COL);
	char ret = 'E'; //Empty,棋盘最开始全为空,返回值可为D,WHITE和BLACK
	int ProRet = 0;
	while (1)
	{
    
    
		ShowArr(Arr, ROW, COL);
		again: //输入不对的情况,重新输入
	    ProRet=PlayerArr(Arr, ROW, COL);
		if (2 == ProRet)
		{
    
    
			printf("该位置已经有点了,请重新输入\n");
			goto again;
		}
		else if (-1 == ProRet)
		{
    
    
			printf("坐标输入有误,请重新输入\n");
			goto again;
		}
		else
		{
    
    
			printf("Player done!\n");
			Sleep(500);
		}
	    ret=JudgeArr(Arr, ROW, COL);
		if (ret != 'E')//不等于空,棋盘满了,不执行了
		{
    
    
			break;
		}
		ComputerArr(Arr, ROW, COL);
		ret = JudgeArr(Arr, ROW, COL);
		if (ret != 'E')//不等于空,棋盘满了,不执行了
		{
    
    
			break;
		}
	}
	ShowArr(Arr, ROW, COL);
	switch (ret)//进行判定,谁输谁赢,平局
	{
    
    
	case BLACK:
		printf("\n恭喜你,你赢了!\n");
		break;
	case WHITE:
		printf("\n很遗憾,你输了!\n");
		break;
	case 'D':
		printf("\n平局,再接再厉!\n");
		break;
	default:
		break;
	}
	printf("\n游戏结束!\n");
}

Insert picture description here
The sample picture of Gobang is the same as the realization method of Sanbang.

I have put the programs of three chess and gobang on my Github. If you need it, please pick it up: three chess and gobang

Guess you like

Origin blog.csdn.net/w903414/article/details/105622734