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 7647147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:23:34+00:00 2026-05-31T10:23:34+00:00

I am new to programming and I’m making a very simple blackjack game with

  • 0

I am new to programming and I’m making a very simple blackjack game with only basic functions. When I run the program on the emulator it runs maybe for one hand, two, sometimes 5 or more but it always stops responding at some stage when i click on one of the three butons. There is a splash screen that runs for three seconds and the there is a thread comming from that activity that starts this menu activity. Could anyone maybe tell why this is happening? It usually happens when I clcik on one of the buttons even though there is no much comput

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);   
    btDeal = (Button) findViewById(R.id.deal);
    playerCards1 = (TextView) findViewById(R.id.playerCards);
    playerPoints = (TextView) findViewById(R.id.playerPoints);
    dealerCards1 =  (TextView) findViewById(R.id.dealerCard);

    mpBusted= MediaPlayer.create(this, R.raw.busted);
    mpWin = MediaPlayer.create(this, R.raw.win);
    mpShuffling = MediaPlayer.create(this, R.raw.shuffling);
    mpEven = MediaPlayer.create(this, R.raw.even);
    mpHit= MediaPlayer.create(this, R.raw.hit);

    btDeal.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            deal ();

        }
    });  //getTotalDealerCards()
        //getTotalPlayerCards()


    btHit = (Button) findViewById(R.id.hit);
    btHit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Boolean busted = isBusted();


            if(!busted){
                hitPlayer();
                playerCards1.setText(getPlayerCardsToString());
                if (isBusted()){
                    mpBusted.start();

                }else{
                    playerCards1.setText(getPlayerCardsToString());
                    playerPoints.setText(Integer.toString(getTotalPlayerPoints()));
                }
            }

        }
    });

    btStand = (Button) findViewById(R.id.stand);

    btStand.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            checkWinner();

        }// testValue(getTotalPlayerCards())
    });
}





/***********    Function declarations starts here    **********/





//Sum and return the total points for the dealer cards
public int getTotalDealerPoints(){
    int points = 0;
    int aceFlag = 0; //flag to deal with Aces
    int counter;

    for (counter = 0; counter <= getTotalDealerCards(); counter++){
        if (dealerCards [counter].getCard() + 1 == 1){
               points += 11;
               aceFlag++;               
        }
        else if (dealerCards [counter].getCard() + 1 > 10)
            points += 10;

        else
            points += dealerCards [counter].getCard() + 1;
    }

    do {
        if (points > 21 && aceFlag > 0){
            points -= 10;
            aceFlag--;
        }
    } while (aceFlag>0);

    return points;
}


//Get the total player points deal
public int getTotalPlayerPoints(){
    int points = 0;
    int aceFlag = 0; //flag to deal with Aces
    int counter;

    for (counter = 0; counter <= getTotalPlayerCards(); counter++){
        if (playerCards [counter].getCard() + 1 == 1){
               points += 11;
               aceFlag++;               
        }
        else if (playerCards [counter].getCard() + 1 > 10)
            points += 10;

        else
            points += playerCards [counter].getCard() + 1;
    }

    do {
        if (points > 21 && aceFlag > 0){        
            points -= 10;
            aceFlag--;
        }
    } while (aceFlag>0);

    return points;
}


//Deal function to start hand
public void deal (){

    // If deal is pressed reset all and start over.
    mpShuffling.start();
    totalDealerPoints = 0;
    totalPlayerPoints = 0;
    totalCreatedCards = 0;

    for (int i = 0; i < TOTAL_CARDS; i++){
        dealerCards [i] = null;
        playerCards [i] = null;
        createdCards [i] = null;

    }
// create dealer & player cards and save them to dealer, player and total arrays.       

    for (int dealcounter = 0; dealcounter <=1 ; dealcounter++){

        dealerCards[dealcounter]= createCard();
        addCardToCreatedCards(dealerCards[dealcounter]);        
        playerCards[dealcounter] = createCard();
        addCardToCreatedCards(playerCards[dealcounter]);
    }

    String theCards = getPlayerCardsToString();
    String dealerCard = dealerCards[0].toString();
    String playerpoints= Integer.toString(getTotalPlayerPoints());
    playerCards1.setText(theCards);
    dealerCards1.setText(dealerCard);
    playerPoints.setText(playerpoints);//getTotalPlayerPoints()


    while (getTotalDealerPoints() < 16){
        hitDealer();    
    }

}

// Create card and validate against existing before returning object.
public Card createCard(){

    int counter2 = 0;
    int flag = 0;
    int value;
    int suit;

    do {
        flag = 0;
        suit = randomer.nextInt(4);
        value = randomer.nextInt(13);
        // validate against permitted values before creating cards
        while (counter2 <= getTotalPlayerCards()) {
           if (createdCards[counter2].getSuit() == suit && createdCards[counter2].getCard() == value || suit > 3 || suit < 0 || value > 12 || value < 0){
              flag = -1;
           }
           counter2++;
        } 

    } while (flag != 0);

    Card theCard = new Card (suit, value);

    return theCard;     
}




// Add card to the records of created cards
public void addCardToCreatedCards(Card aCard){      
    createdCards [totalCreatedCards] = aCard;
    totalCreatedCards++;
}




// Add a card to dealers cards
public void hitPlayer(){

    //If the hand was started add card, else deal to start hand.

    if (getTotalPlayerCards()+1 != 0){ 
        mpHit.start();
        playerCards [getTotalPlayerCards()+1] = createCard();
        addCardToCreatedCards(playerCards [getTotalPlayerCards()]);
    }
    else
        deal();
}

// Create a new card for the dealer
public void hitDealer(){

    dealerCards [getTotalDealerCards()+1] = createCard();
    addCardToCreatedCards(dealerCards [getTotalDealerCards()]);

}

public String getPlayerCardsToString(){

    String cards = "";
    int total = getTotalPlayerCards();

    if (getTotalPlayerPoints() <=21){
        int counter = 0;

        while (counter <= total){

            cards += playerCards[counter].toString() + "  ";
            counter++;
        }

    return  cards;

    }else {
        int counter=0;
        while (counter <= total){

            cards += playerCards[counter].toString() + "  ";
            counter++;
        }
        return cards;
    }
}


public int getTotalPlayerCards(){
    int initialCount = 0;

    while (playerCards[initialCount] != null){

        initialCount++;
    }

    return initialCount-1;
}



public int getTotalDealerCards(){
    int initialCount = 0;

    while (dealerCards[initialCount] != null){

        initialCount++;
    }

    return initialCount-1;
}

public int getTotalCreatedCards(){
    int initialCount = 0;

    while (createdCards[initialCount] != null){

        initialCount++;
    }

    return initialCount-1;
}


public Boolean isBusted(){
    Boolean busted = false;

    if (getTotalPlayerPoints()>21){
        busted=true;
        totalDealerPoints = 0;
        totalPlayerPoints = 0;
        mpBusted.start();
        playerPoints.setText("You were busted!!");


        for (int i = 0; i < TOTAL_CARDS; i++){
            dealerCards [i] = null;
            playerCards [i] = null;
            createdCards [i] = null;

        }

    }
    return busted;
}


//Check for winner
public void checkWinner(){

    if (getTotalDealerPoints() <= 21 || getTotalPlayerPoints() <= 21 && !isBusted()){




             if (getTotalDealerPoints() > 21 || getTotalDealerPoints() < getTotalPlayerPoints()){
                 playerPoints.setText("You won!!");
                 mpWin.start();

             }

             else if(getTotalDealerPoints() > getTotalPlayerPoints()){
                 mpBusted.start();
                 playerPoints.setText("You were busted!!");

                 for (int i = 0; i < TOTAL_CARDS; i++){
                      dealerCards [i] = null;
                      playerCards [i] = null;
                      createdCards [i] = null;

                 }

             }

             else{

                 mpEven.start();
                 playerCards1.setText("We have same points!");

            }   



    }
    else {

        deal ();            
    }

}

}

  • 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-05-31T10:23:35+00:00Added an answer on May 31, 2026 at 10:23 am

    Use the debugger in eclipse to find out where it gets frozen.

    Also the android emulator is very slow even with a fast PC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to programming and I dont know very much about but I'm making
I'm new to programming. I want to make a card game with C++ /
Very new to programming for iOS and Cocoa so please take it easy on
I am very new to programming and Objective-C and I am trying to work
i been thinking of a new programming language. Before trying to implement it i
What features could be added to a new programming language to make it more
let's say that you in your new programming work your boss comes and says
What is the real benefit of creating a new programming language? It is highly
I always believed that when starting to learn a new programming language programmer must
I've came across this new acronym, SOFEA, apparently a new programming paradigm for web

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.