[C language] Three-dimensional chess game (application of two-dimensional array)

Use the relevant knowledge of two-dimensional arrays to realize the game of backgammon.


Table of contents

1. game.h

Two, game.c

Three, test.c 

4. Idea construction

(1) Main function frame:

(2) Encapsulated game function framework:

(1) Construction of the chessboard:

(2) Players play chess:

(3) Computer chess:

(4) Judging winning or losing:

5. TIPS


1. game.h

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<time.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 PlayerMove(char board, int row,int col);

//电脑移动
void ComputerMove(char board, int row, int col);

//判断输赢
//玩家胜利'*'
//电脑胜利'#'
//平局'Q'
//继续'C'
char Is_win(char board, int row, int col);

//判断棋盘是否满
int Is_full(char board, int row, int col);

Two, game.c

#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)
		{
			int j = 0;
			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;
	printf("请输入坐标,用空格分开:");
	while (1)
	{
		scanf_s("%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("坐标已占用,请重新输入:");
		}
		else
			printf("坐标不合法\n");
	}
}

void ComputerMove(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 - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '#';
			break;
		}
	}
}

int Is_full(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++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

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];
		//列
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ')
				return board[0][j];
		}
	}
	//对角线
	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';
	}
	//游戏继续
	return 'C';
}

Three, test.c 

#include"game.h"
	
void menu()
{
	printf("*****************************\n");
	printf("******    1. start     ******\n");
	printf("******    0.exit       ******\n");
	printf("*****************************\n");
}

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	char ret = 0;
	while (1)
	{
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = Is_win(board, ROW, COL);
		if (ret != 'C')
			break;
		ComputerMove(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");
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		scanf_s("%d", &input);
		switch (input)
		{
		case 1:
			printf("开始游戏\n");
			game();
			break;
		case 0:
			printf("游戏结束\n");
			break;
		default:
			printf("请重新输入:\n");
			break;
		}
	}while (input);//只有输入0,游戏才结束
	return 0;
}

4. Idea construction


(1) Main function frame:

First of all, I want this game to be played repeatedly, so I built a do while loop. Secondly, I need to provide the user with a game menu for the user to choose the next operation, so a menu() function is necessary. And it needs to be matched with switch, etc. The framework of the main function is so simple.


(2) Encapsulated game function framework:

(1) Construction of the chessboard:

In order to visualize the game, we need to create a chessboard and print it out. The board I envisioned and finally designed is shown in the figure:

            

 If you want to implement the chessboard with a two-dimensional array, it is obvious that its composition is:

The first line: three groups of "space + data + space + |", the last "|" is not printed, we regard "space + data + space" as a whole, and "|" as a whole, we get the first A line of preliminary logic.

The second line: three groups of "-+-+-+|", the same last "|" is not printed, similarly , we regard "-+-+-" as a whole , and "|" as a overall .

Then according to the above grouping idea, we can regard the first line and the second line as a whole , in fact, print three groups consisting of the first line and the second line as a whole , and omit the second line of the third group .

This constitutes the idea of ​​​​building the entire chessboard.

(2) Players play chess:

We set the player's position as "*", so it is obvious that the user needs to input the coordinates, and then we need to change the data of the corresponding coordinates from "space" to "*" according to the coordinates given by the user.

It should be noted that we need to judge whether the coordinates entered by the user are legal, and whether there is a chess piece at the position corresponding to the coordinates.

(3) Computer chess:

We set the computer position as "#", we can use the rand() function to generate a random number with a timestamp, and get the random coordinates by % row (column) of its coordinates. Similarly, like users, we need to judge Whether there is a chess piece at the position corresponding to the coordinates.

(4) Judging winning or losing:

So how to judge winning or losing?

Winning or losing : Obviously, if the same pieces are connected in a row, column, or diagonal, then one side will win.

Tie : The board is full.

Game Continue : If the above conditions are not met, we need to allow the game to continue.

The clever point here is that when I design the function, I design the return value of the judging function to be char. If the player wins, it returns "*", and the computer wins and returns " # ", so when judging the victory, we only need to return the board[ x][y] (this is one of the elements connected to the line), you know whether the player wins or the computer wins. A draw returns "Q", and the game continues to return "C", then the corresponding judgment condition can be added to the game() function.


 5. What you should learn most in this article

Build ideas, sort out the functions you expect to achieve, implement the code step by step according to the functions, and gradually optimize the debugging process.

Guess you like

Origin blog.csdn.net/2301_77112634/article/details/130564720