%@ page import ="java.util.*" %> <% String url = "http://www.cs.indiana.edu/classes/a202-dger/fall2005/notes/flagquiz/images/"; String message = (String) session.getAttribute("message"), key = (String) session.getAttribute("key"), good = (String) session.getAttribute("good"), total = (String) session.getAttribute("total"), b = (String) session.getAttribute("questions"), monthClient = request.getParameter("monthClient"), monthServer = (String) session.getAttribute("monthServer"), input = "", answer = request.getParameter("answer"); if (message == null) { // initialize state monthServer = "1"; message = "Welcome to the game, are you ready?"; String[] flags = { "Australia", "China", "Russia", "Spain", "Brazil", "Italy", "South Africa", "United States" }; for (int i = 0; i < 100; i++) { int index = (int) (Math.random() * flags.length); int other = (int) (Math.random() * flags.length); String temp = flags[index]; flags[index] = flags[other]; flags[other] = temp; } b = ""; for (int i = 0; i < flags.length; i++) { b += flags[i]; if (i < flags.length - 1) b += ","; } // out.println(b + "
"); good = "0"; total = "0"; } else { // read input, update state boolean match; try { match = Integer.parseInt(monthServer) == Integer.parseInt(monthClient); } catch (Exception e) { match = false; } if (match) { monthServer = Integer.parseInt(monthServer) + 1 + ""; // grade first, so you can ask a new question if (answer != null) { if (key.equals(answer)) good = (Integer.parseInt(good) + 1) + ""; out.println("I am comparing " + key + " with " + answer + "
"); total = (Integer.parseInt(total) + 1) + ""; } // else: answer is null, don't do anything // now ask a new question StringTokenizer st = new StringTokenizer( b , ","); if (st.hasMoreTokens()) { key = st.nextToken(); b = ""; while (st.hasMoreTokens()) { b += st.nextToken(); if (st.hasMoreTokens()) b += ","; } out.println("I am asking about " + key + " and I am keeping " + b + " for later
");
input = "\n " + " is the flag of
"; message = "Score so far: " + good + " out of " + total ; } else { // running out of questions out.println ("I bet you can see me now.
"); message = "Score so far: " + good + " out of " + total ; // bernard says: initialize! message += "
Welcome to the game, are you ready?"; String[] flags = { "Australia", "China", "Russia", "Spain", "Brazil", "Italy", "South Africa", "United States" }; for (int i = 0; i < 100; i++) { int index = (int) (Math.random() * flags.length); int other = (int) (Math.random() * flags.length); String temp = flags[index]; flags[index] = flags[other]; flags[other] = temp; } b = ""; for (int i = 0; i < flags.length; i++) { b += flags[i]; if (i < flags.length - 1) b += ","; } // out.println(b + "
"); good = "0"; total = "0"; } // running out of questions } else { out.println("No reloads are allowed.
");
if (message.indexOf("Welcome") >= 0) {
} else {
input = "\n " + " is the flag of
"; } } } // end of read input, update state session.setAttribute("message", message); session.setAttribute("monthServer", monthServer); session.setAttribute("questions", b); session.setAttribute("key", key); session.setAttribute("good", good); session.setAttribute("total", total); %>