반응형
가위 바위 보 게임
클래스 함수 포인터를 활용하여 가위 바위 보 게임입니다.
[game.h Code]
class C_GAME
{
public:
enum class E_TYPE
{
E_NONE,
E_SCISSORS,
E_ROCK,
E_PAPRE,
E_MAX,
};
private:
E_TYPE m_ePlayer;
E_TYPE m_eComputer;
std::string m_arResult[(int)E_TYPE::E_MAX][(int)E_TYPE::E_MAX];
void (C_GAME::*m_pPrintType[(int)E_TYPE::E_MAX])();
public:
C_GAME();
void init();
void printResult();
void selectPlayer(C_GAME::E_TYPE eType);
void selectComputer();
private:
int getRandom(int nMin, int nRange);
void printNone();
void printScissors();
void printRock();
void printParer();
};
[game.cpp Code]
C_GAME::C_GAME() : m_eComputer(E_TYPE::E_NONE), m_ePlayer(E_TYPE::E_NONE), m_arResult{}, m_pPrintType{} {}
void C_GAME::init()
{
m_pPrintType[(int)C_GAME::E_TYPE::E_NONE] = &C_GAME::printNone;
m_pPrintType[(int)C_GAME::E_TYPE::E_SCISSORS] = &C_GAME::printScissors;
m_pPrintType[(int)C_GAME::E_TYPE::E_ROCK] = &C_GAME::printRock;
m_pPrintType[(int)C_GAME::E_TYPE::E_PAPRE] = &C_GAME::printParer;
for (int i = 0; i < (int)E_TYPE::E_MAX; i++)
{
m_arResult[0][i] = "준비 안됨";
m_arResult[i][0] = "준비 안됨";
}
m_arResult[(int)E_TYPE::E_SCISSORS][(int)E_TYPE::E_SCISSORS] = "무승부";
m_arResult[(int)E_TYPE::E_SCISSORS][(int)E_TYPE::E_ROCK] = "패배";
m_arResult[(int)E_TYPE::E_SCISSORS][(int)E_TYPE::E_PAPRE] = "승리";
m_arResult[(int)E_TYPE::E_ROCK][(int)E_TYPE::E_SCISSORS] = "승리";
m_arResult[(int)E_TYPE::E_ROCK][(int)E_TYPE::E_ROCK] = "무승부";
m_arResult[(int)E_TYPE::E_ROCK][(int)E_TYPE::E_PAPRE] = "패배";
m_arResult[(int)E_TYPE::E_PAPRE][(int)E_TYPE::E_SCISSORS] = "패배";
m_arResult[(int)E_TYPE::E_PAPRE][(int)E_TYPE::E_ROCK] = "승리";
m_arResult[(int)E_TYPE::E_PAPRE][(int)E_TYPE::E_PAPRE] = "무승부";
}
void C_GAME::printResult()
{
printf("Player : ");
(this->*m_pPrintType[(int)m_ePlayer])();
printf("\n");
printf("Computer : ");
(this->*m_pPrintType[(int)m_eComputer])();
printf("\n");
printf("결과 : ");
printf("%s \n", m_arResult[(int)m_ePlayer][(int)m_eComputer].c_str());
}
void C_GAME::selectPlayer(C_GAME::E_TYPE eType)
{
m_ePlayer = eType;
}
void C_GAME::selectComputer()
{
m_eComputer = (E_TYPE)getRandom(1, 3);
}
int C_GAME::getRandom(int nMin, int nRange)
{
unsigned int uNumber = 0U;
int nNumber = 0;
rand_s(&uNumber);
nNumber = (int)((unsigned int)((double)uNumber / ((double)UINT_MAX + 1.0) * nRange) + nMin);
return nNumber;
}
void C_GAME::printNone()
{
printf("준비 안됨");
}
void C_GAME::printScissors()
{
printf("가위");
}
void C_GAME::printRock()
{
printf("바위");
}
void C_GAME::printParer()
{
printf("보");
}
[main Code]
int main()
{
C_GAME cGame;
cGame.init();
cGame.selectPlayer(C_GAME::E_TYPE::E_PAPRE);
cGame.selectComputer();
cGame.printResult();
return 0;
}
반응형
'C++' 카테고리의 다른 글
[C++] std::string (0) | 2018.03.18 |
---|---|
[C++]랜덤함수 rand_s() 함수 (0) | 2018.03.11 |
[C++]함수 템플릿, 클래스 템플릿 (0) | 2018.03.05 |
[C++]함수 포인터 와 Class (0) | 2018.02.28 |
[C++]static 정적 변수와 Class (0) | 2018.02.27 |