インスパイア元 -> オブジェクトじゃんけん
の更にネタ元 -> 勝手にじゃんけん祭り
やっぱC++っつったらtemplateですよねー
#include <iostream>
#include <string>
class Hand
{
public:
virtual ~Hand() {}
std::string
to_string() const
{ return _to_string(); }
private:
virtual std::string
_to_string() const = 0;
};
std::ostream&
operator<<(
std::ostream& os,
const Hand& h
)
{ return os << h.to_string(); }
class Scissors
: public Hand
{
std::string
_to_string() const
{ return "ちょき"; }
};
class Stone
: public Hand
{
std::string
_to_string() const
{ return "ぐぅ"; }
};
class Paper
: public Hand
{
std::string
_to_string() const
{ return "くぱぁ"; }
};
class Result
{
public:
virtual ~Result() {}
std::string
to_string() const
{ return _to_string(); }
private:
virtual std::string
_to_string() const = 0;
};
std::ostream&
operator<<(
std::ostream& os,
const Result& r
)
{ return os << r.to_string(); }
template<class WINNER>
class Win
: public Result
{
std::string
_to_string() const
{ return WINNER().to_string() + "の勝ち"; }
};
class Even
: public Result
{
std::string
_to_string() const
{ return "あいこ"; }
};
template<class L, class R>
struct Judge
{ typedef Even Message; };
template<>
struct Judge<Scissors, Stone>
{ typedef Win<Stone> Message; };
template<>
struct Judge<Scissors, Paper>
{ typedef Win<Scissors> Message; };
template<>
struct Judge<Stone, Scissors>
{ typedef Win<Stone> Message; };
template<>
struct Judge<Stone, Paper>
{ typedef Win<Paper> Message; };
template<>
struct Judge<Paper, Stone>
{ typedef Win<Paper> Message; };
template<>
struct Judge<Paper, Scissors>
{ typedef Win<Scissors> Message; };
template<class L, class R>
class Match
{ };
template<class L, class R>
std::ostream&
operator<<(
std::ostream& os,
Match<L, R> m
)
{
return os <<
L().to_string() <<
" 対 " <<
R().to_string() <<
" = " <<
typename Judge<L,R>::Message();
}
int main()
{
std::cout << Match<Stone, Stone>() << "\n"
<< Match<Stone, Scissors>() << "\n"
<< Match<Stone, Paper>() << "\n"
<< Match<Scissors, Stone>() << "\n"
<< Match<Scissors, Scissors>() << "\n"
<< Match<Scissors, Paper>() << "\n"
<< Match<Paper, Stone>() << "\n"
<< Match<Paper, Scissors>() << "\n"
<< Match<Paper, Paper>() << "\n";
return 0;
}
え?ランダム?思考ルーチン?? ナニソレオイシイノ? (´ー`y-~
16:25追記 Stone vs Paper で Stoneが勝っちゃってたので修正。 orz




コメントする