Need some help on setting up arrays and charAt
Author |
Message |
shadowofanubis66
|
Posted: Wed Dec 05, 2007 7:17 pm Post subject: Need some help on setting up arrays and charAt |
|
|
I'm having some issue with making a matching card game. Our teacher wants us to have 2 parallel arrays, one for names and one for the buttons. I've set those up, but the problem is, i dont know how to get "random" letters and numbers from the names array. I dont want all 2c like i'm getting with the code below. After i figure that out, 2c would then be 2c.gif and then show the picture 2c.gif on one of the buttons. Could anyone help me out in figuring that out? My code that i currently have is below. Thanks in advance... I appreciate any help you can give.
public void setupButtons(){
buttons = new JButton[16];
Icon icon = new ImageIcon("icon.gif");
for (i=0; i < 16; i++){
buttons[i] = new JButton(names[i]);
buttons[i].setName(names[i]);
buttons[i].addActionListener(this);
add(buttons[i]);
}
}
public void initGame(){
setupName();
setupButtons();
}
public void setupName(){
names = new String[16];
for (i=0; i < 16; i++){
names[i] = "" + "23456789tjqka".charAt(value) + "cdhs".charAt(suit);
}
shuffle(names);
} |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Wed Dec 05, 2007 7:46 pm Post subject: RE:Need some help on setting up arrays and charAt |
|
|
use the Random class? I dont get what you are trying to do here, but
"23456789tjqka".charAt(value) + "cdhs".charAt(suit)
value and suit never changes value, that is probably why? so stick a random variable somewhere in that loop and you'll be good |
|
|
|
|
![](images/spacer.gif) |
Euphoracle
![](http://compsci.ca/v3/uploads/user_avatars/11170373664bf5f25f636f1.png)
|
Posted: Wed Dec 05, 2007 7:52 pm Post subject: RE:Need some help on setting up arrays and charAt |
|
|
Get a random byte between x and y, then cast it to char. Be sure your range (x & y) is within the ASCII "numbers" for a-Z (notice lower->capital). x <= a; y >= Z. |
|
|
|
|
![](images/spacer.gif) |
|
|