clientmap.cpp
来自「巫魔问题求解」· C++ 代码 · 共 210 行
CPP
210 行
#include "ClientMap.h"
#include "WorldModel.h"
#include <algorithm>
using namespace _algorithm;
using namespace _base;
////////////////////////////////////////////////
ClientMap::ClientMap(unsigned int x_border, unsigned int y_border, WorldModel &wm)
: Map(x_border, y_border)
{
this->mHasCameCells.resize(0);
this->mKnownCells.resize(0);
this->mSuspectCells.resize(0);
this->mSafeCells.resize(0);
this->mpWorldModel = &wm;
this->mIsNoSolution = false;
}
////////////////////////////////////////////////
ClientMap::~ClientMap(void)
{
}
////////////////////////////////////////////////
ClientMap::ClientMap(const ClientMap &from)
: Map(0,0)
{
}
////////////////////////////////////////////////
WorldModel &ClientMap::TheWorldModel(void)
{
return *mpWorldModel;
}
////////////////////////////////////////////////
const WorldModel &ClientMap::GetWorldModel(void) const
{
return *mpWorldModel;
}
////////////////////////////////////////////////
void ClientMap::Update(void)
{
using namespace _base;
unsigned int x = this->TheWorldModel().GetInfo().mPositionX;
unsigned int y = this->TheWorldModel().GetInfo().mPositionY;
this->TheCell(x,y)._su_AddType(this->TheWorldModel().GetInfo().mIsBreeze ? Cell::CT_Breeze : Cell::CT_None);
this->TheCell(x,y)._su_AddType(this->TheWorldModel().GetInfo().mIsStench ? Cell::CT_Stench : Cell::CT_None);
this->TheCell(x,y)._su_AddType(this->TheWorldModel().GetInfo().mIsGleam ? Cell::CT_Gleam : Cell::CT_None);
if (this->TheWorldModel().GetInfo().mIsHowl)
{
for (unsigned int i = 0; i < this->GetXBorder(); i++)
{
for (unsigned int j = 0; i < this->GetYBorder(); j++)
{
this->TheCell(i,j)._su_DeleteType(Cell::CT_Stench);
this->TheCell(i,j)._su_DeleteType(Cell::CT_Wumpus);
}
}
}
if ((this->TheCell(x,y).GetType() & Cell::CT_HasCame) == 0)
{
this->TheCell(x,y)._su_AddType(Cell::CT_HasCame);
this->UpdateCellTags(x,y);
}
}
//////////////////////////////////////////////////
void ClientMap::Reset(void)
{
for (unsigned int i=0; i<this->GetXBorder(); i++)
{
for (unsigned int j=0; j<this->GetYBorder(); j++)
{
this->TheCell(i,j)._su_SetType(Cell::CT_None);
}
}
this->mHasCameCells.resize(0);
this->mKnownCells.resize(0);
this->mSuspectCells.resize(0);
this->mSafeCells.resize(0);
this->mIsNoSolution = false;
}
////////////////////////////////////////////////
void ClientMap::UpdateCellTags(unsigned int x, unsigned int y)
{
ClientMap::Tool_InsertToVector(this->mHasCameCells, &this->TheCell(x,y));
ClientMap::Tool_InsertToVector(this->mSafeCells, &this->TheCell(x,y));
ClientMap::Tool_InsertToVector(this->mKnownCells, &this->TheCell(x,y));
if (this->mSuspectCells.size())
{
this->mSuspectCells.erase(std::find(this->mSuspectCells.begin(), this->mSuspectCells.end(), &this->TheCell(x,y)));
}
Cell *c = this->TheCell(x,y).CellUp();
if ((c!=NULL) && (std::find(this->mKnownCells.begin(), this->mKnownCells.end(), c) == this->mKnownCells.end()))
{
ClientMap::Tool_InsertToVector(this->mSuspectCells, c);
}
c = this->TheCell(x,y).CellDown();
if ((c!=NULL) && (std::find(this->mKnownCells.begin(), this->mKnownCells.end(), c) == this->mKnownCells.end()))
{
ClientMap::Tool_InsertToVector(this->mSuspectCells, c);
}
c = this->TheCell(x,y).CellLeft();
if ((c!=NULL) && (std::find(this->mKnownCells.begin(), this->mKnownCells.end(), c) == this->mKnownCells.end()))
{
ClientMap::Tool_InsertToVector(this->mSuspectCells, c);
}
c = this->TheCell(x,y).CellRight();
if ((c!=NULL) && (std::find(this->mKnownCells.begin(), this->mKnownCells.end(), c) == this->mKnownCells.end()))
{
ClientMap::Tool_InsertToVector(this->mSuspectCells, c);
}
}
////////////////////////////////////////////////////////
void ClientMap::UpdateCells(void)
{
for (std::vector<_base::Cell>::iterator iter = mCells.begin();
iter != mCells.end();
iter++)
{
iter->_su_AddType((iter->IsWumpusAround())
? _base::Cell::CT_Stench
: _base::Cell::CT_None);
iter->_su_AddType((iter->IsTrapAround())
? _base::Cell::CT_Breeze
: _base::Cell::CT_None);
iter->_su_AddType((iter->IsGoldAround())
? _base::Cell::CT_Gleam
: _base::Cell::CT_None);
}
}
/////////////////////////////////////////////////////////
std::vector<_base::Cell*> &ClientMap::TheHasCameCells(void)
{
return this->mHasCameCells;
}
/////////////////////////////////////////////////////////
std::vector<_base::Cell*> &ClientMap::TheKnownCells(void)
{
return this->mKnownCells;
}
/////////////////////////////////////////////////////////
std::vector<_base::Cell*> &ClientMap::TheSafeCells(void)
{
return this->mSafeCells;
}
/////////////////////////////////////////////////////////
std::vector<_base::Cell*> &ClientMap::TheSuspectCells(void)
{
return this->mSuspectCells;
}
/////////////////////////////////////////////////////////
const std::vector<_base::Cell*> &ClientMap::GetHasCameCells(void) const
{
return this->mHasCameCells;
}
/////////////////////////////////////////////////////////
const std::vector<_base::Cell*> &ClientMap::GetKnownCells(void) const
{
return this->mKnownCells;
}
/////////////////////////////////////////////////////////
const std::vector<_base::Cell*> &ClientMap::GetSafeCells(void) const
{
return this->mSafeCells;
}
/////////////////////////////////////////////////////////
const std::vector<_base::Cell*> &ClientMap::GetSuspectCells(void) const
{
return this->mSuspectCells;
}
/////////////////////////////////////////////////////////
void ClientMap::Tool_InsertToVector(std::vector<_base::Cell*> &v, Cell *c)
{
if (std::find(v.begin(), v.end(), c) == v.end())
{
v.push_back(c);
}
}
/////////////////////////////////////////////////////////
bool ClientMap::IsNoSolution(void) const
{
return this->mIsNoSolution;
}
/////////////////////////////////////////////////////////
bool &ClientMap::_su_IsNoSolution(void)
{
return this->mIsNoSolution;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?