Code ... using ***Collection.suffle();***

So Last Post
we have saw that about Collection.suffle(); about it.

we have also saw that about how its work.
now it's work on eclipse android and how to arrange number in button Label to randomly arranging one by one.

so that you have work need to work with android application need java file and xml file.
android.xml file need to set layout your structure.
& .java file is your coding file which you have generate code of java.

so that


public class Demo extends Activity{

Button buttons[];
private static final Integer[] goal = new Integer[] {0,1,2,3,4,5,6,7,8};
   
private ArrayList<Integer> cells = new ArrayList<Integer>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamepage);
        buttons=findbutton();
     
       /* for(int i=0;i<9;i++)
        {
            this.cells.add(i);
        }
        Collections.shuffle(this.cells);
     fill_grid();
*/
        //ArrayList<Integer> al = new ArrayList<Integer>();
for(int i=0;i<9;i++)
cells.add(i);

Collections.shuffle(cells);
fill_grid();
    }
 
    /*here fill_grid() is arrange a generate randomly number on the buttons..so that look like above image. it's contain a absolutelayout that manage your layout so that use and x,y point it use managing position.*/

private void fill_grid() {
// TODO Auto-generated method stub
        for(int i=1;i<9;i++){
int text = cells.get(i);
AbsoluteLayout.LayoutParams absParams =
   (AbsoluteLayout.LayoutParams)buttons[text].getLayoutParams();

switch(i){

case(0):

absParams.x = 5;
absParams.y = 5;
buttons[text].setLayoutParams(absParams);
break;
case(1):

absParams.x = 110;
absParams.y = 5;
buttons[text].setLayoutParams(absParams);
break;
case(2):

absParams.x = 215;
absParams.y = 5;
buttons[text].setLayoutParams(absParams);
break;
case(3):

absParams.x = 5;
absParams.y = 110;
buttons[text].setLayoutParams(absParams);
break;
case(4):

absParams.x =110;
absParams.y =110;
buttons[text].setLayoutParams(absParams);
break;
case(5):

absParams.x = 215;
absParams.y =110;
buttons[text].setLayoutParams(absParams);
break;
case(6):

absParams.x = 5;
absParams.y = 215;
buttons[text].setLayoutParams(absParams);
break;
case(7):

absParams.x = 110;
absParams.y = 215;
buttons[text].setLayoutParams(absParams);
break;
case(8):

absParams.x = 215;
absParams.y = 215;
buttons[text].setLayoutParams(absParams);
break;
}
}

}

private Button[] findbutton() {
// TODO Auto-generated method stub
Button[] b = new Button[9];

b[0] = (Button) findViewById(R.id.Button00);
b[1] = (Button) findViewById(R.id.Button01);
b[2] = (Button) findViewById(R.id.Button02);
b[3] = (Button) findViewById(R.id.Button03);
b[4] = (Button) findViewById(R.id.Button04);
b[5] = (Button) findViewById(R.id.Button05);
b[6] = (Button) findViewById(R.id.Button06);
b[7] = (Button) findViewById(R.id.Button07);
b[8] = (Button) findViewById(R.id.Button08);
return b;
}

}

Comments