DesignPattern/21.StatePattern/2.Code/GameAccount.cpp

78 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "GameAccount.h"
#include "Level.h"
#include <Windows.h>
#include <time.h>
#define random(x) (rand()%x)
GameAccount::GameAccount(){
level = nullptr;
printf("创立游戏角色积分100级别PRIMARY\n");
score = 100;
name = "none";
setLevel(new Primary(this));
}
GameAccount::GameAccount(string iName){
level = nullptr;
printf("创立游戏角色积分100级别PRIMARY\n");
score = 100;
name = iName;
setLevel(new Primary(this));
}
GameAccount::~GameAccount(){
if(level){
delete level;
level = nullptr;
}
}
void GameAccount::setLevel(Level* iLevel){
if(level != nullptr){
delete level;
level = nullptr;
}
this->level = iLevel;
}
string GameAccount::getName(){
return name;
}
void GameAccount::playCard(){
this->level->playCard();
Sleep(100);
srand((int)time(0));
int res = random(2);
if (res % 2 == 0){
this->win();
}
else{
this->lose();
}
this->level->upgradeLevel();
}
void GameAccount::win(){
if (this->getScore() < 200){
setScore(getScore() + 50);
}
else{
setScore(getScore() + 100);
}
printf("\n\t胜利,最新积分为 %d\n", score);
}
void GameAccount::lose(){
setScore(getScore() + 30);
printf("\n\t输牌,最新积分为 %d\n", score);
}
int GameAccount::getScore(){
return this->score;
}
void GameAccount::setScore(int iScore){
this->score = iScore;
}