私はボードゲームアプリ(チェスに似ています)に取り組んでいます。GridView のクリックをリッスンするアクティビティ GameBoardActivity があり、クリックするたびにクラス Game から関数を呼び出して、何が発生するかを処理します。
クラス Game 内には、駒のMove(int xFrom, int yFrom, int xTo, int yTo)
場所と駒の移動方法に関するデータがあります。
ユーザーが指定できる特定の動き (たとえば、xFrom、yFrom のピースが xTo、yTo に移動する必要がある) について、2 つのオプションから選択できるようにしたいと考えています。ひとつは普通にそこに行くこと、もうひとつは変形した駒としてそこに行くことだと想像できます。これを行うには、ユーザーがクリックする 2 つの選択肢を提示するカスタム ダイアログを表示します。
私のカスタム Dialog クラスを以下に示します。
public class CustomDialog extends Dialog implements View.OnClickListener{
Context mcontext;
Button button1;
Button button2;
int choice; //holds value of user's choice
public CustomDialog(Context context) {
super(context);
mcontext = context;
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
choice = 0; //no choice yet
}
public void setLayout(){
this.setContentView(R.layout.custom_dialog);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
choice = 1;
break;
case R.id.button2:
choice = 2;
break;
}
dismiss();
}
}
私がはっきりしていないのは、ユーザーの選択に関する情報をクラス Game に戻す方法です。
どんな助けでも大歓迎です。