去牛客刷题的时候看到的,随便写了写
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])//如果落子