Androidで3連単フォーメーション馬券

今回はAndroidでフォーメーション馬券の組み合わせ計算機を作った。

JRAのフォーメーションマークシートをイメージ

プログラム作成

以下の場所のみ説明

  • 定数、メンバ変数の定義
  • Createメソッドの作成
  • 画面レイアウト構成
  • マークシートテーブルの作成

定数、メンバ変数の定義

    //ボタンサイズ
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT; 
    private final int WIDTH = WC;
    private final int HEIGHT = FP;
    
    //出走馬数
    private final int MAX = 10;
    //画面の改行番号 11番以降は2行目になる
    private final int WRAP_CNT = 10;
    
    //色 選択した番号は緑
    private final int CHECK_COLOR = Color.GREEN;
    private final int UNCHECK_COLOR = Color.WHITE;

    //1着〜3着のそれぞれの配列
    private List<CheckButton> firstCheckList;
    private List<CheckButton> secondCheckList;
    private List<CheckButton> thirdCheckList;
    
    //組み合わせ数
    private TextView result;

内部クラスCheckButtonの作成

1着〜3着配列に格納させる。CheckButtonクラスを作成する
CheckButton

private boolean check;
private int hourseNo;	//今回は使わないが後のカスタマイズ対応として作成
public int getHourseNo() {
	return hourseNo;
}
public void setHourseNo(int hourseNo) {
	this.hourseNo = hourseNo;
}
public CheckButton(Context context) {
	super(context);
	// TODO Auto-generated constructor stub
}
public void setCheck(boolean check) {
	this.check = check;
}
public boolean isCheck() {
	return check;
}

onCreateメソッドの作成
今回は、CheckButtonをfor文を使って作成するので、xml定義ファイルは作らない。
xmlファイルに18 * 3個も定義したくないし

画面レイアウト構成

ScrollView sv = new ScrollView(this);
setContentView(sv);

//枠
LinearLayout outlinearLayout = new LinearLayout(this);
outlinearLayout.setOrientation(LinearLayout.VERTICAL);

//組み合わせレイアウト
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);

//馬番レイアウト
TableLayout tabelLayout = new TableLayout(this);


マークシートテーブル
各着順のテーブル構成
各着につき3行分必要
各行内容

表示文字 変数名
1行目 着順 StringRow
2行目 馬番 1〜10 UpperNoRow
3行目 馬番 11〜18 LowerNoRow

各着順の行をそれぞれ定義

TableRow firstStringRow = new TableRow(this);
TableRow firstUpperNoRow = new TableRow(this);
TableRow firstLowerNoRow = new TableRow(this);

Viewを作成

