• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flag Game

 
Jon Peterson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post your 'not working' code and we'll try to steer you in the right direction
 
Jon Peterson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic