0

I am designing a game in Android/Java and so far everything has gone well. The game shows a random card from a deck of 52 and the user has to determine whether the next card will be higher or lower. I put my switch & case in my main class, and im not sure how to re-run the switch and case statement to draw another card at random.

Below are my classes, I know I dont need this many, but I changed the code around alot since my original in attempt to get this to work. I know the problem is not with my app, its because of my inexperience in android... Check out the code and tell me what you think. Since its a pretty broad question and there may be more than one answer, I will upvote everyone who gives me a relevant answer, weather I use the solution or not.

Thanks,

-Steve

My Main Game Class:

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



        }
    });
}

}

Deck Class: (originally this was in my game class)

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;

}

}

Shuffle Class: (Originally this was in my game class)

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

}

Steven
  • 833
  • 3
  • 11
  • 18
  • 2
    Are you really using a huge `switch` statement for that? Why not just use an array indexed by the card number [0, 52) to the corresponding image resource field? You can also replace all the `cardNum = ...` with merely `cardNum = x + 1` – obataku Aug 28 '12 at 23:50
  • I had to name the cards "one, two, theree" instead of (1,2,3) because android gives me a problem when I start res files with numbers. – Steven Aug 28 '12 at 23:52
  • Try to name them like card_01, card_02 ... card_52, and access the Drawable ids using the Reflection api or some other clever solution. See my previous question about this: [link](http://stackoverflow.com/questions/11814490/access-variables-by-name-in-a-loop). Your code will shrink in size dramatically. – Balázs Édes Aug 28 '12 at 23:57
  • Ill take a look at that, but do you know how I can pick another random card after I hit higher or lower? I tried using an intent... and that kind of works... and in the create class I cant run a method from another class. – Steven Aug 28 '12 at 23:59
  • 1
    I will add an answer for this. – Balázs Édes Aug 29 '12 at 00:05

1 Answers1

1

If you want to replace that big switch-case, then try naming your drawables like card_00.png, card_01.png ... card_51.png. This way you can access your drawable ids much easier using the reflection API (or something else) like this:

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){}

If everything went well, then now you have an array of all the drawable ids of your cards. Now your life is 90% more simple. To set the random card simply use:

//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]);
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89