ビンゴのアルゴリズムを変更しました

配列にあらかじめ1から75までいれておいて

シャッフルするようにしました

しかし、終了後に動作が不安定になる不具合があります^^;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class Bingo extends Activity
implements View.OnClickListener {

private static Random rand = new Random();
private static int array[] = new int[75];
private static int no = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn = (Button)findViewById(R.id.Button01);
btn.setOnClickListener(this);

for(int i=0; i<75; i++){
array[i] = i+1;
}
shuffle();

TextView textView1 = (TextView)findViewById(R.id.TextView01);
textView1.setText("");

TextView textView2 = (TextView)findViewById(R.id.TextView02);
textView2.setText("");

TextView textView3 = (TextView)findViewById(R.id.TextView03);
textView3.setText("");
}
public static void shuffle(){
int tmp;
for(int i=74; i>0; i--){
int t = rand.nextInt(i); //0~iの中から適当に選ぶ

//選ばれた値と交換する
tmp = array[i];
array[i] = array[t];
array[t] = tmp;
}
}

public void onClick(View v) {
if (no < 75) {
TextView textView = (TextView)findViewById(R.id.TextView01);
textView.setTextSize(50);
textView.setText(Integer.toString(array[no]));

String out = "";
for(int i=0; i<=no; i++){
String s = String.format("%1$02d",array[i]);
if ((i+1)%10 == 0) {
out = out + s + ",\n";
} else {
out = out + s + ",";
}
}
TextView textView2 = (TextView)findViewById(R.id.TextView02);
textView2.setText(out);

no++;
} else {
TextView textView3 = (TextView)findViewById(R.id.TextView03);
textView3.setText("\nfinish!");
}
}
}
適当に作ってみたけど、境界値あたりでバグがでてしまってます

作り替えますが、記録としてのこしておきます


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Random;

public class Bingo extends Activity
implements View.OnClickListener {

ArrayList array = new ArrayList();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn = (Button)findViewById(R.id.Button01);
btn.setOnClickListener(this);
}

public void onClick(View v) {
Random rand = new Random();

int value = rand.nextInt(75)+1;
for(int i=0; i if (array.get(i)== value && array.size() != 0) {
value = rand.nextInt(75)+1;
i = 0;
}
}

if (array.size() <= 75) {
array.add(value);
}

TextView textView = (TextView)findViewById(R.id.TextView01);
textView.setTextSize(50);
textView.setText(Integer.toString(value));

String out = "";
for(int i=0; i String s = String.format("%1$02d",array.get(i));
if ((i+1)%10 == 0) {
out = out + s + ",\n";
} else {
out = out + s + ",";
}
}
TextView textView2 = (TextView)findViewById(R.id.TextView02);
textView2.setText(out);
}
}
intの時

ArrayList array = new ArrayList();

array.add(value);

array.get(i);


ついでにrandomも

Random rand = new Random();

int value = rand.nextInt(75);
http://www.katch.ne.jp/~k_okada/

pngで作ってしまった画像の一括変換のソフトをさがしていて

ここを見つけました
現象:ボタンの押す位置がずれていた

原因:他の画面のボタンの同じくらいの位置のボタンが先に拾われていた

対策:必要なタイミングの時にしかその先に読まれていたボタンを読まないようにした
さて

今日から新しいブログを始めます

今がんばって作っている

androidアプリの防備録として使っていくつもりです

いい年なので、なんせ忘れっぽくて困ってます^^;

ということで、ぼちぼちとやっていきます