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.
public int randomGen(int[]num){
int size =num.length;
for(int j=0;j<size;j++){
num[j]=j;
}
for(int i =0;i<size;i++){
int indexToSwap = random.nextInt(size);
int oldValue = num[i];
num[i] = num[indexToSwap];
num[indexToSwap] = oldValue;
}return num[];
}
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
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<WORLD_WIDTH;x++)
{
for(int y=0;y< 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<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
}
}
}
This is my cardButton class
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;
}
}
This will be in my game screen class which displays the squares
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<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);
}