tabelLayout.addView(firstStringRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
this.firstCheckList = new ArrayList<CheckButton>();
createTargetSet(1, this.firstCheckList, firstStringRow, firstUpperNoRow, firstLowerNoRow);
tabelLayout.addView(firstUpperNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
tabelLayout.addView(firstLowerNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));

ソース全体

package formation.hourse;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FormationHourse extends Activity implements OnClickListener{
    
	//ボタンサイズ
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT; 
    private final int WIDTH = WC;
    private final int HEIGHT = FP;
    
    //出走馬数
    private final int MAX = 10;
    //画面の改行番号 11番以降は2行目になる
    private final int WRAP_CNT = 10;
    
    //色 選択した番号は緑
    private final int CHECK_COLOR = Color.GREEN;
    private final int UNCHECK_COLOR = Color.WHITE;

    //1着〜3着のそれぞれの配列
    private List<CheckButton> firstCheckList;
    private List<CheckButton> secondCheckList;
    private List<CheckButton> thirdCheckList;
    
    //組み合わせ数
    private TextView result;
	
    /**
     * CheckButton
     * 
     * checkと馬番のプロパティを追加したButtonクラスの派生クラス
     * @author aki2
     *
     */
    class CheckButton extends Button{
    	private boolean check;
    	private int hourseNo;	//今回は使わないが後のカスタマイズ対応として作成
		public int getHourseNo() {
			return hourseNo;
		}
		public void setHourseNo(int hourseNo) {
			this.hourseNo = hourseNo;
		}
		public CheckButton(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}
		public void setCheck(boolean check) {
			this.check = check;
		}
		public boolean isCheck() {
			return check;
		}
    }
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 
		ScrollView sv = new ScrollView(this);
		setContentView(sv);

		//枠
		LinearLayout outlinearLayout = new LinearLayout(this);
        outlinearLayout.setOrientation(LinearLayout.VERTICAL);

		//組み合わせレイアウト
		LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        //馬番レイアウト
		TableLayout tabelLayout = new TableLayout(this);

		//組み合わせText
		final Button calc = new Button(this);
		calc.setText("計算");
		calc.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	int cnt = 0;
            	cnt = calcFormation();
            	result.setText("" + cnt);
            }
        });
		
		TextView tv = new TextView(this);
		this.result = new TextView(this);
		tv.setText("通り");
		this.result.setText("0");
		linearLayout.addView(calc, new LinearLayout.LayoutParams(WIDTH, WIDTH));
		linearLayout.addView(this.result, new LinearLayout.LayoutParams(WIDTH, WIDTH));
		linearLayout.addView(tv, new LinearLayout.LayoutParams(WIDTH, WIDTH));
		
		
		//1着
		TableRow firstStringRow = new TableRow(this);
		TableRow firstUpperNoRow = new TableRow(this);
		TableRow firstLowerNoRow = new TableRow(this);
        
		//2着
		TableRow secondStringRow = new TableRow(this);
		TableRow secondUpperNoRow = new TableRow(this);
		TableRow secondLowerNoRow = new TableRow(this);

		//3着
		TableRow thirdStringRow = new TableRow(this);
		TableRow thirdUpperNoRow = new TableRow(this);
		TableRow thirdLowerNoRow = new TableRow(this);


		//1着
        tabelLayout.addView(firstStringRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
        this.firstCheckList = new ArrayList<CheckButton>();
        createTargetSet(1, this.firstCheckList, firstStringRow, firstUpperNoRow, firstLowerNoRow);
        tabelLayout.addView(firstUpperNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
        tabelLayout.addView(firstLowerNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
        
        //2着
        tabelLayout.addView(secondStringRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
        this.secondCheckList = new ArrayList<CheckButton>();
        createTargetSet(2, this.secondCheckList, secondStringRow, secondUpperNoRow, secondLowerNoRow);
        tabelLayout.addView(secondUpperNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
        tabelLayout.addView(secondLowerNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));

        //3着
        tabelLayout.addView(thirdStringRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
        this.thirdCheckList = new ArrayList<CheckButton>();
        createTargetSet(3, this.thirdCheckList, thirdStringRow, thirdUpperNoRow, thirdLowerNoRow);
        tabelLayout.addView(thirdUpperNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));
        tabelLayout.addView(thirdLowerNoRow, new TableLayout.LayoutParams(HEIGHT, WIDTH));

        
        outlinearLayout.addView(linearLayout);
        outlinearLayout.addView(tabelLayout);
        sv.addView(outlinearLayout);
    }
	
	/**
	 * n着の馬番行をまとめて作成
	 * @param no
	 * @param targetList
	 * @param stringNo
	 * @param upper
	 * @param lower
	 */
	private void createTargetSet(int no, List<CheckButton> targetList, TableRow stringNo, TableRow upper, TableRow lower){
		
		//着番行の作成
		TextView text = new TextView(this);
        text.setText(no + "着");
        stringNo.addView( text );

        //馬番配列の作成
        createCheckList(targetList);
        //馬番 行の作成
        createTargetRows(targetList, upper, lower);
        
	}
	
	/**
	 * 馬番行の作成
	 * @param targetList
	 * @param upper
	 * @param lower
	 */
	private void createTargetRows(List<CheckButton> targetList, TableRow upper, TableRow lower){
        int cnt = 1;
		for( Button c : targetList ){
        	c.setOnClickListener(this);
        	if( cnt <= WRAP_CNT ){
        		upper.addView( c );
        	}else{
        		lower.addView(c);
        	}
        	cnt++;
        }
	}
	
	/**
	 * 馬番配列の作成
	 */
	private void createCheckList(List<CheckButton> list){
		CheckButton tmpCheck;
        for( int i = 0; i < MAX; i++){
        	tmpCheck = new CheckButton(this);
        	
        	tmpCheck.setText("" + (i + 1));
        	tmpCheck.setHourseNo(i+1);
        	tmpCheck.setBackgroundColor(UNCHECK_COLOR);
        	list.add(tmpCheck);
        }
	}
	
	/**
	 * ボタンクリック
	 */
	@Override
	public void onClick(View arg0) {
		CheckButton b = (CheckButton)arg0;
		if( b.isCheck()){
			b.setBackgroundColor(UNCHECK_COLOR);
			b.setCheck(false);
		}else{
			b.setBackgroundColor(CHECK_COLOR);
			b.setCheck(true);
		}
	}

	/**
	 * フォーメーション組み合わせ計算
	 * @return
	 */
	private int calcFormation(){
		int cnt = 0;
		
		//1着、2着、3着にチェックがあることを確認
		for( CheckButton firstHourse : this.firstCheckList){
			//チェックあり
			if(firstHourse.isCheck()){
				for(CheckButton secondHourse : this.secondCheckList){
					
					//1着、2着の馬番が同じ
					if( secondHourse.getHourseNo() == firstHourse.getHourseNo()){
						continue;
					}
					
					//チェックあり
					if( secondHourse.isCheck()){
						for(CheckButton thirdHourse : this.thirdCheckList){
							//1着、2着、3着の馬番が同じ
							if( thirdHourse.getHourseNo() == firstHourse.getHourseNo()
								|| thirdHourse.getHourseNo() == secondHourse.getHourseNo()
							){
								continue;
							}
							
							if(thirdHourse.isCheck()){
								cnt++;
							}
						}//3着for
					}//2着チェックあり
				}//2着for
			}//1着チェックあり
		}//1着for
		
		//結果出力
		return cnt;
	}
}


追加したい機能

  • クリア機能
  • チェックしたら自動計算(計算ボタンの削除)
  • 全とおりチェック
  • スクロールなし
  • 3連複対応

※今回は出走馬数を10頭にしたが、11等以上の場合は画面をスクロールしないと全体表示できない

実行画面

18頭立てに設定