xml レイアウトを使用せずに Android でメモリ ゲームを作成しようとしています。基本的に、グリッド上の異なる場所に 2 つの同一のカードを作成しようとしています。ここで質問しているのは、これらのカードをランダムにグリッドに配置する方法です。私は最初にランダムな整数の配列を作成しましたが、重複はありませんでしたが、その特定の場所に何かが既に配置されているかどうかを確認する 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 は、画像の割り当て方法になります。サーフェス ビューから画面を拡張し、ランナブルを実装しています。場所カード メソッドは 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);
}