Reputation: 11
In this assignment you will use an applet to display images of playing cards. The applet should load a deck of 52 playing card images from the "images" folder that you downloaded. The applet should shuffle the deck (use a random number generator) and display the first 10 cards of the shuffled deck. Display the cards in two rows of five cards each.
My applet works partially, but when I run the applet viewer the display is just the same card ten times. Here is my code:
package CTY;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
public class Exercise12 extends Applet {
Image c1;
Image c2;
Image c3;
Image c4;
Image c5;
Image c6;
Image c7;
Image c8;
Image c9;
Image c10;
public void init() {
Random random = new Random();
String cards[][] = {
{ "cards/c1.gif", "cards/c2.gif", "cards/c3.gif", "cards/c4.gif", "cards/c5.gif", "cards/c6.gif",
"cards/c7.gif", "cards/c8.gif", "cards/c9.gif", "cards/c10.gif", "cards/cj.gif",
"cards/ck.gif", "cards/cq.gif" },
{ "cards/s1.gif", "cards/s2.gif", "cards/s3.gif", "cards/s4.gif", "cards/s5.gif", "cards/s6.gif",
"cards/s7.gif", "cards/s8.gif", "cards/s9.gif", "cards/s10.gif", "cards/sj.gif",
"cards/sk.gif", "cards/sq.gif" },
{ "cards/d1.gif", "cards/d2.gif", "cards/d3.gif", "cards/d4.gif", "cards/d5.gif", "cards/d6.gif",
"cards/d7.gif", "cards/d8.gif", "cards/d9.gif", "cards/d10.gif", "cards/dj.gif",
"cards/dk.gif", "cards/dq.gif" },
{ "cards/h1.gif", "cards/h2.gif", "cards/h3.gif", "cards/h4.gif", "cards/h5.gif", "cards/h6.gif",
"cards/h7.gif", "cards/h8.gif", "cards/h9.gif", "cards/h10.gif", "cards/hj.gif",
"cards/hk.gif", "cards/hq.gif" } };
int Card[] = new int[10];
int Suit[] = new int[10];
int suit = random.nextInt(4);
int card = random.nextInt(10);
boolean newCard = false;
for (int i = 0; i < 10; i++) {
while (newCard == false) {
newCard = true;
suit = random.nextInt(4);
card = random.nextInt(13);
for (int j = 0; j < i; j++) {
if (Card[j] == card && Suit[j] == suit ) {
newCard = false;
}
}
}
Card[i] = card;
Suit[i] = suit;
}
c1 = getImage(getDocumentBase(),
cards[Suit[0]][Card[0]]);
c2 = getImage(getDocumentBase(),
cards[Suit[1]][Card[1]]);
c3 = getImage(getDocumentBase(),
cards[Suit[2]][Card[2]]);
c4 = getImage(getDocumentBase(),
cards[Suit[3]][Card[3]]);
c5 = getImage(getDocumentBase(),
cards[Suit[4]][Card[4]]);
c6 = getImage(getDocumentBase(),
cards[Suit[5]][Card[5]]);
c7 = getImage(getDocumentBase(),
cards[Suit[6]][Card[6]]);
c8 = getImage(getDocumentBase(),
cards[Suit[7]][Card[7]]);
c9 = getImage(getDocumentBase(),
cards[Suit[8]][Card[8]]);
c10 = getImage(getDocumentBase(),
cards[Suit[9]][Card[9]]);
}
public void paint(Graphics graphics) {
graphics.drawImage(c1, 30, 30, this);
graphics.drawImage(c2, 30, 150, this);
graphics.drawImage(c3, 120, 30, this);
graphics.drawImage(c4, 120, 150, this);
graphics.drawImage(c5, 210, 30, this);
graphics.drawImage(c6, 210, 150, this);
graphics.drawImage(c7, 300, 30, this);
graphics.drawImage(c8, 300, 150, this);
graphics.drawImage(c9, 390, 30, this);
graphics.drawImage(c10, 390, 150, this);
}
}
Upvotes: 1
Views: 331
Reputation: 3012
The problem is in the setup of your loops:
for (int i = 0; i < 10; i++) {
while (newCard == false) {
newCard = true;
suit = random.nextInt(4);
card = random.nextInt(13);
for (int j = 0; j < i; j++) {
if (Card[j] == card && Suit[j] == suit ) {
newCard = false;
}
}
}
Card[i] = card;
Suit[i] = suit;
}
The first time through the while loop i
is 0 and so it never enters the inner for loop causing newCard to remain true. Because newCard is true it doesn't go through the while loop again and so the variables card and suit never change through the iterations of the outer for loop.
One solution is to set newCard = false
right before the while loop:
for (int i = 0; i < 10; i++) {
newCard = false;
while (newCard == false) {
newCard = true;
suit = random.nextInt(4);
...
This will ensure it always enters the while loop on each iterations of the outer for loop.
Upvotes: 2
Reputation: 431
Here's your problem:
for (int i = 0; i < 10; i++) {
while (newCard == false) {
newCard = true;
suit = random.nextInt(4);
card = random.nextInt(13);
for (int j = 0; j < i; j++) {
if (Card[j] == card && Suit[j] == suit ) {
newCard = false;
}
}
Card[i] = card;
Suit[j] = suit;
}
}
Upvotes: 2
Reputation: 158
Issue with the loop as newCard flag will be true always after one iteration
for (int i = 1; i <=10; i++) {
while (newCard == false) {
newCard = true;
suit = random.nextInt(4);
card = random.nextInt(13);
for (int j = 0; j < i; j++) {
if (Card[j] == card && Suit[j] == suit ) {
newCard = false;
}
}
}
Card[i] = card;
Suit[i] = suit;
}
Upvotes: 2