gpt4 book ai didi

java - Android 中的内存游戏从表面 View 延伸

转载 作者:行者123 更新时间:2023-12-01 14:40:15 30 4
gpt4 key购买 nike

尝试在 android 中创建一个内存游戏而不使用 xml 布局,本质上我现在想做的是在网格上的不同位置创建两张相同的卡片。所以我在这里问的是如何将这些卡片随机放置在网格上。我首先创建了随机整数数组,并且没有重复项,但是除非我有一个 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[];


}

卡号将是我分配图片的方式。我正在从表面 View 扩展我的屏幕并实现可运行的。地点卡方法是 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;

}



}

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

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

}

最佳答案

我真的建议使用 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.
}

});

}

}

关于java - Android 中的内存游戏从表面 View 延伸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16039758/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com