Untitled
動unknown
plain_text
2 years ago
3.9 kB
8
Indexable
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
class Map;
class character
{
protected:
//名字、血量、攻擊、防禦、速度
char symbol;
string name;
int healthPoints;
int attackPoints;
int defensePoints;
int speedPoints;
int x; // 角色在方格地圖上的x、y座標
int y;
public:
//Constructor
character(const char charSymbol, const string& charName, int charHP, int charAttack, int charDefense, int charSpeed, int x, int y) : symbol(charSymbol), name(charName), healthPoints(charHP), attackPoints(charAttack), defensePoints(charDefense), speedPoints(charSpeed), x(x), y(y) {}
//取得基本資料函數
char getSymbol() const { return symbol; }
string getName() const { return name; }
int getHealthPoints() const { return healthPoints; }
int getAttackPoints() const { return attackPoints; }
int getDefensePoints() const { return defensePoints; }
int getSpeedPoints() const { return speedPoints; }
int getX() const { return x; }
int getY() const { return y; }
//顯示基本資料
void displayInfo() const {
cout << "Name: " << name << endl;
cout << "HP: " << healthPoints << endl;
cout << "Attack: " << attackPoints << endl;
cout << "Defence: " << defensePoints << endl;
}
//傷害
void takeDamage(int damage) {
healthPoints -= damage;
if (healthPoints < 0) {
healthPoints = 0;
cout << name << " 被擊敗了!" << endl;
}
else {
cout << name << " 受到 " << damage << " 點傷害,剩餘生命值:" << healthPoints << endl;
}
}
// 移動角色
void move(Map& chessBoard);
};
class Map {
private:
int rows; // 地圖的行數
int cols; // 地圖的列數
vector<std::vector<char>> map; // 地圖的表示
public:
Map(int rows, int cols)
: rows(rows), cols(cols), map(rows, vector<char>(cols, '.')) {}
void draw() const {
for (const auto& row : map) {
for (char cell : row) {
std::cout << cell << ' ';
}
cout << endl;
}
}
void placeCharacter(const character& character) {
int row = character.getY();
int col = character.getX();
// 檢查座標是否合法
if (row >= 0 && row < rows && col >= 0 && col < cols) {
map[row][col] = character.getSymbol();
}
}
void cleanCharacter(const character& character) {
int row = character.getY();
int col = character.getX();
// 檢查座標是否合法
if (row >= 0 && row < rows && col >= 0 && col < cols) {
map[row][col] = ' ';
}
}
};
void character::move(Map& chessBoard) {
int moveX = 0;
int moveY = 0;
chessBoard.cleanCharacter( *this); // 將原先位置設為空格
cout << "Your Move(Enter your movement): " << endl;
cin >> moveX >> moveY;
x += moveX;
y += moveY;
chessBoard.placeCharacter(*this); // 更新新位置
cout << x << " " << y << endl;
}
int main() {
// 初始化地圖
Map chessBoard(8, 8);
// 初始化玩家
character wizard('W', "Wizard", 100, 100, 100, 100, 5, 0);
// 放置玩家到地圖上
chessBoard.placeCharacter(wizard);
// 繪製初始地圖
chessBoard.draw();
wizard.displayInfo();
// 移動玩家
//chessBoard.moveCharacter(player, 5, 5);
wizard.move(chessBoard);
//chessBoard.placeCharacter(wizard);
// 繪製移動後的地圖
cout << "Chess Board after moving player:" << std::endl;
chessBoard.draw();
return 0;
}
Editor is loading...
Leave a Comment