timer - Boggle game loop logic -
i'm writing console boggle game , gameloop logic isn't quite think should be.
my basic game loop method is:
public void gamestart() { timer.schedule(new boggletimertask(1),0); gameactive = true; system.out.println(boggle.getboggletrayasstring()); while (gameactive) { string word = scanner.next(); boggle.addguess(word); } scanner.close(); timer.cancel(); postgame(); } my timer task class run method looks this:
public void run() { if (minutesremaining == 4 || minutesremaining == 3 || minutesremaining == 2) minutesremaining--; else if (minutesremaining == 1 || minutesremaining == .5) minutesremaining -= .5; if (minutesremaining == 3) { system.out.println("3 minutes remaining."); timer.schedule(new boggletimertask(minutesremaining),60000); } else if (minutesremaining == 2) { system.out.println("2 minutes remaining."); timer.schedule(new boggletimertask(minutesremaining),60000); } else if (minutesremaining == 1) { system.out.println("1 minute remaining."); timer.schedule(new boggletimertask(minutesremaining), 30000); } else if (minutesremaining == .5) { system.out.println("30 seconds remaining."); timer.schedule(new boggletimertask(minutesremaining), 30000); } else { system.out.println("time's up!"); gameactive = false; } the timer counts down think should. "time's up!" prints out well, postgame() never called. thought issue main thread waiting on scanner.next() found solution using robot class , calling key press , key release vk_enter , did not work.
i tried adding check on word in gameloop. added check if (word.compareto("quit") == 0) gameactive = false. call postgame() both when enter quit word or when spell out quit , enter robot.
so i've surmised much: gameloop terminate , send program postgame() when gameactive = false , "time's up!" printing last else block catching appropriately in timer. why 2 don't work in conjunction execute (or @ least how think should execute) don't know.
any provide appreciated. in advance.
i think need have 2 threads running (better robot).
public static void main(string[] args) { new thread() { public void run() { while(true) { string input = new scanner(system.in).nextline(); dosomethingwithinput(input); } } }.start(); new thread() { public void run() { int time = 4; while(time > 0) { system.out.println(time + " seconds left."); printgameboard(); time--; thread.sleep(1000); } printscore(); system.exit(0); } }.start(); } just define functions , have game.
Comments
Post a Comment