Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Key Listener and for loops

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am currently working on a quiz game. The game has five different types of rounds and two different types of questions.
I am trying to implement a multiplayer mode where two players play with different sets of keys. I have assigned the keys to the players and i am trying to implement a key listener. I  figured i cant use for loops to get user input and set up the game (changing questions and rounds) like i would do conventionally because that sets up the order in which the players will answer (correct me if im wrong). I ended up writing rather messy code with a tone of "if" statements and im wondering if theres a better way to set this up.
This is my code for the key listener:

 
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right about the for loops, if you want to get input via keystrokes then the keystroke listener is the way to go.

And when a key is pressed, all you should do is to change the state of the game. That often involves updating the GUI and it often involves changing internal variables of the game which keep track of things.

So changing the GUI would include the things you're doing right now using System.out (which can't be seen in normal Swing applications). And the internal variables are things like "game" and "p1played". (Although for me, I don't understand why "game" keeps track of a lot of things relevant to the game but it doesn't keep track of whether Player 1 has played.)

Anyway, yeah, you're on the right track. But normally you'd write some methods to do things, rather than having the long messy list of if-statements. For example: in several places you need to know whether there are two players. So perhaps the "game" object could tell you that via a "boolean hasTwoPlayers()" method. And then you've got duplicated code like "game.getPlayer(x).getPlayerKeys().contains(answer)" which could also be encapsulated in a "game" method like "boolean game.isCorrectAnswer(x, answer)". Basically the "game" object should be responsible for doing a lot of the things which you are doing in the code you posted there.
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic