次の列挙型がcode.cppで宣言されており、列挙型が何として設定されているかに依存するswitchステートメントを使用しています。
enum State { HighMoral, PoorMoral, EndGame };
State CurrentState = HighMoral;
switch (CurrentState)
{
case HighMoral: random = rand()%3+1;
switch (random)
{
case 1: CurrentState = g_Solider.KillCommander(&CurrentState, random = rand()%2+1);
break;
case 2: CurrentState = g_Solider.KillSeniorCommander(&CurrentState,random = rand()%2+1);
break;
case 3: CurrentState = g_Solider.LessHalfStrength(&CurrentState);
break;
};
break;
case PoorMoral: CurrentState = g_Solider.JoinSeniorCommander();
break;
};
この列挙型をクラス内の関数に渡してからHighMoral
、PoorMoral
またはのいずれかを返しEndGame
、switchステートメントの現在の状態を変更したいと思います。
しかし、これを渡して返すことに関しては、私はかなり無知です。私は周りを見回しましたが、これを行う方法を見つけることができませんでした。
私は3つのファイルを持っています。code.cpp(void main()
とを含むenum
)、solider.h(soliderクラスを含むが状態列挙型が存在することを知らない(これを行う方法は?))、solider.cpp(すべてのsoliderコードを含むが、現在の状態を取得して、新しい状態)
これが私がやろうとしていることの例です。
Solider.h
#include <time.h>
#include <iostream>
using namespace std;
extern enum State;
class Solider
{
private:
public:
void KillSeniorCommander(State& currentState, int random); // Kill the Senior Commander or random event
void JoinSeniorCommander(State& currentState); // Have the Commander join the group
void DefunctGroup(State& currentState); // Disband the group
};
Solider.cpp
void Solider::KillSeniorCommander(State& currentState, int random)
{
if (SeniorCommander==1) // If The SeniorCommander is Active
{
cout << "The Senior Commander has died!\n";
SeniorCommander--; // Kill the SeniorCommander
Groupsize--; // Reduce the Groupsize
Strength = Strength - (5*2.5); // Remove SeniorCommanders impact on Strength
SquadMoral = SquadMoral - (5*2.5);// Remove SeniorCommanders impact on SquadMoral
CurrentState = PoorMoral;
}
else // Otherwise do something random
{
switch (random)
{
case 1: cout << "Your group survives a ambush!\n";
break;
case 2: random = rand()%5+1; // Give random a new value
if (random>1)
{
cout << random << " group members have died!\n"; // Kill x Aamount of members
}
else
{
cout << "A group member has died!\n"; // Kill a member
}
Groupsize = Groupsize - random; // Remove the members from the group
Strength = Strength - (random*2.5); // Remove there effect Strength
SquadMoral = SquadMoral - (random*2.5); // Remove there effect on GroupMoral
break;
}
CurrentState = CurrentState;
}
} // KillSeniorCommander(int random)
void Solider::JoinSeniorCommander(State& currentState)
{
if (SeniorCommander==2 && Commander == 0) // Check to see if the Commander is dead and a
{ // SeniorCommander is not in service
cout << "The Senior Commander has joined!\n";
SeniorCommander--; // Change their status to active
Groupsize++; // Add them to the group
Strength = Strength - (5*2.5); // Add their impact to Strength
SquadMoral = SquadMoral - (5*2.5); // Add their impact to GroupMoral
CurrentState = HighMoral;
}
else // He isn't available to join
{
cout << "You fail to recruit new command!\n";
CurrentState = CurrentState;
}
} // JoinSeniorCommander()
void Solider::DefunctGroup(State& currentState)
{
cout << "Your group has been disbanded as it is not fit for duty.";
CurrentState = EndGame;
} // DefunctGroup()
code.cpp