C++实现简单的井字棋

这篇博客介绍了如何使用C++实现一个简单的井字棋游戏,包括PVP(玩家对玩家)和PVE(玩家对电脑)两种模式。作者通过调用mod1PVP.h和mod2PVE.h中定义的函数,结合publicFunction.h中的通用函数,实现了游戏的逻辑。尽管代码可能不够简洁,但游戏可以正常运行,作者欢迎读者提出改进意见。
摘要由CSDN通过智能技术生成

去牛客刷题的时候看到的,随便写了写

PVE补上了,并且还优化了一小部分代码

main函数

#include"mod1PVP.h"
#include"mod2PVE.h"
#include"publicFunction.h"
int main()
{
	cout << "请选择模式:"<<endl;
	cout << "1:PVP     2:PVE" << endl;
	int gamemod = 0;
	isLegal_Others(gamemod);


	if (gamemod == 1)
	{
		StartThePVPGame();
	}
	else if (gamemod == 2)
	{
		StartThePVEGame();
	}

	//重新开始游戏阶段
	while (true)
	{
		cout << "希望再来一局吗?是请输入1,否请输入2" << endl;
		int YesOrNo;
		isLegal_Others(YesOrNo);
		if (YesOrNo == 1)
		{
			cout << "请选择模式:" << endl;
			cout << "1:PVP     2:PVE" << endl;
			gamemod = 0;
			isLegal_Others(gamemod);
			if (gamemod == 1)
			{
				StartThePVPGame();
			}
			else if (gamemod == 2)
			{
				StartThePVEGame();
			}
		}
		else if (YesOrNo == 2)
		{
			cout << "感谢您的游玩!" << endl;
			break;
		}
	}
	
	return 0;
}

其中调用了:

mod1PVP.h,包含双人使用时的函数

mod2PVE.h,包含人机时使用的函数

publicFunction.h,包含双人和人机都能使用的函数

mod1PVP.h:

#pragma once
#include<iostream>
using namespace std;
void StartThePVPGame();//开始游戏

mod1PVP.cpp:

#include<vector>
#include<string.h>
#include"mod1PVP.h"
#include"publicFunction.h"

void StartThePVPGame()//开始游戏
{
	InitialMap();
	vector<bool> isEnable;//判断棋盘某位置是否能下
	isEnable.push_back(false);
	for (int i = 1; i <= 9; i++)
	{
		isEnable.push_back(true);//开始将棋盘上所有位置设为可下
	}

	vector<string> checkerboard;//棋盘
	checkerboard.push_back("-");//开局时棋盘没有子
	for (int i = 1; i <= 9; i++)
	{
		checkerboard.push_back("-");
	}

	int turn = 1;//回合数
	while (true)//下棋中
	{
		int piece;
		bool checkturn = true;//判断正确轮数
		isLegal_Playing(piece);//判断输入是否合法
		if (turn % 2)//如果是单数回合
		{
			if (isEnable[piece])//如果落子地方为空位
			{
				isEnable[piece] = false;
				checkerboard[piece] = "X";
			}
			else//落子地方不为空位,则循环结束时turn不增加
			{
				checkturn = false;
			}
		}
		else if(turn % 2 == 0)//如果是双数回合
		{
			if (isEnable[piece])//如果落子地方为空位
			{
				isEnable[piece] = fal
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值