⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 map.cpp

📁 巫魔问题求解
💻 CPP
字号:
#include "Map.h"
#include "Cell.h"
#include "Predeclarations.h"
#include <iostream>
#include <fstream>

using namespace _base;

//////////////////////////////////////////////////
Map::Map(unsigned int x_border, unsigned int y_border)
{
    mXBorder = x_border;
    mYBorder = y_border;
    
    this->mCells.assign(this->mXBorder * this->mYBorder, Cell(this));
    this->LinkCells();
}

//////////////////////////////////////////////////
Map::Map(const _base::Map &from)
{
    mXBorder = from.mXBorder;
    mYBorder = from.mYBorder;

    this->mCells.assign(from.mCells.begin(), from.mCells.end());
    this->LinkCells();
}

Map::Map(char *filename)
{
    this->Open(filename);
}
//////////////////////////////////////////////////
Map::~Map(void)
{
}

//////////////////////////////////////////////////
Map Map::operator = (const Map &right)
{
    mXBorder = right.mXBorder;
    mYBorder = right.mYBorder;

    mCells.assign(right.mCells.begin(), right.mCells.end());
    this->LinkCells();
    return *this;
}

//////////////////////////////////////////////////
Cell & Map::TheCell(unsigned int x, unsigned int y)
{
    ASSERT(x < mXBorder && y < mYBorder);
    return this->mCells[x * this->mYBorder + y];
}

//////////////////////////////////////////////////
const Cell & Map::GetCell(unsigned int x, unsigned int y) const
{
    ASSERT(x < mXBorder && y < mYBorder);
    return this->mCells[x * this->mYBorder + y];
}

//////////////////////////////////////////////////
std::vector<Cell> & Map::_su_GetCells(void)
{
    return this->mCells;
}

//////////////////////////////////////////////////
bool Map::IsInMap(unsigned int x, unsigned int y) const
{
    return (x >= 0 &&
            x < mXBorder &&
            y >= 0 &&
            y < mYBorder);
}

//////////////////////////////////////////////////
unsigned int Map::GetXBorder(void) const
{
    return mXBorder;
}

//////////////////////////////////////////////////
unsigned int Map::GetYBorder(void) const
{
    return mYBorder;
}

//////////////////////////////////////////////////
void Map::LinkCells()
{
    for (unsigned int i = 0; i < mXBorder; i++)
    {
        for (unsigned int j = 0; j < mYBorder; j++)
        {
            Cell & cellxy = TheCell(i, j);
            cellxy._su_SetCellUp    ((j==mYBorder-1)    ? NULL : &TheCell(i,  j+1));
            cellxy._su_SetCellDown  ((j==0)             ? NULL : &TheCell(i,  j-1));
            cellxy._su_SetCellLeft  ((i==0)             ? NULL : &TheCell(i-1,  j));
            cellxy._su_SetCellRight ((i==mXBorder-1)    ? NULL : &TheCell(i+1,  j));
            cellxy._su_SetX(i);
            cellxy._su_SetY(j);
        }
    }
}



#ifdef CONSOLE
void Map::Print(void) const
{
    std::cout << '\n';
    for (unsigned int i = 0; i < mYBorder; i++)
    {
        for (unsigned int j = 0; j < mXBorder; j++)
        {
            GetCell(j,i).Print();
        }
        std::cout << '\n';
    }
}
#endif

//////////////////////////////////////////////////
void Map::Save(char *filename) const
{
    std::ofstream of;
    of.open(filename);
    of << this->GetXBorder() << std::endl
        << this->GetYBorder() << std::endl;
    for (unsigned int i=0; i<this->GetXBorder(); i++)
    {
        for (unsigned int j=0; j<this->GetYBorder(); j++)
        {
            of << this->GetCell(i,j).GetType() << std::endl;
        }
    }
}

//////////////////////////////////////////////////
void Map::Open(char *filename)
{
    std::ifstream inf;
    inf.open(filename);
    inf >> this->mXBorder
        >> this->mYBorder;
    this->mCells.clear();
    this->mCells.assign(this->mXBorder * this->mYBorder, Cell(this));
    this->LinkCells();

    for (unsigned int i=0; i<this->GetXBorder(); i++)
    {
        for (unsigned int j=0; j<this->GetYBorder(); j++)
        {
            unsigned int tmp;
            inf >> tmp;
            this->TheCell(i,j)._su_SetType(tmp);
        }
    }
}

/////////////////////////////////////////////////////////
void Map::Reset(char *filename)
{
    this->Open(filename);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -