Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
8.4 kB
17
Indexable
Never
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
#include <cstdlib> 
#include <string> 
#include <stdexcept>

using namespace std;

class Team;
class Map;
class Obstacle; 
class character
{
protected:
    //名字、血量、攻擊、防禦、速度
    char symbol;
    string name;
    int current_healthPoints;
    int origin_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 originHP ,int charAttack, int charDefense, int charSpeed, int x, int y) : 
		symbol(charSymbol), name(charName), current_healthPoints(charHP), origin_healthPoints(originHP), attackPoints(charAttack), defensePoints(charDefense), speedPoints(charSpeed), x(x), y(y) {}

    //取得基本資料函數
    char getSymbol() const { return symbol; }
    string getName() const { return name; }
    int getHealthPoints() const { return current_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: " << current_healthPoints << endl;
        cout << "Attack: " << attackPoints << endl;
        cout << "Defence: " << defensePoints << endl;
    }

    //傷害
    void takeDamage(int damage) {
        current_healthPoints -= damage;
        if (current_healthPoints < 0) {
            current_healthPoints = 0;
            cout << name << " 被擊敗了!" << endl;
        }
        else {
            cout << name << " 受到攻擊 " << damage << " 點傷害,剩餘生命值:" << current_healthPoints << endl;
        }
    }
    
	void heal(int amount) {
		
		int old_healthPoints = current_healthPoints;
	    int new_healthPoints = current_healthPoints + amount;
	    // 确保生命值不超過最大值
	    current_healthPoints = min(new_healthPoints,origin_healthPoints);
	    cout << name << " 回复了 " << current_healthPoints-old_healthPoints << " 點生命值,當前生命值:" << current_healthPoints << endl;
	}
	    	

    // 移動角色
    void move(Map& chessBoard);
    void checkObstacle(vector<Obstacle>& allObstacles);
};

class Obstacle{
	private:
		char symbol;
		string name;
		int x;
		int y;
	public:
		Obstacle(const char symbol ,const string& name, int X , int Y) :  symbol(symbol), name(name), x(X), y(Y) {}; 
		char getSymbol() const { return symbol; }
		int getX() const {return x; }
		int getY() const {return y; }
		void applyEffect(character& someone);
};


class Map {
private :
    int rows;   // 地圖的行數
    int cols;   // 地圖的列數
    vector<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) {
                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();
        }else {
        	throw runtime_error("Character is out of map bounds");
    	}
    }

    void cleanCharacter(character& character) {
        int row = character.getY();
        int col = character.getX();

        // 檢查座標是否合法
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            map[row][col] = ' ';
        }
    }
    
	void placeObstacle(const Obstacle& obstacle  ) {
		int row = obstacle.getY();
		int col = obstacle.getX();
		
		// 檢查座標是否合法
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            map[row][col] = obstacle.getSymbol();
        }else {
        	throw runtime_error("the obstacle is out of map bounds");
    	}	
	} 
};


void character::move(Map& chessBoard) {
        int moveX = 0;  
        int moveY = 0;
        bool validMove = false;

		while(!validMove){
			
			try{
		        chessBoard.cleanCharacter( *this);  // 將原先位置設為空格
		        cout << "Your Move(Enter your movement): " << endl;
		        cin >> moveX >> moveY;
		        
				x += moveX;
        		y += moveY;
        		chessBoard.placeCharacter(*this);  // 嘗試放置到新位置
            	validMove = true;  // 如果成功,退出循環 
            	
        	} catch (const runtime_error& e) {
        		
            cout << "Error: " << e.what() << " Please enter again!" << endl;
            
            // 重置上一有效位置
            x -= moveX;
            y -= moveY;
        }
        cout << x << " " << y << endl;
    }
}

void character::checkObstacle(vector<Obstacle>& allObstacles){
	for (auto& obstacle : allObstacles) {
        int obstacleX = obstacle.getX();
        int obstacleY = obstacle.getY();

            // 檢查障礙物是否在九宮格
        if (x >= obstacleX - 1 && x <= obstacleX + 1 && y >= obstacleY - 1 && y <= obstacleY + 1){
                // 如果找到,調用障礙物的 applyEffect 方法
                obstacle.applyEffect(*this);
        }
    }
}

void Obstacle::applyEffect(character& someone){
    // 障礙物的效果邏輯 
    if (name == "supplement") {
        // 回血
        int healAmount = 10; 
        someone.heal(healAmount);
    }
    if(name == "damagement"){
    	int damageAmount = 20;
    	someone.takeDamage(damageAmount);
	}
}

class Team{
private:
    vector<character>** members;
    int membersCnt;
    bool teamMark;
public:
    Team(){
        members = new vector<character>*[6];
        teamMark = false;
    };
    ~Team(){
        delete[] members;
    }
    void setTeamMark(){teamMark = true; return;}
    addClassWarrior();
    addClassWizard();
    addClassArcher();
    addClassTank();
    addClassBishop();
    addClassKnight();
};

int main() {
    // 初始化地圖
    Map chessBoard(20, 20);

    //Create teams
    Team *playerOne = new Team();//toBdelete
    Team *playerTwo = new Team();//toBdelete
    playerTwo->setTeamMark();
    
    

    //compare two number to decide move order
    srand(time(NULL));
    int firstOrder = 0;
    
    firstOrder = 1 + rand() % 100;
    if(firstOrder > 50)
    {
        cout << "First order is Player 1" << endl;
    }
    else
    {
        cout << "First order is Player 2" << endl;
    }
    
    //初始化障礙與道具
	vector<Obstacle> All_obstacles = {
		Obstacle('+',"supplement",9, 8),//用rand函數寫 我覺得在放置的時候就可以刷新一次地圖讓玩家看補給品在哪了
		Obstacle('-',"damagement",13,5),
	};

    // 初始化玩家
        
    
    // 玩家的隊伍
    vector<character> team = {
        character('W', "Wizard", 100, 100 , 50, 30, 10, 5, 0),
        character('K', "Knight", 120,120 , 40, 25, 15, 7, 2),
        // Add more characters to the team...
    };

    // 放置隊伍進地圖
    for (const auto& character : team) {
        chessBoard.placeCharacter(character);
    }
    
    //放置道具進地圖 
    for (const auto&  Obstacle : All_obstacles ) {
        chessBoard.placeObstacle(Obstacle);
    }
    

    //遊戲剛開始時地圖
    cout << "Initial Chess Board:" << endl;
    chessBoard.draw();

    // Game loop (回合制)
    for (auto& character : team) {
        // 顯示角色基本資料
        character.displayInfo();

        // 移動角色
        character.move(chessBoard); 
		
		//檢查周圍的 Obstacle 
		character.checkObstacle(All_obstacles) ; 
		   
    }

    // 繪製移動後的地圖
    cout << "Chess Board after moving player:" << endl;
    chessBoard.draw();

    return 0;
}
Leave a Comment