Realization of Three-Mixed Chess with C Language

three pieces

The rules of three-piece chess are: in a 3x3 chessboard, the two sides take turns to play stones (represented by X and O), and the first player to connect the three stones into a line (both horizontal, vertical, and diagonal) wins.

The following are several problems in the implementation of the three-banger C language:
1. To implement the three-banger, we need a chessboard, and then the content of the chessboard.
2. When playing chess, it is necessary to judge whether the position is legal.
3. The player and the computer have different chess logic. The player needs to input manually, and the computer plays the chess by itself
. 4. Every next chess move needs to determine whether the game is over (which side wins, draws, or continues to play chess).

Design method:

Three files need to be created: a header file and two source files. The header file contains function declarations and a series of preprocessing commands, named game.h

Two source files, one contains the main function (the general running order of the game), named work.c, and the other contains the implementation logic of the game content (magnifying part of the execution function), named game.c

work.c settings

The first is to enter the game, in order to ensure that the game can be played repeatedly, we can achieve this in a loop

//work.c里面的内容
#include"gmae.h"//这样的话,在game.h里面包含的文件,在work.c里面也能使用

int main()
{
    
    
	int option = 0;//在主函数里面设计循环,可以保证每次游戏结束后可以选择继续游戏或者退出游戏
	do 
	{
    
    
		Menu();
		scanf("%d", &option);
		switch (option)
		{
    
    
		case 1:
			game();//如果选择开始游戏,就会进入到game()函数,
                /*我们需要定义game函数来存放游戏的实现顺序*/
			break;
		case 0:
			printf("已退出");
			break;
		default :
			printf("错误选择,请重新输入");
		}

	} while (option);
	return 0;
}
//work.c里面的内容
void Menu()//定义菜单函数,配合主函数里面的循环,可以实现每次游戏结束会跳到这个界面
{
    
    
	printf("*****************\n");
	printf("***  1.play  ****\n");
	printf("***  0.exit  ****\n");
	printf("*****************\n");
	printf("请选择");

}

Then the order of the game needs to be implemented in the game function

//work.c里面的内容
void game()
{
    
    
	srand((unsigned int)time(NULL));//使用随机数,便于后面电脑下棋时是随机出棋
	char board[ROW][COL] = {
    
     0 };//创建一个二维数组,用来实现棋盘的内容
	printf("游戏开始:\n");
	init_board(board,ROW,COL);//初始化棋盘

	print_board(board, ROW, COL);//打印棋盘
	while (1)
	{
    
    
		char ret = '0';
		player_move(board, ROW, COL);//玩家下棋
		print_board(board, ROW, COL);//打印棋盘
		ret = is_win(board, ROW, COL);//判断是否结束
		if (ret == '*')
		{
    
    
			printf("玩家胜利\n");
			break;
		}
		computer_move(board, ROW, COL);//电脑下棋
		print_board(board, ROW, COL);//打印棋盘
		ret = is_win(board, ROW, COL);//判断是否结束

		if (ret == '#')//
		{
    
    
			printf("电脑胜利\n");
			break;
		}//判断电脑胜利
		if (ret == 'Q')//判断平局
		{
    
    
			printf("平局\n");
			break;
		}//判断平局
		
	}

}

Here are the solutions to these problems:

Content settings of game.h

#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3//设置了行与列的值,可以更改
#include<stdio.h>
#include<time.h>//用来实现时间戳
//下面是游戏实现逻辑所需的函数声明。
void init_board(char board[ROW][COL], int row, int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);

game.c settings

1. Contents of the chessboard

We create a two-dimensional array as a chess piece, and perform the steps of playing chess by entering the coordinates

Create an array:

char board[ROW][COL] ;这里括号里面的值我们用提前处理过的ROW与COL来表示,
    通过预处理命令,我们后期可以很方便的更改ROW与COL的值来改变棋盘的大小

1. Checkerboard and initialization of checkerboard

The chessboard is in the form of 3x3, so we can print a chessboard in a loop, the following is the format of the chessboard

 0 | 0 | 0  
---|---|---
 0 | 0 | 0
---|---|---
 0 | 0 | 0 //而这个表里面的0就是我们的下的棋子占有的位置
  
 /*而这个棋盘的设计是有明显的规律的:
     第一行: 空格%c空格
	第二行:---|循环3遍,但是最后遍不打印“|”
     然后这样的两行在循环中多次打印,就出现了这样的三子棋表格
     注意:最后一行不用再打印“---”

Here we define the init_board() function to initialize the chessboard (ie let all elements of the array have blanks)

void init_board(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++)
		{
    
    
			board[i][j] = ' ';//把所有元素赋值为' '
		}
	}
}

2. Print the chessboard

void print_board(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++)
		{
    
    

			printf(" %c ", board[i][j]);
			if (j != col - 1)
			{
    
    
				printf("|");//最后一列不打印'|'
			}
		}
		//每次进行上一个循环时,i的值在变化,打印的是不同的值
		printf("\n");
		if (i != row - 1)//最后一行不打印字符
		{
    
    
			for (j = 0; j < col; j++)
			{
    
    


				printf("---");
				if (j != col - 1)
					printf("|");

			}
		}

		printf("\n");


	}
	printf("\n");



}

3. Chess steps

player playing chess
void player_move(char board[ROW][COL], int row, int col)
{
    
    
	printf("玩家下棋:\n");
	int x = 0;
	int y = 0;
	int i = 0;
	int j = 0;
	
	
	while (1)
	{
    
    
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y <= col & y >= 1)
		{
    
    
			if (board[x-1][y-1] == ' ')
			{
    
    
				board[x-1][y-1] = '*';
				break;
			}
			else
			{
    
    
				printf("该坐标被占用,请重新输入\n");
			}
		}
	}
		
	
}
computer chess
void computer_move(char board[ROW][COL], int row, int col)
{
    
    
	char x = 0;
	char y = 0;
	while (1)
	{
    
    
		
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}
	}
}

4. Judging whether to win or not

char is_full(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;//用是否还有空格判断平局
			
		} 
	}
}
char is_win(char board[ROW][COL], int row, int col)//这里是判断横竖斜三种方式是否出现胜利
{
    
    
	int i = 0;
	int j = 0;
	int count = 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];
		}
		
	}

	for (i = 0; i < row; i++)
	{
    
    
		
			if ((board[0][i] == board[1][i])&&(board[2][i]==board[1][i]) && board[1][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 (is_full(board,ROW,COL))
	{
    
    
		return 'Q';//平局返回'Q'
	}
	else
		return 'C';//什么情况都没有就继续游戏
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324516677&siteId=291194637