lundi 24 octobre 2016

generating items on a grid randomly

Hi i am trying to generate treasures(denoted by X) on a grid using threads. My activity is supposed to generate the treasures every 3 seconds but i am having trouble generating more than one.

heres what i have:

import android.os.Handler;
import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends Activity {

GridView gridView;

private static int w=5,curx,cury;

private Random r = new Random();

private Random x = new Random();

static String[] numbers = new String[w*w];

private TextView countTextView;

private Integer count; // count treasures collected
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    Thread thread = new Thread (countNumbers);
    //thread.start();

    gridView = (GridView) findViewById(R.id.gridView1);
    countTextView = (TextView) findViewById(R.id.textView);
    for(int i=0;i<numbers.length;i++) numbers[i]=" ";
    curx=r.nextInt(w);
    cury=r.nextInt(w);
    numbers[cury*w+curx]="0";

    count = 0;
    thread.start();




    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.list_item, numbers);

    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(getApplicationContext(),
                    (CharSequence) (new Integer(position).toString()),
                    Toast.LENGTH_SHORT).show();
        }
    });




}

public void reset(View view){
    init();
}

void init(){
    for(int i=0;i<numbers.length;i++) numbers[i]=" ";
    curx=r.nextInt(w);
    cury=r.nextInt(w);
        numbers[cury * w + curx] = "O";
    ((ArrayAdapter)gridView.getAdapter()).notifyDataSetChanged();

}

public Handler threadHandler = new Handler() {
    public void handleMessage (android.os.Message message){
        countTextView.setText(count.toString());
    }

};
/* this method generates the treasures */
private Runnable countNumbers = new Runnable() {

    private static final int DELAY = 3000;
    int i = 0;
    public void run() {
        try {
            while (i < numbers.length) {
                if (numbers[cury*w+curx] <=  )
                count++;
                i++;
                curx=r.nextInt(w);
                cury=r.nextInt(w);
                numbers[cury*w+curx]="X";
                Thread.sleep(DELAY);
                threadHandler.sendEmptyMessage(0);
            }
        } catch (InterruptedException e)

        {
            e.printStackTrace();
        }
    }
};


}

this part handles the random generator for treasures

    private Runnable countNumbers = new Runnable() {

    private static final int DELAY = 3000;
    int i = 0;
    public void run() {
        try {
            while (i < numbers.length) {
                if (numbers[cury*w+curx] <=  )
                count++;
                i++;
                curx=r.nextInt(w);
                cury=r.nextInt(w);
                numbers[cury*w+curx]="X";
                Thread.sleep(DELAY);
                threadHandler.sendEmptyMessage(0);
            }
        } catch (InterruptedException e)

        {
            e.printStackTrace();
        }
    }
};

Any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire