Creating timed word game in Java -
i have been trying out how create game (in java) in player needs unscramble word in 4 seconds.
if answer right, game continues. if wrong game ends , player presented final score (1pt per right answer).
i struggling keep track of points. every time loops through, points reset itself. also, game continues if answer @ entered.
my code trainwreck , might need start over, hoping pointed in right direction.
i new here please let me know formatting or issues question or go better assistance. appreciated! thank much!
import java.util.timer; import java.util.timertask; import javax.swing.joptionpane; class test { private string ans = ""; keepscore score = new keepscore(); int scoree = 0; timertask task = new timertask() { public void run() { if (ans.equals("")) { joptionpane.showmessagedialog(null, "time's up!", "", joptionpane.information_message); joptionpane.showmessagedialog(null, score.keepscore(scoree), "", joptionpane.information_message); system.exit(0); } } }; public void getinput() throws exception { timer timer = new timer(); dictionaryclass object = new dictionaryclass(); timer.schedule(task, 4 * 1000); string word = "0"; string = ""; int x = 0; randomletters meth = new randomletters(); int randomsequence = meth.randomletters(x); if (randomsequence == 1) { word = "1"; ans = joptionpane.showinputdialog("f s h i"); if (ans.equals(object.dictionaryclass(word))) { scoree = score.keepscore(scoree); system.out.println("ok: " + scoree); } } if (randomsequence == 2) { word = "2"; ans = joptionpane.showinputdialog("k p n i"); if (ans.equals(object.dictionaryclass(word))) { scoree = score.keepscore(scoree + 1); } } timer.cancel(); system.out.println("you have entered: " + ans); } public static void main(string[] args) { // int forever=1; int count = 0; int points = 0; (int aa = 0; aa < 10; aa++) { try { (new test()).getinput(); } catch (exception e) { system.out.println(e); } system.out.println(""); count = count + 1; system.out.println("count:" + count); } joptionpane.showmessagedialog(null, "score: " + count, "score", joptionpane.information_message); } }
you can use futures
, and executorservice
timeout instead of timertask
.
you use executorservice
run input dialog in 1 thread , main thread waits until dialog returns or timeout occurs.
it implemented way
import java.util.arraylist; import java.util.collections; import java.util.list; import java.util.random; import java.util.concurrent.callable; import java.util.concurrent.executionexception; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.future; import java.util.concurrent.timeunit; import java.util.concurrent.timeoutexception; import javax.swing.joptionpane; public class wordgame { private static final int idx_scrumbled = 0; private static final int idx_unscrumbled = 1; private static final list<string[]> dict; static { // todo: think of correct data structure list<string[]> tmp = new arraylist<>(); // list contains scumbled word followed unscumbled word. tmp.add( new string[]{"k p n i", "pink" }); tmp.add(new string[]{"f s h i", "fish" }); dict = collections.unmodifiablelist(tmp); } public wordgame() { random = new random(); fetchinputexecutor = executors.newsinglethreadexecutor(); } private final executorservice fetchinputexecutor; private final random random; private int getnextnotnegativerandomint(int startindex, int endindex) { int value = -1; if (startindex < 0 || endindex < 0 || startindex > endindex || endindex > integer.max_value -1) throw new illegalargumentexception("invalid bounds " + startindex + " " + endindex); { value = random.nextint(endindex + 1); } while (value < startindex || value > endindex); return value; } public string fetchinput(final string scrumbled) throws interruptedexception, executionexception, timeoutexception { future<string> future = fetchinputexecutor.submit(new callable<string>() { @override public string call() throws exception { return joptionpane.showinputdialog(scrumbled); } }); // wait 4 seconds return future.get(4, timeunit.seconds); } public int playround(int oldscore) throws interruptedexception, executionexception { try { int idx = getnextnotnegativerandomint(0, dict.size()-1); string answer = fetchinput(dict.get(idx)[idx_scrumbled]); if (dict.get(idx)[idx_unscrumbled].equals(answer)) { return oldscore + 1; } return oldscore; } catch (timeoutexception toe) { joptionpane.getrootframe().dispose(); joptionpane.showmessagedialog(null, "time's up!", "", joptionpane.information_message); return oldscore; } } public int play() throws interruptedexception, executionexception { int score = 0; (int aa = 0; aa < 10; aa++) { score = playround(score); } return score; } public static void main(string[] args) throws interruptedexception, executionexception { int score = new wordgame().play(); joptionpane.showmessagedialog(null, "score: " + score, "score", joptionpane.information_message); } }
it can implemented using timer
, timertask
here timertask kills dialogmessagebox after timeout value reached.
the playround
, fetchinput
may like:
public string fetchinput(final string scrumbled) { // need prevents timeup dialog if answer // entered within timeout value. // because access 2 threads let's atomicboolean final atomicboolean completed = new atomicboolean(false); // start timer kills optionpane after specified delay. timer.schedule(new timertask() { @override public void run() { if (!completed.get()) joptionpane.getrootframe().dispose(); } }, 4 * 1000l); string tmp = joptionpane.showinputdialog(scrumbled); // prevent killing 1 of next input dialogs completed.set(true); return tmp; } public int playround(int oldscore) { int idx = getnextnotnegativerandomint(0, dict.size()-1); string answer = fetchinput(dict.get(idx)[idx_scrumbled]); // if dialog disposed because of timeout result null if (answer == null) { joptionpane.showmessagedialog(null, "time's up!", "", joptionpane.information_message); return oldscore; } if (dict.get(idx)[idx_unscrumbled].equals(answer)) { return oldscore + 1; } return oldscore; }
Comments
Post a Comment