Hello there!
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.
- public String imagePath = "/finalpkg/";
-
public String myArray[] = {"img1.png", "img2.png", "img3.png", "img4.png"};
I then randomised the value that'd be returned for the 'second half' of the image path, combined it with the first half as a single string then used that string as the argument for 'setIcon'.
This was looped and used on multiple jLabels by the way.
- nextImage = imagePath + myArray[myRand()];
-
something();
-
displayPhoto = new ImageIcon(getClass().getResource(nextImage));
-
lblImage1.setIcon(displayPhoto);
Now back to what I'm currently doing...
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.
- chosenArray = (String)comboWord.getSelectedItem();
The arrays are in their own class, 'words'. What I've been trying to do is to 'add' "words." to the array name String to create "words."+chosenArray and have that String read literally, so that I can get the size of the chosen array and save it to an int variable in my 'game' class. (Then I can use that int as a range to randomly select a word from the String array.)
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)
- game.arrayLength = ("words."+chosenArray).length;
Hopefully I explained that properly and it wasn't too confusing.
(Sorry, I'm new here. :P)
Thank you very much in advance for any help I may receive, It'd be most appreciated.