Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:04:45+00:00 2026-06-10T11:04:45+00:00

I am designing a game in Android/Java and so far everything has gone well.

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:04:47+00:00Added an answer on June 10, 2026 at 11:04 am

    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]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been designing a card game in Java on Windows. It runs really well
I am designing a Card based Game in Java. I have implemented the game
I'm designing game site where many (hopefully thousands) players will simultenaously play certain card
I am currently working on designing my first FPS game using JOGL. (Java bindings
I'm designing a game where a character has many items and those items can
I am designing a simple Android game and I am having problem with collusion.
I am designing a game where user and computer play in alternate turns(like chess).
Here is where I am at presently. I am designing a card game with
I'm designing a simple game, which uses Java 2D and Newtonian physics. Currently my
I started java this summer and am designing a small game in my free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.