Note that there are some explanatory texts on larger screens.

plurals
  1. POApplication randomly stops responding.
    primarykey
    data
    text
    <p>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</p> <pre><code>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 &lt;= getTotalDealerCards(); counter++){ if (dealerCards [counter].getCard() + 1 == 1){ points += 11; aceFlag++; } else if (dealerCards [counter].getCard() + 1 &gt; 10) points += 10; else points += dealerCards [counter].getCard() + 1; } do { if (points &gt; 21 &amp;&amp; aceFlag &gt; 0){ points -= 10; aceFlag--; } } while (aceFlag&gt;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 &lt;= getTotalPlayerCards(); counter++){ if (playerCards [counter].getCard() + 1 == 1){ points += 11; aceFlag++; } else if (playerCards [counter].getCard() + 1 &gt; 10) points += 10; else points += playerCards [counter].getCard() + 1; } do { if (points &gt; 21 &amp;&amp; aceFlag &gt; 0){ points -= 10; aceFlag--; } } while (aceFlag&gt;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 &lt; TOTAL_CARDS; i++){ dealerCards [i] = null; playerCards [i] = null; createdCards [i] = null; } // create dealer &amp; player cards and save them to dealer, player and total arrays. for (int dealcounter = 0; dealcounter &lt;=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() &lt; 16){ hitDealer(); } } </code></pre> <p>// Create card and validate against existing before returning object. public Card createCard(){</p> <pre><code> 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 &lt;= getTotalPlayerCards()) { if (createdCards[counter2].getSuit() == suit &amp;&amp; createdCards[counter2].getCard() == value || suit &gt; 3 || suit &lt; 0 || value &gt; 12 || value &lt; 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() &lt;=21){ int counter = 0; while (counter &lt;= total){ cards += playerCards[counter].toString() + " "; counter++; } return cards; }else { int counter=0; while (counter &lt;= 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()&gt;21){ busted=true; totalDealerPoints = 0; totalPlayerPoints = 0; mpBusted.start(); playerPoints.setText("You were busted!!"); for (int i = 0; i &lt; TOTAL_CARDS; i++){ dealerCards [i] = null; playerCards [i] = null; createdCards [i] = null; } } return busted; } //Check for winner public void checkWinner(){ if (getTotalDealerPoints() &lt;= 21 || getTotalPlayerPoints() &lt;= 21 &amp;&amp; !isBusted()){ if (getTotalDealerPoints() &gt; 21 || getTotalDealerPoints() &lt; getTotalPlayerPoints()){ playerPoints.setText("You won!!"); mpWin.start(); } else if(getTotalDealerPoints() &gt; getTotalPlayerPoints()){ mpBusted.start(); playerPoints.setText("You were busted!!"); for (int i = 0; i &lt; TOTAL_CARDS; i++){ dealerCards [i] = null; playerCards [i] = null; createdCards [i] = null; } } else{ mpEven.start(); playerCards1.setText("We have same points!"); } } else { deal (); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload