Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory game in android extending from surface view
    primarykey
    data
    text
    <p>Trying create a memory game in android without using the xml layout, essentially what i'm trying to do now is create two identical cards in different places on the grid. So what i'm asking here is how you would place these cards on a grid randomly. I had first created array of random integers and no duplicates however it would prove useless unless i had an if statement checking to see if something has already been placed in that particular. In the process of typing this im trying to see if i can somehow put in an array list and shuffle co ordinates :S so i dont get any repeats.</p> <pre><code>public int randomGen(int[]num){ int size =num.length; for(int j=0;j&lt;size;j++){ num[j]=j; } for(int i =0;i&lt;size;i++){ int indexToSwap = random.nextInt(size); int oldValue = num[i]; num[i] = num[indexToSwap]; num[indexToSwap] = oldValue; }return num[]; } </code></pre> <p>The cardNumber will be how i assign the picture. I'm extending my screen from surface view and implementing runnable. the place card method is part of a Word class</p> <pre><code>public class World { //show them icons first then take away static final int WORLD_WIDTH =4; static final int WORLD_HEIGHT=3; static final int HIGH_SCORE_INC=5; static final int SCORE_INCREMENT=1; boolean grid[][] = new boolean[WORLD_WIDTH][WORLD_HEIGHT]; public CardButton c; public CardButton cd; Random random = new Random(); public World(){ placeCards();//place cards will only be called once } private void placeCards() { int world = WORLD_WIDTH * WORLD_HEIGHT; for(int x=0;x&lt;WORLD_WIDTH;x++) { for(int y=0;y&lt; WORLD_HEIGHT;y++){ grid[x][y]= false; } } //this will need to be in for loop int []rand = new int[WORLD_WIDTH];//this could be one rand =randomGen(rand); int cardx = random.nextInt(WORLD_WIDTH); int cardY = random.nextInt(WORLD_HEIGHT);//this could be one for(int i = 0;i&lt;world/2;i++){ c= new CardButton(cardX,cardY,i); grid[cardX][cardy]= true; //cd=new CardButton(cardX,cardY,i); two cards need to be added with different co ordinates } } } </code></pre> <p>This is my cardButton class</p> <pre><code>public class CardButton { public int cardNum; private boolean isFacedup; public int x,y; public static final int CIRCLE=0; public static final int SQUARE=1; public static final int STAR=3; public static final int HEXAGONNC=4; public static final int OVAL=5; public static final int DIAMOND=6; public static final int REGULAR_DECAGON=7; public static final int TRIANGLE=8; public static final int CLOVER = 9; public static final int DECAGON = 10; public static final int DECAGONNC = 11; public CardButton(int x,int y, int cardNumIn){ //passes a string called code base which cardNum = cardNumIn; isFacedup = false; } public void setFaceUp(){ } public void setFaceDown(){ } public boolean isFaceUp(){ return isFacedup; } public boolean equals(CardButton card){ return cardNum ==card.cardNum? true:false; } } </code></pre> <p>This will be in my game screen class which displays the squares</p> <pre><code>private void drawWorld(World world) { //the integer size will be based on the difficulty perhaps create a constants class Graphics g = game.getGraphics(); CardButton c = world.c; CardButton cd = world.cd; int width = g.getWidth(); int height = g.getHeight(); xpos=width/5; ypos=height/5; Pixmap cards = null; //something to thing about is that although they are two card objects to be created, they will share the same asset if(c.cardNum==CardButton.CIRCLE) cards=Assets.circle; if(c.cardNum==CardButton.CLOVER) cards=Assets.clover; if(c.cardNum==CardButton.DECAGON) cards=Assets.decagon; if(c.cardNum==CardButton.DECAGONNC) cards=Assets.decagonnc; if(c.cardNum==CardButton.SQUARE) cards=Assets.square; if(c.cardNum==CardButton.STAR) cards=Assets.star; if(c.cardNum==CardButton.HEXAGONNC) cards=Assets.hexagennc; if(c.cardNum==CardButton.OVAL) cards=Assets.oval; if(c.cardNum==CardButton.OVAL) cards=Assets.oval; //this is how many times u wish to see the object int length; //this varible will be based on the difficulty int x = c.x; int y = c.y; int sx=cd.x; int sy=cd.y; // for(int i=0;i&lt;2;i++) { g.drawPixmap(cards, x, y); g.drawPixmap(cards, sx, sy); } //g.drawRect(10, 10, g.getWidth(), height, Color.WHITE); /* * Asset simply holds a constant of pixmaps that later get intialised by loading screen with a string */ //g.drawPixmap(Assets.tiles,xpos,ypos); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload