hi, i have an assignment for uni where i have to make a text game according to a given scenario, a part of the game has a bomb room and a bomb timer, where if the player doesent get the code right within 10 seconds the bomb explodes and the player has the choice to restart.
here is the code to the bomb, im not sure if the sleep method timer is causing the problems, it runs, but when you choose y to restart it just goes into this error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at hostage.Main.main(Main.java:134)
here is the code:
--------------------------------------------------------------------------------
boolean go = true;
boolean countfinish = false;
Scanner sc = new Scanner(System.in);
int rn1, rn2, answer, sumofrn1rn2;
boolean restart = true;
System.out.println("you have entered the BOMB ROOM!!! solve the equasion within 10 seconds to defuse the bomb");
System.out.println();
//below is the equasion to defuse the bomb
//------------------------------------------------------
rn1 = (int)(Math.random()*1000);
rn2 = (int)(Math.random()*1000);
System.out.print(rn1);
System.out.print(" + ");
System.out.print(rn2);
System.out.print(" = ");
answer = 0;
sumofrn1rn2 = (rn1+rn2);
try
{
// the sleep method of the thread object is called
Thread.sleep(10000);
}
catch(InterruptedException e)
{
}
countfinish = true;
if (countfinish == true)
{
if (sumofrn1rn2 != answer)
{
System.out.println("You faild, oh and your dead... Restart? (y/n)");
restart2 = sc.next().charAt(0);
if (restart2 == 'y')
{
Current_Room = "ROOM 1";
System.out.println("Welcome to the Lobby!.....again....");
}
}
}
answer = sc.nextInt();
if (answer == rn1+rn2)
{
System.out.println("You have defused the bomb... fewww...");
//go back to lobby
}
--------------------------------------------------------------------------------
any help would be appresiated, thank you
-----------------This post was merged together. In future use the edit button.-----------------
/*
* Main.java
*
* Created on 21 November 2007, 23:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package hostage;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
/**
*
* @author Pain In Arse
*/
public class Main {
public static void main (String[] args) {
System.out.println("Welcome to the Lobby!");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String Last_Command = null;
String Temp_Split[] = {null};
String Temp_String = null;
boolean Room_1_South_Door_Locked;
boolean Game_Over;
char restart2;
boolean Picked_Up_Note = false;
boolean Picked_Up_Key = false;
String Current_Room = null;
Current_Room = "ROOM 1"; // START OF HERE
Room_1_South_Door_Locked = true;
Game_Over = false;
do{
try {
Last_Command = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error reading 'Command'");
System.exit(1);
}
Last_Command = Last_Command.toUpperCase();
Temp_Split = Last_Command.split(" ");
while (Current_Room.equals("ROOM 1")){
// ROOM 1
if (Temp_Split[0].equals("MOVE")){
if (Temp_Split[1].equals("NORTH")){
// R1 - North - NO ROOM TO NORTH
System.out.println("NO ROOM TO THE NORTH!");
}else{
if (Temp_Split[1].equals("EAST")){
// R1 - EAST - YOUR DEAD == BOMB ROOM
boolean go = true;
boolean countfinish = false;
Scanner sc = new Scanner(System.in);
int rn1, rn2, answer, sumofrn1rn2;
boolean restart = true;
System.out.println("you have entered the BOMB ROOM!!! solve the equasion within 10 seconds to defuse the bomb");
System.out.println();
//below is the equasion to defuse the bomb
//------------------------------------------------------
rn1 = (int)(Math.random()*1000);
rn2 = (int)(Math.random()*1000);
System.out.print(rn1);
System.out.print(" + ");
System.out.print(rn2);
System.out.print(" = ");
answer = 0;
sumofrn1rn2 = (rn1+rn2);
try
{
// the sleep method of the thread object is called
Thread.sleep(10000);
}
catch(InterruptedException e)
{
}
countfinish = true;
if (countfinish == true)
{
if (sumofrn1rn2 != answer)
{
System.out.println("You faild, oh and your dead... Restart? (y/n)");
restart2 = sc.next().charAt(0);
if (restart2 == 'y')
{
Current_Room = "ROOM 1";
System.out.println("Welcome to the Lobby!.....again....");
}
}
}
answer = sc.nextInt();
if (answer == rn1+rn2)
{
System.out.println("You have defused the bomb... fewww...");
//go back to lobby
}
System.exit(0);
}else{
if (Temp_Split[1].equals("SOUTH")){
if (Room_1_South_Door_Locked == true){
// R1 - LOCKED DOOR
System.out.println("DOOR LOCKED");
}else{
// R1 - Next Level
System.out.println("!!! Next Level !!!");
System.out.println("!!! GAME OVER !!!");
System.exit(0);
}
}else{
if (Temp_Split[1].equals("WEST")){
// R1 - GOTO ROOM 2
Current_Room = "ROOM 2";
System.out.println("WELCOME TO ROOM " + Current_Room);
}
}
}
}
}else{
if (Temp_Split[0].equals("LOOK")){
System.out.println("There is a Door to the 'EAST'");
System.out.println("There is a Door to the 'SOUTH'");
System.out.println("There is a Door to the 'WEST'");
if (!Picked_Up_Note){
System.out.println("There is a 'NOTE' on the floor");
}
}else{
if (Temp_Split[0].equals("PICKUP")){
if (Temp_Split[1].equals("NOTE")){
System.out.println("You pickup up a '" + Temp_Split[1] + "'");
Picked_Up_Note = true;
}else{
System.out.println("There is no " + Temp_Split[1] + " to pick up");
}
}else{
if (Temp_Split[0].equals("USE")){
if (Temp_Split[1].equals("NOTE")){
if (Picked_Up_Note){
System.out.println("There are 3 doors in room 1, 1 door is locked, the other 2 are open!");
System.out.println("HOWEVER 1 contains a bomb! (fizz!) the other contains the key to the locked door! choose wiseley!");
}
}else{
if (Room_1_South_Door_Locked == true){
if (Temp_Split[1].equals("KEY")){
if (Picked_Up_Key){
System.out.println("Door UnLocked");
Room_1_South_Door_Locked= false;
}
}
}else{
System.out.println("Door Already UnLocked");
}
}
}
}
}
}
}
if (Temp_Split[0].equals("MOVE")){
if (Temp_Split[1].equals("EAST")){
// R2 - GOTO ROOM 1
Current_Room = "ROOM 1";
System.out.println("WELCOME TO ROOM " + Current_Room);
}else{
System.out.println("NO ROOM TO THE " + Temp_Split[1]);
}
}else{
if (Temp_Split[0].equals("LOOK")){
System.out.println("There is a Door to the 'EAST'");
if (!Picked_Up_Key){
System.out.println("There is a 'KEY' on the floor");
}
}else{
if (Temp_Split[0].equals("PICKUP")){
if (Temp_Split[1].equals("KEY")){
System.out.println("You pickup up a '" + Temp_Split[1] + "'");
Picked_Up_Key = true;
}else{
System.out.println("There is no " + Temp_Split[1] + " to pick up");
}
}else{
if (Temp_Split[0].equals("USE")){
if (Temp_Split[1].equals("NOTE")){
if (Picked_Up_Note){
System.out.println("There are 3 doors in room 1, 1 door is locked, the other 2 are open!");
System.out.println("HOWEVER 1 contains a bomb! (fizz!) the other contains the key to the locked door! choose wiseley!");
}
}else{
if (Temp_Split[1].equals("KEY")){
if (Picked_Up_Key){
System.out.println("No Doors to UnLock");
}
}
}
}
}
}
}
}while(!Game_Over);
}
}