Take you to play three-piece chess

Foreword: The so-called simple three-piece chess is that you play chess with the computer. When one of you and the computer has three chess pieces in three rows, three columns or two diagonals on the chessboard, then that side wins. Let me walk you through the simple steps.

1. Design the main function

int main()
{
	test();
	return 0;
}

All our operations will be performed in the test() function.

2. Design the test function

void test()
{
	int input = 0;
	int srand = ((unsigned)time(NULL));//初始化随机数
	do
	{
		menu();//最开始的菜单
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();//选择1进入游戏(游戏函数)
			break;
		case 2:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入错误,请重新输入!");
			break;
		}
	} while (input);
}

3. Print menu menu

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

4. Enter the game

void game()
{
	char board[ROW][COL] = { 0 };
	Initboard(board, ROW, COL);//初始化棋盘
	Displayboard(board, ROW, COL);//打印棋盘
	int ret = 0;
    //下棋
	while (1)
	{
		//玩家下棋
		player_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);
        //结果判断
		// 玩家赢-‘*’
		// 电脑赢-‘#’
		// 继续-‘C’
		//平局(也就是棋盘满了的时候)-‘Q’
		if (ret != 'C')//判断结果是不是继续,不继续就代表出现了赢家
		{
			break;
		}
    }

Next, let's initialize the chessboard, print the chessboard, play chess and judge the result.

1. Initialization of the chessboard

Initboard(board, ROW, COL)

First of all, we need to know what the chessboard of Sanbang looks like, in order to realize

 

The initialization of the chessboard requires that each grid is empty, which can be achieved directly by using the initialization of the two-dimensional array.

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

2. Printing of the chessboard

Displayboard(board, ROW, COL)

The chessboard of the three-piece is composed of dotted lines and solid lines, so we have to find a way to print out the dividing lines first.

void Displayboard(char board[ROW][COL], int row, int col)
{
	int i = 0, j = 0, k = 0;
	for (i = 0; i < row; i++)
	{
		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");
	}
}

3. The player plays chess

player_move(board, ROW, COL)

void player_move(char board[ROW][COL], int row, int col)
{
	int x = 0, y = 0;
	printf("玩家走:");
	while (1)
	{
		scanf("%d %d", &x, &y);//玩家输入的坐标比二维数组的下标要小1
		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("输入错误!");
		}
	}
}

4. Computer playing chess

computer_move(board, ROW, COL

void computer_move(char board[ROW][COL], int row, int col)
{
	printf("电脑走:\n");
	int x = 0, y = 0;
	while (1)
	{
		x = rand() % ROW;//电脑输入的坐标由随机值产生
		y = rand() % COL;
		if (board[x - 1][y - 1] == ' ')//电脑随机下棋,只要棋盘中哪里有空格就可以下哪里
		{
			board[x - 1][y - 1] = '#';
			break;
		}
	}
}

5. Result judgment

is_win(board, ROW, COL)

There are three scenarios for winning the game:

a. Three consecutive chess pieces appear in a row

b. Three of the same chess appear in a row in a row

c. There are three identical chess pieces in a row on the diagonal (and the diagonal can be divided into a diagonal line and a diagonal line)

int is_win(char board[ROW][COL], int row, int col)
{
    //每行
	int i = 0, j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col-1; j++)
		{
			if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j+2] && board[i][j]!=' ')
				return board[i][j];
		}
	}

    //每列
	i = 0; j = 0;
	for (j = 0; j < col; j++)
	{
		for (i = 0; i < row - 1; i++)
		{
			if (board[i][j] == board[i+1][j] && board[i + 1][j] == board[i+2][j] && board[i][j] != ' ')
				return board[i][j];
		}
	}

    //捺对角线
	i = 0; j = 0;
	for (i = 0; i < row; i++)
	{
		if (board[i][i] == board[i + 1][i + 1] && board[i + 1][i + 1] == board[i+2][j+2]&& board[i][i] != ' ')
			return board[i][i];
	}
    
    //撇对角线
	i = 0; j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = col-1; j >= 0; j--)
		{
			if (board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i+2][j-2] && board[i][j] != ' ')
				return board[i][j];
		}
	}

	if (1 == is_full(board, ROW, COL))//判断棋盘是否满了
	{
		return 'Q';
	}
	else
		return 'C';
}

