android - How do I randomly place/set/allocate items within a 2D Array? -
i’ve been trying create minesweeper game in android, , far, has gone accordingly. however, i’m stuck on part have randomly place mines within game board.
i’ve tried few things think of, none of worked, except one. however, doesn’t give me results want. here how drawing game board (using 2d array of buttons).
final button currentbutton = new button(this); final int bombstate = r.drawable.bomb_state; final button[][] buttonarray = new button[6][6]; final int mine = r.drawable.bomb; final random rand = new random(); final int number = 36; int button; int row; //create new linear layout relativelayout linearlayout = new relativelayout(this); //creating layout params layoutparams linlayoutparam = new layoutparams(layoutparams.fill_parent, layoutparams.fill_parent); //set layout background linearlayout.setbackgrounddrawable(getwallpaper()); //set linearlayout root element of screen setcontentview(linearlayout, linlayoutparam); //create new table layout game grid tablelayout minegrid = new tablelayout(this); /* * creates tablerows , buttons using loop * add buttons rows , rows * tablelayout */ for(row = 0; row < 6; row++){ //create new table row tablerow currentrow = new tablerow(this); for(button = 0; button < 6; button++){ //create new button for(int id = 0; id < number; id++){ currentbutton.setid(id); } currentbutton.settext(" "); //storing buttons array of buttons buttonarray[row][button] = currentbutton; if(currentbutton.isclickable()){ currentbutton.setonclicklistener(new onclicklistener() { /* * (non-javadoc) * @see android.view.view.onclicklistener#onclick(android.view.view) */ public void onclick(view v) { try { for(int = 0; < 10; i++){ if(rand.nextint(10) == i){ currentbutton.setbackgroundresource(mine); restart.setbackgroundresource(bombstate); } } } catch(exception e) { toast.maketext(game.this,e.getmessage() + "error : ", toast.length_short).show(); } } }); } //store button table row currentrow.addview(currentbutton); } //add newly created row table minegrid.addview(currentrow); } linearlayout.addview(score, params3); linearlayout.addview(minegrid, params); }
what above code gives me, 6x6 grid made of buttons. , following i’m trying randomly place n amount of mines within board.
try { for(int = 0; < 10; i++){ if(rand.nextint(10) == i){ currentbutton.setbackgroundresource(mine); restart.setbackgroundresource(bombstate); } } }
unfortunately, fills whole board mines, instead of placing n amount of mine on board. know missing when try randomly set mines! can advise me i’m going wrong , point me in right direction?
please ask me clarification.
thanks in advance.
you basically, on every click of button try place mine instead placing them when create buttons. maybe add list, id of buttons mines , check if user has clicked on 1 of buttons.
arraylist<integer> mines = new arraylist<integer>(); . . . currentbutton.settext(" "); if(rand.nextint(2)==1) mines.add(currentbutton.id);
and in onclick()
check if currentbutton.id
in mines
list , if is, display appropriate image.
Comments
Post a Comment