public void showResult(int count) { Image img = null; int imageX, imageY = 0; // 6 for system tray, and then account for all 4 results imageX = 89 - imageBull.getWidth() * 4 - imageInter.getWidth(); if (gamemode == gameModeBasic) { for (int i = 0; i < result[count - 1].length(); i++) { switch (result[count - 1].charAt(i)) { case 'b': img = imageBull; break; case 'w': img = imageCow; break; case '-': img = imageWrong; break; } displayImageAtXY(img, imageX, imageY); imageX += img.getWidth(); } } else { // number of bulls int bulls = Character.digit(result[count - 1].charAt(0), 10); int cows = Character.digit(result[count - 1].charAt(2), 10); int imageXB = imageX, imageXW = imageX; int imageYB = 0, imageYW = 0; for (int i = 0; i < bulls; i++) { displayImageAtXY(imageBull, imageXB, imageYB); imageXB += imageBull.getWidth(); } if (bulls != 0) { imageYW = imageBull.getHeight(); } for (int i = 0; i < cows; i++) { displayImageAtXY(imageCow, imageXW, imageYW); imageXW += imageCow.getWidth(); } if (bulls == 0 && cows == 0) { displayImageAtXY(imageWrong, imageX, imageY); } } }
public void write(char ch) throws IOException { boolean ok; switch (state) { case S_text: if (ch == '\n' || ch == '\r') { break; // unadorned newlines are ignored } else if (ch == '{') { if (currentCharacters.length() > 0) { handleText(currentCharacters.toString()); currentCharacters = new StringBuffer(); } level++; begingroup(); } else if (ch == '}') { if (currentCharacters.length() > 0) { handleText(currentCharacters.toString()); currentCharacters = new StringBuffer(); } if (level == 0) throw new IOException("Too many close-groups in RTF text"); endgroup(); level--; } else if (ch == '\\') { if (currentCharacters.length() > 0) { handleText(currentCharacters.toString()); currentCharacters = new StringBuffer(); } state = S_backslashed; } else { currentCharacters.append(ch); } break; case S_backslashed: if (ch == '\'') { state = S_aftertick; break; } if (!Character.isLetter(ch)) { char newstring[] = new char[1]; newstring[0] = ch; if (!handleKeyword(new String(newstring))) { warning("Unknown keyword: " + newstring + " (" + (byte) ch + ")"); } state = S_text; pendingKeyword = null; /* currentCharacters is already an empty stringBuffer */ break; } state = S_token; /* FALL THROUGH */ case S_token: if (Character.isLetter(ch)) { currentCharacters.append(ch); } else { pendingKeyword = currentCharacters.toString(); currentCharacters = new StringBuffer(); // Parameter following? if (Character.isDigit(ch) || (ch == '-')) { state = S_parameter; currentCharacters.append(ch); } else { ok = handleKeyword(pendingKeyword); if (!ok) warning("Unknown keyword: " + pendingKeyword); pendingKeyword = null; state = S_text; // Non-space delimiters get included in the text if (!Character.isWhitespace(ch)) write(ch); } } break; case S_parameter: if (Character.isDigit(ch)) { currentCharacters.append(ch); } else { /* TODO: Test correct behavior of \bin keyword */ if (pendingKeyword.equals("bin")) { /* magic layer-breaking kwd */ long parameter = Long.parseLong(currentCharacters.toString()); pendingKeyword = null; state = S_inblob; binaryBytesLeft = parameter; if (binaryBytesLeft > Integer.MAX_VALUE) binaryBuf = new ByteArrayOutputStream(Integer.MAX_VALUE); else binaryBuf = new ByteArrayOutputStream((int) binaryBytesLeft); savedSpecials = specialsTable; specialsTable = allSpecialsTable; break; } int parameter = Integer.parseInt(currentCharacters.toString()); ok = handleKeyword(pendingKeyword, parameter); if (!ok) warning("Unknown keyword: " + pendingKeyword + " (param " + currentCharacters + ")"); pendingKeyword = null; currentCharacters = new StringBuffer(); state = S_text; // Delimiters here are interpreted as text too if (!Character.isWhitespace(ch)) write(ch); } break; case S_aftertick: if (Character.digit(ch, 16) == -1) state = S_text; else { pendingCharacter = Character.digit(ch, 16); state = S_aftertickc; } break; case S_aftertickc: state = S_text; if (Character.digit(ch, 16) != -1) { pendingCharacter = pendingCharacter * 16 + Character.digit(ch, 16); ch = translationTable[pendingCharacter]; if (ch != 0) handleText(ch); } break; case S_inblob: binaryBuf.write(ch); binaryBytesLeft--; if (binaryBytesLeft == 0) { state = S_text; specialsTable = savedSpecials; savedSpecials = null; handleBinaryBlob(binaryBuf.toByteArray()); binaryBuf = null; } } }
/** * When the user pauses (pause time specified by the wizard), all strokes in the ICRContext are * cleared */ public void hwrUserPause(long time, String result) { // only process when there is a code to crack if (gamestate == gameinprogress) { // remove any spaces from the string recognized by the pen result = result.trim(); String newresult = ""; for (int i = 0; i < result.length(); i++) { if (result.charAt(i) != ' ') { newresult += result.charAt(i); } } // add the result to the one existing guess[guesscount - 1] += newresult; int guesslength = guess[guesscount - 1].length(); /* now sort based on x coordinates of each digits. * split into array of char, then do bubble sort in each char * once complete, reassemble the data to the represent * the value written by the user */ char[] tempresultc; tempresultc = new char[guesslength]; // extract the individual characters from the guess for (int i = 0; i < guesslength; i++) { tempresultc[i] = guess[guesscount - 1].charAt(i); } /* bubble sort all the individual digits based on the xcoordinates * stored during the stroke creation process */ int tempx; char tempc; for (int i = 0; i < numberofxcoordinates; i++) { for (int j = 0; j < numberofxcoordinates - 1; j++) { if (xcoordinates[j] > xcoordinates[j + 1]) { // swap the coordinates tempx = xcoordinates[j]; xcoordinates[j] = xcoordinates[j + 1]; xcoordinates[j + 1] = tempx; // swap the digits tempc = tempresultc[j]; tempresultc[j] = tempresultc[j + 1]; tempresultc[j + 1] = tempc; } } } // now add tempresult back in the correct order guess[guesscount - 1] = ""; for (int i = 0; i < guesslength; i++) { guess[guesscount - 1] += tempresultc[i]; } guesstodisplay = guesscount; int length = guess[guesscount - 1].length(); if (length == 4) { /* we have enough digits. if abortCode then play code and game over otherwise compare. */ numberofxcoordinates = 0; if (guess[guesscount - 1].compareTo(abortCode) != 0) { int[] guessdigitcount; String[] tempresult; guessdigitcount = new int[10]; tempresult = new String[4]; for (int j = 0; j < 10; j++) { guessdigitcount[j] = 0; } // initialize the temp result for (int j = 0; j < 4; j++) { tempresult[j] = "-"; } // go through all the guess and find the blacks for (int i = 0; i < 4; i++) { char digit = guess[guesscount - 1].charAt(i); int intvalueofdigit; if (digit == codetocrack.charAt(i)) { intvalueofdigit = Character.digit(digit, 10); guessdigitcount[intvalueofdigit]++; tempresult[i] = "b"; } } // now for any place that does not have a black, check for white only if count not // exceeded for (int i = 0; i < 4; i++) { boolean matchfound = false; char digit = guess[guesscount - 1].charAt(i); int intvalueofdigit = Character.digit(digit, 10); if (tempresult[i] == "-") { if (guessdigitcount[intvalueofdigit] < codedigitcount[intvalueofdigit]) { guessdigitcount[intvalueofdigit]++; for (int j = 0; j < 4 && !matchfound; j++) { char code = codetocrack.charAt(j); if (i != j) { if (digit == code) { tempresult[i] = "w"; matchfound = true; } } } } } this.result[guesscount - 1] += tempresult[i]; } /* if we are in advanced game mode, then do not reveal the * position of the bulls & cows. just give the total count * in the format <n>b<m>w. example 3b1w or 3b0w */ if (gamemode == gameModeInter || gamemode == gameModeAdvanced) { int bcount = 0, wcount = 0; for (int i = 0; i < 4; i++) { String currchar = this.result[guesscount - 1].substring(i, i + 1); if (currchar.equals("w")) { wcount++; } else if (currchar.equals("b")) { bcount++; } } this.result[guesscount - 1] = String.valueOf(bcount) + "b" + String.valueOf(wcount) + "w"; } if (this.result[guesscount - 1].equals("bbbb") || (this.result[guesscount - 1].equals("4b0w"))) { codeBroken(); } else if (guesscount != maxGuesses) { promptForGuess(guesscount); guesscount++; } else { // all ten attempts exhausted gameOver(); } } else { // user has entered the abort code gameAbort(); } } else if (length > 4) { // cancel the entries guess[guesscount - 1] = ""; numberofxcoordinates = 0; promptForGuess(guesscount); playSound("toomanydigits"); } else { updateGuessDisplay(guesscount); playDigits(newresult); } } this.icrContext.clearStrokes(); }