0

尝试在不使用 xml 布局的情况下在 android 中创建记忆游戏,基本上我现在要做的是在网格的不同位置创建两张相同的卡片。所以我在这里要问的是如何将这些卡片随机放置在网格上。我首先创建了随机整数数组并且没有重复,但是除非我有一个 if 语句检查是否已经放置了某些东西,否则它将证明是无用的。在键入此内容的过程中,我试图查看是否可以以某种方式放入数组列表并随机排列坐标:S,因此我没有得到任何重复。

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[];


}

cardNumber 将是我分配图片的方式。我正在从表面视图扩展我的屏幕并实现可运行。place card 方法是 Word 类的一部分

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

    }


}
}

这是我的 cardButton 类

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;

}



}

这将在我的游戏屏幕类中显示方块

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

}
4

1 回答 1

1

我真的建议使用 xml 来做这个记忆游戏,但如果你不想以下应该工作,请注意我无法测试它,因为我没有任何所需的图像或任何东西

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.
            }

        });

    }

}
于 2013-04-16T17:53:15.740 回答