五子棋游戏实现

头文件(chess.h)

#pragma once
#include<stdio.h>
#define ROW 20 //棋盘行数
#define COLUMN 20//棋盘列数
#define CONNECT 5//多少子棋

//结构体创建棋盘
typedef struct Board {
	const char* chess_Board[ROW][COLUMN];
	int size;
}Board,*PBoard;

//功能
//初始化棋盘属性
void initchessboard(PBoard pboard);

//判断棋子所下的位置 是否满足成功的条件:连成CONNECT 个数。
bool success(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type);// 横向,竖向,两个斜方向

//该棋子横向 是否连成5子 [白] [白] [白] [白] [白]/或者[黑] [黑] [黑] [黑] [黑] 
bool success_hx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type);  //"●"

//该棋子竖向 是否连成5子
bool success_sx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type);

//该棋子正斜向 是否连成5子   /
bool success_zxx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type);

//该棋子反斜向 是否连成5子  \ 
bool success_fxx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type);

bool isfull(PBoard pboard); //判断棋盘是否已被占满

void start(PBoard pboard); //开始下棋

void show_board(PBoard pboard); //展示棋盘

函数功能实现模块(chess.cpp)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include"chess.h"
#include<Windows.h>

//棋盘初始化
void initchessboard(PBoard pboard) {
	assert(pboard);
	for (int i = 0; i < ROW; i++) {
		for (int j = 0; j < COLUMN; j++) {
			pboard->chess_Board[i][j]="+";
		}
	}
	pboard->size = 0;//开始的时候没有棋子
}

//判断棋子所下的位置 是否满足成功的条件:连成CONNECT 个数。
bool success(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type)// 横向,竖向,两个斜方向
{
	return success_hx_position(pboard, cur_xpos, cur_ypos, chess_type)
	|| success_sx_position(pboard, cur_xpos, cur_ypos, chess_type)
	|| success_zxx_position(pboard, cur_xpos, cur_ypos, chess_type)
	|| success_fxx_position(pboard, cur_xpos, cur_ypos, chess_type);
}
//该棋子横向 是否连成5子 [白] [黑] [白]
bool success_hx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type) //"●"
{
	int count = 1, x = cur_xpos;
	int y=cur_ypos-1;
	for (; y >= 0&& pboard->chess_Board[x][y] == chess_type; y--) {
		count++;
	}
	y = cur_ypos + 1;
	for (; y <= COLUMN && pboard->chess_Board[x][y] == chess_type; y++) {
		count++;
	}
	if (count >= CONNECT) {
		return true;
	}
	return false;
}
//该棋子竖向 是否连成5子
bool success_sx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type)
{
	int count = 1, x = cur_xpos-1;
	int y = cur_ypos ;
	for (; x >= 0 && pboard->chess_Board[x][y] == chess_type; x--) {
		count++;
	}
	x = cur_xpos + 1;
	for (; x <= ROW && pboard->chess_Board[x][y] == chess_type; x++) {
		count++;
	}
	if (count >= CONNECT) {
		return true;
	}
	return false;
}

//该棋子正斜向 是否连成5子   /
bool success_zxx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type)
{
	int count = 1, x = cur_xpos - 1;
	int y = cur_ypos+1;
	for (; x >= 0&&y<= COLUMN && pboard->chess_Board[x][y] == chess_type; x--,y++) {
		count++;
	}
	x = cur_xpos + 1,y=cur_ypos-1;
	for (; x <= ROW&&y>=0 && pboard->chess_Board[x][y] == chess_type; x++,y--) {
		count++;
	}
	if (count >= CONNECT) {
		return true;
	}
	return false;
}

//该棋子反斜向 是否连成5子  \ 
bool success_fxx_position(PBoard pboard, int cur_xpos, int cur_ypos, const char* chess_type) {
	int count = 1, x = cur_xpos - 1;
	int y = cur_ypos - 1;
	for (; x >= 0&& y >= 0&& pboard->chess_Board[x][y] == chess_type; x--, y--) {
		count++;
	}
	x = cur_xpos + 1, y = cur_ypos + 1;
	for (; x <= ROW&&y <= COLUMN  && pboard->chess_Board[x][y] == chess_type; x++, y++) {
		count++;
	}
	if (count >= CONNECT) {
		return true;
	}
	return false;
}

bool isfull(PBoard pboard) //判断棋盘是否已被占满
{
	return pboard->size == ROW * COLUMN;
	
}
void start(PBoard pboard)//开始下棋
{
	
	const char* chess_type;
	char chioce;
	int colour, xpos, ypos;
	while (true)
	{
		initchessboard(pboard);
		printf("五子棋游戏开始\n 请选择输入的棋子类型\n 1. 黑棋\n 2. 白棋\n 3. 退出\n");
		scanf("%d", &colour);
		if (colour == 3)
		{
			printf("您选择了结束游戏!\n");
			return;
		}
		chess_type = colour == 1 ? "●" : "○";
		while (true)
		{
			while (true)
			{
				show_board(pboard);
				printf("请输入%s棋子的坐标(下标):", chess_type);
				scanf("%d%d", &xpos, &ypos);
				if (pboard->chess_Board[xpos][ypos] == "+") { //前提该位置空闲
					pboard->chess_Board[xpos][ypos] = chess_type; //下棋操作
					break;
				}
				else {
					printf("该位置已被占用,请重新选择新坐标\n");
				}
			}
			pboard->size++; //有效棋子个数+1
			//判断该棋子所处的位置,是否周围连续连成五个子
			bool result = success(pboard, xpos, ypos, chess_type);
			//若result == true   赢   提示是否需要进行新一轮游戏?
			if (result) {
				show_board(pboard);
				printf("%s棋子获胜\n 是否进行新游戏(y or n)?:", chess_type);
				scanf("%s", &chioce); 
				getchar();
				if (chioce == 'y') { //新游戏开始,新的棋盘
					break; //跳出一层循环
				}
				else if (chioce == 'n')
				{ //'n'
					printf("游戏结束\n");
					return;
				}
			}
			else
			{
				chess_type = chess_type == "○" ? "●" : "○";
			}
		}

	}
}
void show_board(PBoard pboard) //展示棋盘
{
	system("cls");
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COLUMN; j++)
		{
			printf("%s", pboard->chess_Board[i][j]);
		}
		printf("\n");
	}
}

测试文件(text.cpp)

#include<stdio.h>
#include"chess.h"
int main()
{

	Board board;
	start(&board);
	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值