Elementary C language - three chess

Let’s talk about a small game today. Everyone should have played backgammon, that is, when there are characters of the same size on the row and column, or on the diagonal, it is a win. Today we will write such a code to achieve this. Function

First we create two source files and one header file

The header file game.h is used to contain our header files and function declarations and some definitions
game.c to implement the game
test.c to test the logic of our game

test.c

#include<stdio.h>
void menu()
{
    
    
	printf("***************\n");
	printf("****0.exit*****\n");
	printf("****1.play*****\n");
	printf("***************\n");
}
void test()
{
    
    
	int input = 0;
	do
	{
    
    
		menu();//打印菜单
		printf("请选择>");
		scanf("%d", &input);
		switch(input)
		{
    
    
			case 0:
				printf("退出游戏\n");
				break;
			case 1:
				printf("进入游戏\n");
				break;
			default:
				printf("请重新选择\n");
				break;
		}
	} while (input);
}
int main()
{
    
    
	test();
	return 0;
}

Let’s implement the logic of a print menu first.
insert image description here
Select 1 to enter the game, select 0 to exit the game, and select another to re-select.
If you use the do while function, the menu will be printed as soon as it comes up to meet our needs.
Now we need to implement the game and select 1 to complete.

The next step is to create the game() function to implement the logic of the three-move game.
First, we need to initialize the chessboard.

test.c

void game()
{
    
    
	//存放下棋的数据
	char board[Row][Col ] = {
    
     0 };
	//初始化棋盘为空格
	Initboard(board, Row, Col);
}

game.h

//初始化棋盘
void Initboard(char board[Row][Col], int row, int col);

game.c

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

		 
}

After initializing the chessboard, we want to print the chessboard, and printing the chessboard is a tic-tac-toe

game.h

//打印棋盘
void Displayboard(char board[Row][Col], int row, int col);

game.c

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

test.c

//打印棋盘
	Displayboard(board, Row, Col);

The next thing we do is we play chess

game.h

//玩家下棋
void play_move(char board[Row][Col], int row, int col);

game.c

void play_move(char board[Row][Col], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	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] = '*';
				break;
			}
			else
			{
    
    
				printf("该内容被占用,请重新选择\n");
			}
		}
		else
		{
    
    
			printf("坐标非法,请重新输入\n");
		}
	}

}

test.c

play_move(board, Row, Col);
	//打印棋盘
	Displayboard(board, Row, Col);

After the player has finished playing chess, we start the computer to play chess, but what we have done here is just random chess

game.h

void computer_move(char board[Row][Col], int row, int  col);

game.c

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

The computer playing chess and the player playing chess are a cycle, and they are random. At this time, we need to use our rand function, and at the same time, we need to use srand, as well as the concept of time stamp. These are all mentioned in Guessing Numbers. Let’s
continue The next thing to do is to think about how to generate x = rand() % row; y = rand() % col;such coordinates. Thinking about this, we need to think about how to judge whether to win or lose. To judge whether to win or lose in three-game chess is that the diagonal or row and column must be satisfied, then we will make such a function to achieve

test.c

while (1)
	{
    
    
		
		//玩家下棋
		play_move(board, Row, Col);
		Displayboard(board, Row, Col);
		 ret=is_win(board,Row,Col);
		 if (ret != 'C')
		 {
    
    
			 break;
		 }
		//电脑下棋
		computer_move(board, Row, Col);
		Displayboard(board, Row, Col);
		ret = is_win(board, Row, Col);
		if (ret != 'C')
		{
    
    
			break;
		}
	}
	if (ret == '*')
	{
    
    
		printf("玩家赢了\n");
	}
	else if (ret == '#')
	{
    
    
		printf("电脑赢了\n");
	}
	else
	{
    
    
		printf("平局\n");
	}
	Displayboard(board, Row, Col);//再次打印棋盘
}
*玩家赢
#电脑赢
C平局

Complete code
game.h

#pragma once


#include<stdio.h>

#define Row 3
#define Col 3
//初始化棋盘
void Initboard(char board[Row][Col], int row,int  col);
//打印棋盘
void Displayboard(char board[Row][Col], int row,int  col);
//玩家下棋
void play_move(char board[Row][Col], int row, int  col);

//电脑下棋
void computer_move(char board[Row][Col], int row, int  col);
//判断输赢
char is_win(char board[Row][Col], int row, int  col);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void Initboard(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 Displayboard(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 play_move(char board[Row][Col], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	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] = '*';
				break;
			}
			else
			{
    
    
				printf("该内容被占用,请重新选择\n");
			}
		}
		else
		{
    
    
			printf("坐标非法,请重新输入\n");
		}
	}

}

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

char is_win(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][0] != ' ')
		{
    
    
			return board[i][0];
		}
	}


}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
//测试三子棋逻辑

#include"game.h"
void game()
{
    
    
	char ret = 0;
	//存放下棋的数据
	char board[Row][Col] = {
    
     0 };
	//初始化棋盘为空格
	Initboard(board, Row, Col);
	//打印棋盘
	Displayboard(board, Row, Col);
	while (1)
	{
    
    
		
		//玩家下棋
		play_move(board, Row, Col);
		Displayboard(board, Row, Col);
		 ret=is_win(board,Row,Col);
		 if (ret != 'C')
		 {
    
    
			 break;
		 }
		//电脑下棋
		computer_move(board, Row, Col);
		Displayboard(board, Row, Col);
		ret = is_win(board, Row, Col);
		if (ret != 'C')
		{
    
    
			break;
		}
	}
	if (ret == '*')
	{
    
    
		printf("玩家赢了\n");
	}
	else if (ret == '#')
	{
    
    
		printf("电脑赢了\n");
	}
	else
	{
    
    
		printf("平局\n");
	}
	Displayboard(board, Row, Col);
}
void menu()
{
    
    
	printf("**************\n");
	printf("****1.play****\n");
	printf("****0.exit****\n");
	printf("**************\n");

}
void test()
{
    
    
	int input = 0;
	do
	{
    
    
		menu();
		printf("请选择>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 0:
			printf("退出游戏\n");
			break;
		case 1:
			game();
			break;
		default :
			printf("请重新输入\n");
			break;
		}
	} while (input);
}
int main()
{
    
    
	srand((unsigned int)time(NULL));//时间戳
	test();
	
	return 0;
}

That's all for today's mini game, see you next time

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/131548066