ごあいさつ Stackoverflowians
私はAndroidカードゲームアプリに取り組んでいます。ゲームの特定の State にいる 2 人の Player のいずれかが異なるアクションを利用できることを考慮して、State Design Pattern (State Machine) を使用して実装した GameClass にゲーム全体のダイナミクスを組み込みました。
たとえば、ゲームの開始時に、プレーヤー A は、GameClass が持つ合計 21 個のメソッドのうち 5 個しか実行できません。どのアクションとカードがプレイされるかに応じて、メソッドはさまざまな状態に移行し、さまざまな状態ごとにさまざまなメソッドが利用可能になります。
したがって、各メソッドのブール値を設定および取得する CurrentActions というクラスを使用して、さまざまなメソッドの可用性を解決しました。したがって、プレイヤーがゲームの開始時に選択できるアクションをプレイヤーに示したい場合は、一連の IF (約 21...) を使用して、Getter のいずれかが true に設定されているかどうかを確認します。ボタンをレイアウトに追加すると、すべてが魅力的に機能します。
さて、私の最大の問題は、最初の状態のボタンを表示する方法がわからないことです。その後、プレーヤー A がそれらのボタン (メソッド) の 1 つをクリックすると、以前の状態で使用可能だったすべてのボタンがフラッシュされ、現在表示されています。ゲームの新しい状態の新しいボタン。
これを行う方法はありますか?私はすでに Layout.removeAllViews() メソッドを使用してすべてのボタンをクリアしていますが、次の State に従ってすべてのボタンを再度設定することが私の最大の問題です。この件に関する何らかのガイダンスをいただければ幸いです。
コードを表示したいというあなたのリクエストに応えて、ゲーム、レイアウト、ボタン、および可能なアクションを処理するコードをアクティビティに追加しました。
// CurrentActions CA holds the current actions that are available
//for the current State of the game.
final CurrentActions CA = new CurrentActions();
//JuegoTruco JT is the game itself
final JuegoTruco JT = new JuegoTruco(CA);
//I've changed the names up to make it understandable
//If Action1 is performable at the current State of the game, set the button up and
//await a posible Click
if(CA.performAction1())
{
Action1Button = new Button(this);
Action1Button.setId(13);
Action1Button.setLayoutParams(paramsActions);
Action1Button.setText("Action1");
layoutAcciones.addView(Action1Button);
Action1Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mpButton.start();
//Here I change the state of JT, by performing an action and
//I also give it instance CA, so the State Handler
//can change the available Actions for the following state
JT.performAction(otherParameters, CA);
layoutActions.removeAllViews();
}
}
});
}
if(CA.performAction2())
{
Action2Button = new Button(this);
Action2Button.setId(13);
Action2Button.setLayoutParams(paramsActions);
Action2Button.setText("Action2");
layoutAcciones.addView(Action2Buttonn);
Action2Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mpButton.start();
JT.performAction2(OtherParameters, CA);
layoutActions.removeAllViews();
}
}
});
}
ゲーム内に存在するすべてのアクションに対して、これらの IF の 1 つを用意します。だから....(ゲームの状態に応じて)利用可能なボタンの1つをクリックした後、actionsLayoutをクリアしてボタンの新しいセットを実行し、ボタンを押すたびにこれを継続的に実行する方法はありますか(つまり、状態が変化するたびに)?