我正在用 Android/Java 设计一款游戏,到目前为止一切都很顺利。游戏从一副 52 张牌中随机显示一张牌,用户必须确定下一张牌是更高还是更低。我将 switch 和 case 放在主类中,但我不确定如何重新运行 switch 和 case 语句以随机绘制另一张卡。
下面是我的类(class),我知道我不需要这么多,但是自从我原来的类(class)以来我改变了很多代码,试图让它工作。我知道问题不在于我的应用程序,而是因为我对 android 缺乏经验......查看代码并告诉我你的想法。由于这是一个相当广泛的问题,并且可能有多个答案,因此无论我是否使用该解决方案,我都会投票给所有给我相关答案的人。
谢谢
-史蒂夫
我的主要游戏类:
public class Game extends SwarmActivity {
int cardNum;
Deck d = new Deck();
int x = d.shuffle();
String y = String.valueOf(x);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Swarm.init(this, 1388, "1e02a1ecfa9483b7b62e7b32c7e055f3");
TextView t = (TextView) findViewById(R.id.score);
ImageView display = (ImageView) findViewById(R.id.display);
switch (x) {
case 0:
display.setImageResource(R.drawable.c_one);
cardNum = 1;
break;
case 1:
display.setImageResource(R.drawable.c_two);
cardNum = 2;
break;
case 2:
display.setImageResource(R.drawable.c_three);
cardNum = 3;
break;
case 3:
display.setImageResource(R.drawable.c_four);
cardNum = 4;
break;
case 4:
display.setImageResource(R.drawable.c_five);
cardNum = 5;
break;
---------- (5-49) ----------
case 50:
display.setImageResource(R.drawable.c_fiftyone);
cardNum = 51;
break;
case 51:
display.setImageResource(R.drawable.c_fiftytwo);
cardNum = 52;
break;
}
ImageView higher = (ImageView) findViewById(R.id.btn_higher);
ImageView lower = (ImageView) findViewById(R.id.btn_lower);
higher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
lower.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
套牌类别:(最初这是在我的游戏类别中)
public class Deck {
public int shuffle() {
Shuffle s = new Shuffle();
int[] shuffDeck = s.getShuffle();
int i = 0;
int x = shuffDeck[i];
String y = String.valueOf(x);
return x;
}
}
随机播放类:(最初这是在我的游戏类中)
public class Shuffle {
public static int[] getShuffle() {
int[] cards = new int[52];
ArrayList<Integer> cards_objs = new ArrayList<Integer>();
for (int i = 0; i < cards.length; i++) {
cards_objs.add(i);
}
Collections.shuffle(cards_objs);
for (int i = 0; i < cards.length; i++) {
cards[i] = cards_objs.get(i);
}
return cards;
}
}
最佳答案
如果您想替换那个大开关盒,请尝试将您的绘图命名为card_00.png、card_01.png ... card_51.png。这样你就可以使用反射API(或其他东西)更轻松地访问你的drawable id,如下所示:
int [] cards = new int [52];
Field [] fields = R.drawable.class.getDeclaredFields();
String [] names = new String[52];
for(int i=0; i<fields.length; i++)
if(fields[i].getName().contains("card_"))
names[i] = fields[i].getName();
Arrays.sort(names);
try
{
for(int i=0; i<names.length; i++)
cards[i] = R.drawable.class.getField(names[i]).getInt(null);
}
catch(Exception ex){}
如果一切顺利,那么现在您就拥有了卡片的所有可绘制 ID 的数组。现在您的生活简单了 90%。要设置随机卡,只需使用:
//create a Random object, and an integer
//indicating the current card as a member of your class:
Random random = new Random();
int actual = 0;
//then for random card selection:
actual = random.nextInt(52);
display.setImageResource(cards[actual]);
//for getting a higher card:
actual = actual<51 ? actual+1 : actual;
display.setImageResource(cards[actual]);
//for getting a lower card:
actual = actual>0 ? actual-1 : actual;
display.setImageResource(cards[actual]);
关于java - 制作一个android游戏,不知道如何解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12169076/