Skip to content

Commit

Permalink
Add New Support for Playing Scramble
Browse files Browse the repository at this point in the history
  • Loading branch information
richeso committed Jun 15, 2010
1 parent d9251c4 commit c3ea334
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 24 deletions.
2 changes: 1 addition & 1 deletion WebContent/pages/Play.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<table border="1" width="30%">
<th>Selected Scrambled Word</th>
<tr>
<td><b>${it}</b></td>
<td><b>${it.scrambledWord}</b></td>
</tr>

</table>
Expand Down
30 changes: 17 additions & 13 deletions WebContent/pages/PlayResults.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@
<title>Guess Scramble Word</title>
</head>
<body>
<h1>Guess the Selected Word</h1>
<h1>${it.message}</h1>

<table border="1" width="30%">
<th>Selected Scrambled Word</th>
<th>Number Correct</th>
<th>Number Wrong</th>
<tr>
<td><b>${it}</b></td>
<td><b>${it.numCorrect}</b></td>
<td><b>${it.numWrong}</b></td>
</tr>
<tr>
<td align="left" colspan="2">
<a href="/jersey/hibernate/play/again">Play Scramble Again</a>
</td>
</tr>
<tr>
<td align="left" colspan="2">
<a href="/jersey/hibernate/list">Update Word Dictionary</a>
</td>
</tr>

</table>
<br></br>
<hr></hr>
<p>Enter Your Guess for the Unscrambled Word and then click Submit Guess</p>

<form action="../hibernate/play/guess" method="POST">
<label for="guess">Submit Guess</label>
<input name="guess" />
<input type="submit" value="guess" />
</form>
<hr></hr>


</body>
78 changes: 78 additions & 0 deletions src/example/data/PlayResults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package example.data;

import java.io.Serializable;

public class PlayResults extends Object implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 8808259418494243920L;
private Word selectedWord;
private int score;
private String scrambledWord;
private int numWrong;
private int numCorrect;
private String message;
public PlayResults (Word aWord) {
init(aWord);
}
private void init(Word aWord) {
score = 0;
selectedWord = aWord;
scrambledWord = selectedWord.scrambled();
numCorrect = 0;
numWrong = 0;
message = "" ;
}
public Word getSelectedWord() {
return selectedWord;
}
public void setSelectedWord(Word selectedWord) {
this.selectedWord = selectedWord;
this.scrambledWord = selectedWord.scrambled();
this.message = "";
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getScrambledWord() {
return scrambledWord;
}
public void setScrambledWord(String scrambledWord) {
this.scrambledWord = scrambledWord;
}
public int getNumWrong() {
return numWrong;
}
public void setNumWrong(int numWrong) {
this.numWrong = numWrong;
}
public int getNumCorrect() {
return numCorrect;
}
public void setNumCorrect(int numCorrect) {
this.numCorrect = numCorrect;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void correct() {
++numCorrect;
setMessage("You are correct !!");
setScore(getScore()+getSelectedWord().getPoints().intValue());
}
public void wrong() {
++numWrong;
setMessage("You are incorrect !!");
}

}
4 changes: 1 addition & 3 deletions src/example/resources/Administer.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Viewable getWordList() {
@Path("save")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Viewable newContact(
public Viewable newWord(
@FormParam("value") String value,
@FormParam("points") String points,
@Context HttpServletResponse servletResponse
Expand All @@ -77,6 +77,4 @@ public Viewable newContact(
//servletResponse.sendRedirect("../pages/new_contact.html");
return getWordList();
}


}
47 changes: 40 additions & 7 deletions src/example/resources/Play.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand All @@ -22,6 +24,7 @@
import com.sun.jersey.spi.inject.Inject;

import example.dao.DictionaryDao;
import example.data.PlayResults;
import example.data.Word;
import example.data.WordList;
//The Java class will be hosted at the URI path "/hibernate/Play"
Expand Down Expand Up @@ -51,20 +54,50 @@ public Viewable init() {
int numwords = words.size();
int random = getRandom(0,numwords-1);
Word selectedWord = words.get(random);
PlayResults playResults = new PlayResults(selectedWord);
// store selected word in the session
servletRequest.getSession().setAttribute("selectedWord",selectedWord);
String scrambled = selectedWord.scrambled();
return new Viewable("/pages/Play.jsp",scrambled);
servletRequest.getSession().setAttribute("playResults",playResults);
return new Viewable("/pages/Play.jsp",playResults);
}
@GET
@Path("again")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Viewable play_again() {
List<Word> words = dictionaryDao.getWords();
int numwords = words.size();
int random = getRandom(0,numwords-1);
Word selectedWord = words.get(random);
// retrieve selected word in the session
PlayResults playResults = (PlayResults)
servletRequest.getSession().getAttribute("playResults");
// if no session data - go back to init
if (playResults == null)
return init();

playResults.setSelectedWord(selectedWord);
servletRequest.getSession().setAttribute("playResults",playResults);
return new Viewable("/pages/Play.jsp",playResults);
}

@POST
@Path("guess")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Viewable guess() {
String message = "This is a message";
return new Viewable("/pages/PlayResults.jsp",message);
}
public Viewable guess(
@FormParam("guess") String guess,
@Context HttpServletResponse servletResponse) {

// retrieve selected word in the session
PlayResults playResults = (PlayResults)
servletRequest.getSession().getAttribute("playResults");
if (guess != null && (guess.trim().equals(playResults.getSelectedWord().getValue().trim()))) {
playResults.correct();
} else {
playResults.wrong();
}
return new Viewable("/pages/PlayResults.jsp",playResults);
}

private static int getRandom(int min, int max) {
Random rand = new Random(System.currentTimeMillis());
Expand Down

0 comments on commit c3ea334

Please sign in to comment.