I'm writing a 'hang man' program as a bit of a practice exercise and I've ran into a bit of a problem; I just need a little advise if anyone would be so kind.
My apologies if this has been covered here before; I've tried googling this but I don't know how to word it so my results have been quite unhelpful. I'll try my best to explain what I'm trying to do through example.
(Sorry if this is long winded :/)
I wrote a simple 2D shooting game not too long ago in which I used randomising images that also 'moved' randomly.
The way I randomised the images (possibly quite a dirty way of doing it?) was as follows:
I declared/defined a String, its value was the first half of an image path. I also created a String array which contained the second half of an image path.
Expand|Select|Wrap|Line Numbers
- public String imagePath = "/finalpkg/";
- public String myArray[] = {"img1.png", "img2.png", "img3.png", "img4.png"};
This was looped and used on multiple jLabels by the way.
Expand|Select|Wrap|Line Numbers
- nextImage = imagePath + myArray[myRand()];
- something();
- displayPhoto = new ImageIcon(getClass().getResource(nextImage));
- lblImage1.setIcon(displayPhoto);
I believe it's more or less the same principle.
I have 4 separate String arrays for different categories of words for my hang man game.
The user can select which category he/she wants to play with by a jComboBox.
Once the game has initiated the program saves the selected item from the jComboBox into a string. Each ComboBox item has the same name as the array it refers to.
Expand|Select|Wrap|Line Numbers
- chosenArray = (String)comboWord.getSelectedItem();
I've tried the code below but obviously that's horribly flawed. Is there a way I can do that to make the program read that newly created String literally? Or is there a better way of doing this?
(I don't want to learn bad practice)
Expand|Select|Wrap|Line Numbers
- game.arrayLength = ("words."+chosenArray).length;
(Sorry, I'm new here. :P)
Thank you very much in advance for any help I may receive, It'd be most appreciated.