Compare commits
3 Commits
aaec7bb5db
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| d5531cb2fc | |||
| a9d62a1ff5 | |||
| 266d23e620 |
@@ -0,0 +1,4 @@
|
|||||||
|
### 困难部分第 30 题
|
||||||
|
此题考查继承与多态,我只需要按题目去定义即可。为了方便在列表中创建实例,加了`__init__`方法。Python 定义抽象类需要来自模块 abc 的装饰器 @abstractmethod,能够让 Pylance 不发出协变不兼容警告,此处为了简便没有使用
|
||||||
|
|
||||||
|
[回到主项目 README](../README.md)
|
||||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
+11
-3
@@ -3,12 +3,20 @@ class Shape:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
class Circle(Shape):
|
class Circle(Shape):
|
||||||
|
def __init__(self, r):
|
||||||
|
self.r = r
|
||||||
|
|
||||||
def area(self):
|
def area(self):
|
||||||
return 3.14 * r * r
|
return 3.14 * self.r * self.r
|
||||||
|
|
||||||
class Square(Shape):
|
class Square(Shape):
|
||||||
|
def __init__(self, side):
|
||||||
|
self.side = side
|
||||||
|
|
||||||
def area(self):
|
def area(self):
|
||||||
return side * side
|
return self.side * self.side
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
shapes = [Circle(3), Square(5)]
|
||||||
|
for s in shapes:
|
||||||
|
print(s.area())
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
enum class TileType{
|
||||||
|
EMPTY,
|
||||||
|
WALL,
|
||||||
|
NOMAL_BEAN,
|
||||||
|
ENERGY_BEAN,
|
||||||
|
PACMAN,
|
||||||
|
GHOST
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Tile{
|
||||||
|
TileType type;
|
||||||
|
std::unordered_map<TileType, std::string> tileTypeToPlaceholder{
|
||||||
|
{TileType::EMPTY, " "},
|
||||||
|
{TileType::WALL, "#"},
|
||||||
|
{TileType::NOMAL_BEAN, "."},
|
||||||
|
{TileType::ENERGY_BEAN, "o"},
|
||||||
|
{TileType::PACMAN, "P"},
|
||||||
|
{TileType::GHOST, "G"}
|
||||||
|
};
|
||||||
|
|
||||||
|
Tile() = default;
|
||||||
|
Tile(TileType t) : type(t) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class MapGrid{
|
||||||
|
public:
|
||||||
|
MapGrid() = default;
|
||||||
|
|
||||||
|
MapGrid(const std::string& mapFilePath);
|
||||||
|
|
||||||
|
void loadMap(const std::string& mapFilePath);;
|
||||||
|
|
||||||
|
std::unique_ptr<std::pair<int, int>> getSize() const;
|
||||||
|
|
||||||
|
bool isValidPosition(int x, int y) const;
|
||||||
|
|
||||||
|
void setTile(int x, int y, TileType type, const std::string& placeholder);
|
||||||
|
|
||||||
|
void renderMap() const;
|
||||||
|
|
||||||
|
void move(); // 应该是 W A S D 移动
|
||||||
|
private:
|
||||||
|
std::vector<std::vector<Tile>> grid;
|
||||||
|
std::unique_ptr<std::pair<int, int>> size;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user