This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Game Development and the fly likes Flag Game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Flag Game" Watch "Flag Game" New topic
Author

Flag Game

Jon Peterson
Greenhorn

Joined: Oct 07, 2010
Posts: 5
So, I am building a game that has the flag of a country in a JLabel. There are 6 flags that appear and a combobox with the names of countries. When the flag is "USA" and the ComboBox has "USA" then the submit button is clicked, it says it is right and adds 1 point to the total amount of points and changes the flag. If it is wrong, it does not add to the total points and changes the flag. I am a little bog down in the different strings I'm using and its not working, anybody know how to at least do this in a simple and short way? Thanks!


-Eruansion del Lorien-
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
post your 'not working' code and we'll try to steer you in the right direction
Jon Peterson
Greenhorn

Joined: Oct 07, 2010
Posts: 5
This is the code I have so far, I cleaned out the if-then garbage that didn't work at all :P




Now what I think is the process would be to have all the pictures into a kind of randomizer. Then it displays a random flag on the Gui. When the answer is selected, and the submit button is pressed, if the flag and country are correct, it adds a point to the total (Not created yet). If the guess is correct, that flag is removed from the list of flags to be shown. If it is not right, another random flag is selected and the old flag returns to the list of flags to be shown.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I'd be more inclined to create an array of 'Country' objects
overloaded constructor so the object can be created with name/flag, or just name.
include an overridden toString() to return the country name (need this to add the Country object to the comboBox)
comboBox can be sorted, if required

also create another array of 6 ImageIcons (named e.g. 'flags')
iterate the country array, and if the flag is not null, point flags[0] (then 1 etc) to the country flag
now shuffle the flags array

open the GUI showing flags[0]

button's actionListener:
check if label.getIcon() equals the country's flag of (comboBox.getSelectedItem() cast to a Country object)
do what you want with correct/wrong answers
label.setIcon(next flag)
if last flag is showing, shuffle flags again, reset counter to 0, so next flag shown will be flags[0]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

I edited your post to have code tags around your source. It makes it MUCH easier to read. Next time, you can simply click the 'code' button above where you enter your post to get the tags, then paste your code between them.

or you can simply type them out:

[ code]

[ \code]

(just leave out the space). You can see it preserves the spacing and even does highlighting of keywords.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flag Game