撲克牌遊戲01 BasicCard類

首先定義一個 BasicCard類用於存放單張撲克牌,撲克牌中有三個屬性,“花色”,“數字”,“是否被選爲手牌”;考慮到“是否手牌”這個選項會在其他類裏面判斷,所以,暫時講其設置爲public屬性。

這是BasicCard.h文件

#pragma once
#include <string>
using namespace std;
/*
	定義單張卡牌
*/
class BasicCard
{
private:
	//花色
	string decors = "";
	//數字
	string number = "";
	//此卡牌是否手牌
public:
	bool isChoosen = false;
	//存入卡牌
	bool set_card(string decors, string number);
	//將卡牌改爲手牌
	bool get_card();
	//展示(打印)卡牌
	void view_card();
	BasicCard(string decors, string number);
	~BasicCard();
};

這是對BasicCard.h的具體實現,BasicCard.cpp

 

#include "BasicCard.h"
#include <iostream>

using namespace std;

//存入卡牌
bool BasicCard::set_card(string decors, string number)
{
    try
    {
        this->decors = decors;
        this->number = number;
    }
    catch (exception& e)
    {
        cout << "Standard exception: " << e.what() << endl;
    }
	return true;
}

//將卡牌改爲手牌
bool BasicCard::get_card()
{
    try
    {
        this->isChoosen = true;
    }
    catch (exception& e)
    {
        cout << "Standard exception: " << e.what() << endl;
    }
    return true;
}

//展示(打印)卡牌
void BasicCard::view_card()
{
    cout << this->decors << this->number << "\t";
}

BasicCard::BasicCard(string decors, string number)
{
    set_card(decors, number);
}

BasicCard::~BasicCard()
{
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章