0

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);

}
j bel
  • 153
  • 6
  • 18
  • I would recommend using any array of something to represent the items in your grid, like int[][], then you populate the grid with any array of cards that are sorted randomly (make sure to have 2 of each) see: http://stackoverflow.com/q/375351/1620542 for how to sort any array randomly. – Eluvatar Apr 16 '13 at 14:36
  • When you say two of each, do you mean two multi dimension array, and is your comment sugesting that i dont use a boolean grid[][] but instead a int[][] ?@Eluvatar – j bel Apr 16 '13 at 14:44
  • you should use something in your grid to represent your cards a boolean will not properly represent a card as it only has 2 states. I recommend creating a custom Card class and use. When I say 2 of each I'm saying you should make a 1 dimensional array with (number of unique cards x 2) number of items then shuffle that array about so it's ordered randomly, then populate that into your grid[][] – Eluvatar Apr 16 '13 at 14:49
  • you may want to take a look at http://developer.android.com/guide/topics/ui/layout/gridview.html as that should work perfectly for your grid and you just populate it with a 1 dimensional array of cards. – Eluvatar Apr 16 '13 at 14:51
  • @Eluvatar from what i understood, it was to create a custom grid class, and with that grid class create a multi diemnsional array, then create 2 one dimensional card arrays in order to populate the grid array... saying `grid[i][j]=etc` will only cater for one array to be populated, how will it work for two..unless i simply populate with one card array :S – j bel Apr 16 '13 at 17:36
  • if you use a GridView you provide an array of cards to an ArrayAdapter (which is then given to the grid view) and the grid gets made based on the number of colunms that was specified in the xml of the GridView, if you would like I can post an example what what I've been talking about – Eluvatar Apr 16 '13 at 17:39
  • Please i think that would answer a lot of question, im not using a GridView btw, each one of my screen within my game extends from a surfaceView class i haven't touched any Xml as of yet. For consistency purposes could you show me how to do it without GridView, or is it imposible ? – j bel Apr 16 '13 at 17:45
  • For what it's worth, this being two years after you asked the question, if you have a set "deck" of cards that you will always pull from when you populate your board, you could create a `List` and use the build-int Java Collections shuffle() method. `List deck.suffle();` then loop through that List when it's time to populate your array. >http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#shuffle(java.util.List) – Chamatake-san May 18 '15 at 14:18

1 Answers1

1

I'd really recommend using the xml to do this memory game but if you dont want to the following should work, note I cant test it out as I dont have any of the required images or anything

class MainActivity extends Activity implements OnItemSelectedListener {

    GridView cardsGridView;
    private ArrayAdapter<CardButton> cardsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        cardsGridView = new GridView(getBaseContext());
        setContentView(cardsGridView);

        CardButton[] cards = getListOfRandomCards();



        cardsAdapter = new ArrayAdapter<CardButton>(this, android.R.layout.simple_spinner_item, cards) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null)
                    convertView = new ImageView(getBaseContext());
                ImageView view = (ImageView) convertView;
                CardButton card = getItem(position);
                int ImageResource;
                if (card.IsSelected()) {
                    ImageResource = card.getCardImage();
                } else {
                    ImageResource = R.drawable.default_card_image;
                }
                view.setImageResource(ImageResource);
                return view;
            }
        };

        cardsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cardsGridView.setAdapter(cardsAdapter);

        cardsGridView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // To change body of implemented methods use File | Settings | File Templates.
                CardButton card = (CardButton) parent.getItemAtPosition(position);
                card.SetSelected(true);
                ImageView imageView = (ImageView) view;
                imageView.setImageResource(card.getCardImage());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // To change body of implemented methods use File | Settings | File Templates.
            }

        });

    }

}
Eluvatar
  • 2,265
  • 19
  • 24
  • thank you i think i can adapt some of this, the only question would be displaying the grid diffrenntly to how you have done, ive updated my question to give you an idea of how i have things set up. – j bel Apr 16 '13 at 18:36
  • if you're not using a grid view I'm not sure I dont have experience with the Graphics side of android. – Eluvatar Apr 16 '13 at 19:55
  • thanks for your help none the less, i will share what i've done once ive finished if you wish to see how its done @eluvatar – j bel Apr 18 '13 at 10:56
  • thanks, I don't have any experience with Graphics so I'd be interested to see how you do it. – Eluvatar Apr 18 '13 at 16:08