6. Determine whether the chessboard is full

int is_full(char board[ROW][COL], int row, int col)
{
	int i = 0, j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')//有空格则没满,没满返回0
				return 0;
		}
	}
	return 1;//满了返回1
}

The above is our basic implementation of three-piece chess

After meal:

head File:

//sanziqi.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ROW 5
#define COL 5
//初始化棋盘
void Initboard(char board[ROW][COL], int row, int col);
//打印棋盘
void Displayboard(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);

//结局判断
// 玩家赢-‘*’
// 电脑赢-‘#’
// 继续-‘C’
//平局(也就是棋盘满了的时候)-‘Q’

int is_win(char board[ROW][COL], int row, int col);

//判断棋盘是否满了
int is_full(char board[ROW][COL], int row, int col);

Test part:

//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "sanziqi.h"

void game()
{
	char board[ROW][COL] = { 0 };
	//初始化棋盘
	Initboard(board, ROW, COL);
	//打印棋盘
	Displayboard(board, ROW, COL);
	//下棋
	int ret = 0;
	while (1)
	{
		//玩家下棋
		player_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;
		}

		//结果判断
		// 玩家赢-‘*’
		// 电脑赢-‘#’
		// 继续-‘C’
		//平局(也就是棋盘满了的时候)-‘Q’
		
	}
	if (ret == '*')
	{
		printf("玩家赢!\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢!\n");
	}
	else
	{
		printf("平局!\n");
	}
}

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

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

int main()
{
	test();
	return 0;
}

Function implementation:

//sanziqi.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "sanziqi.h"
//初始化棋盘
void Initboard(char board[ROW][COL], int row, int col)
{
	int i = 0, j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

//打印棋盘
void Displayboard(char board[ROW][COL], int row, int col)
{
	int i = 0, j = 0, k = 0;
	for (i = 0; i < row; i++)
	{
		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 player_move(char board[ROW][COL], int row, int col)
{
	int x = 0, y = 0;
	printf("玩家走:");
	while (1)
	{
		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("该位置已被占用,请重新选择!");
			}
		}
		else
		{
			printf("输入错误!");
		}
	}
}

void computer_move(char board[ROW][COL], int row, int col)
{
	printf("电脑走:\n");
	int x = 0, y = 0;
	while (1)
	{
		x = rand() % ROW;
		y = rand() % COL;
		if (board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '#';
			break;
		}
	}
}

int is_win(char board[ROW][COL], int row, int col)
{
	int i = 0, j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col-1; j++)
		{
			//每行
			if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j+2] && board[i][j]!=' ')
				return board[i][j];
		}
	}

	i = 0; j = 0;
	for (j = 0; j < col; j++)
	{
		for (i = 0; i < row - 1; i++)
		{
			//每列
			if (board[i][j] == board[i+1][j] && board[i + 1][j] == board[i+2][j] && board[i][j] != ' ')
				return board[i][j];
		}
	}

	i = 0; j = 0;
	for (i = 0; i < row; i++)
	{
		//对角线1
		if (board[i][i] == board[i + 1][i + 1] && board[i + 1][i + 1] == board[i+2][j+2]&& board[i][i] != ' ')
			return board[i][i];
	}
	i = 0; j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = col-1; j >= 0; j--)
		{
			if (board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i+2][j-2] && board[i][j] != ' ')
				return board[i][j];
		}
	}

	if (1 == is_full(board, ROW, COL))
	{
		return 'Q';
	}
	else
		return 'C';
}

int is_full(char board[ROW][COL], int row, int col)
{
	int i = 0, j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

Alright, that's it for simple three-piece chess, have you stopped learning?

Guess you like

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