Back to project page game_guess_lib.
The source code is released under:
MIT License
If you think the Android project game_guess_lib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.wkmf.guess.lib.structure; /*w w w . j ava 2 s . c om*/ import android.app.ProgressDialog; import com.wkmf.guess.lib.common.async.DownloadTask; import com.wkmf.guess.lib.impl.GuessGameImageDownload; import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; import java.text.Collator; import java.util.List; import java.util.Random; import java.util.UUID; /** * Created by ernestofndz on 9/02/14. */ @JsonIgnoreProperties(ignoreUnknown = true) public class GuessQuestion { public static final int TAGS_MAX = 3; private static final String IMG_EXT = ".png"; // extensin de las imgenes private static final String EMPTY = ""; @JsonProperty("id") private String id; @JsonProperty("img_src") private String imageURL; @JsonProperty("img_path") private String imagePath; @JsonProperty("answer") private String answer; @JsonProperty("tags") private List<String> tags; @JsonProperty("tags_added") private int tagsAdded = 0; @JsonProperty("completed") private boolean completed = false; @JsonProperty("part_of_answer") private String partOfAnswer = EMPTY; // constructor public GuessQuestion(String id, String imageURL, String imagePath, String answer, List<String> tags, int tagsAdded, boolean completed, String partOfAnswer) { this.id = id; this.imageURL = imageURL; this.imagePath = imagePath; this.answer = answer; this.tags = tags; this.tagsAdded = tagsAdded; this.completed = completed; this.partOfAnswer = partOfAnswer; } public GuessQuestion() { } // descargamos la imagen public void downloadImage(GuessGameImageDownload callback, ProgressDialog pd) { new DownloadTask(callback, this.imageURL, pd).execute(); } // generar la ruta al fichero public void generateImagePath() { this.imagePath = String.valueOf(UUID.randomUUID()).concat(IMG_EXT); } // comprobar una respuesta public boolean checkAnswer(String answer) { Collator collator = Collator.getInstance(); collator.setStrength(Collator.PRIMARY); // comparamos, limpiamos tanto la respuesta correcta como el intento del usuario return collator.compare(clean(this.answer), clean(answer)) == 0; } // limpiar la respuesta private String clean(String str) { return str.trim().replaceAll("[^\\w\\s]", EMPTY).replaceAll(" ", EMPTY); } // aadir un tag public void addTag() { this.tagsAdded++; } // revelar parte de la respuesta public void revealPartOfAnswer() { int revealed = 0; int size = this.answer.length(); int numLetters = size / 2; // obtenemos la 4ta parte char[] answer = this.answer.toCharArray(); char[] partOfAnswer = (this.answer.replaceAll("[^\\d]", "_")).toCharArray(); while (revealed < numLetters) { for (int i = 0; i < size; i++) { if (answer[i] == ' ' || partOfAnswer[i] == '_' && (new Random().nextBoolean() && new Random().nextBoolean())) { if (answer[i] != ' ') revealed++; partOfAnswer[i] = answer[i]; } if (revealed == numLetters) break; } } // seteamos el valor this.partOfAnswer = String.valueOf(partOfAnswer); } // comprobar si ya tiene solicitado parte de la respuesta public boolean hasPartOfAnswer() { return !this.partOfAnswer.isEmpty(); } // get/set public String getId() { return id; } public void setId(String id) { this.id = id; } public String getImageURL() { return imageURL; } public void setImageURL(String imageURL) { this.imageURL = imageURL; } public String getAnswer() { return answer; } public void setAnswer(String answer) { this.answer = answer; } public List<String> getTags() { return tags; } public void setTags(List<String> tags) { this.tags = tags; } public boolean isCompleted() { return completed; } public void setCompleted(boolean completed) { this.completed = completed; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } public int getTagsAdded() { return tagsAdded; } public void setTagsAdded(int tagsAdded) { this.tagsAdded = tagsAdded; } public String getPartOfAnswer() { return partOfAnswer; } }