Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple scramble game using jsp

Status
Not open for further replies.

radiapo

Programmer
Nov 29, 2008
2
GB
Hi. I need to do a scramble game.
Simply I need to randomly select word from my set of strings and print out in shuffled way. Such as if the word is "banana" it should print out something like "anaban" and there should be text field for user to enter and guess the correct word and submit button. I also need to keep record correct and wrong number of guesses.
I am quite new to the jsp and couldn't go much further. Please help =(

my current code is:
{code}
<HTML>
<BODY>

<form method="post">

<table>
<tr><td><b>Guess</b>
<td><input type="text" name="guess">

<input type="submit" value="Submit">
</table>

</form>

<%@ page import="java.util.Random" %>


<%!
int game = 0;
String target = null;
String shuff = null;
String guessed = null;

int corrects = 0;
int wrongs = 0;
String [] list = {"computer", "macintosh", "apple", "banana", "laptop"};
Random rand = new Random();
%>



<%
//correct = session.setAttribute("correct", correct);
//wrong = session.setAttribute("wrong", wrong);
out.println("Welcome to Anagram Game \n");
//String correct = (String) session.setAttribute("correct");
//String wrong = (String) session.setAttribute("wrong");
if (session.isNew())
{
out.println("Session is new /n");
target = list[rand.nextInt(5)];
shuff = getMixed(target);
//final String shuff1 = shuff;
session.setAttribute("target", target);
session.setAttribute("shuff", shuff );
out.println("Session is new2 /n");

out.println("The word is :" + " " + shuff);%> <br> <%
//out.println("The shuff1 is :" + " " + shuff1);%> <br> <%


}
else {
session.setAttribute("target", target);
session.setAttribute("shuff", shuff );
}
if (!session.isNew())
{
//target = list[rand.nextInt(5)];
//shuff = getMixed(target);

target = list[rand.nextInt(5)];
shuff = getMixed(target);
out.println("Session is in else /n ");



out.println("The word is :" + " " + shuff); %> <br> <%
}
String tempo = shuff;
guessed = request.getParameter("guess");
//String correct1 = (String) session.getAttribute("target");
//String wrong1 = (String) session.getAttribute("shuff");
//String head = request.getParameterValues("head");


out.println("The guessed is :" + " " + guessed); %> <br> <%
//out.println("The correct is :" + " " + correct1); %> <br> <%
//out.println("The wrong is :" + " " + wrong1); %> <br> <%
out.println("The shuff is :" + " " + shuff);%> <br> <%
out.println("The target is :" + " " + target);%> <br> <%
//out.println("The head is :" + " " + head);%> <br> <%

//out.println("The shuff1 2 is :" + " " + shuff1);%> <br> <%








if (guessed == target){
//out.println("Well done!");
corrects++;
}
else{
//out.println("Wrong!");
wrongs++;
}
game++;



%>
<h2>The number of game = <% out.println(" " + game); %></h2><br>
<h2>The number of correct guesses = <% out.println(" " + corrects); %></h2><br>
<h2>The number of wrong guesses = <% out.println(" " + wrongs); %></h2><br>


<%! public String getMixed(String target){

int len = target.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
for (int i = 0; i < len; i++) {
tempCharArray = target.charAt(i);
}
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len - 1 - j];
}
int r = rand.nextInt(charArray.length);
int N = (100 + rand.nextInt(1000)) * r;
for(int i = 0; i < N; i++) {
int idx1 = rand.nextInt(r);
int idx2 = rand.nextInt(r);
char temp1 = charArray[idx1];
charArray[idx1] = charArray[idx2];
charArray[idx2] = temp1;

}
String mixed = new String(charArray);
return mixed;

}
%>


</BODY>
</HTML>



{code}
 
<code>
<%@page import="java.util.Random"%>
<%
String newNumValue = request.getParameter("NEWNUM");
boolean shouldBeChecked = true;
int result = 0;
if (newNumValue!=null && newNumValue.length()>0){
if (newNumValue.equals("Y")){
shouldBeChecked = true;
Random generator = new Random();
int maxNumber = 47;
double r = generator.nextDouble();
result = (int)(47*r +1);
}
else{
shouldBeChecked = false;
}
}
%>
<HTML>
<HEAD></HEAD>
<FORM METHOD="POST">
Generate new Number<INPUT TYPE="RADIO" NAME="NEWNUM" VALUE="Y" <%if (shouldBeChecked) {%>
CHECKED
<%}%>>
&nbsp;&nbsp;Let me Guess<INPUT TYPE="RADIO" NAME="NEWNUM" VALUE="N" <%if (!shouldBeChecked) {%>
CHECKED
<%}%>>
<BR>
<%
if (!shouldBeChecked) {
out.println("The new number is "+result+"<BR>");
}
else{
out.println("The number is "+result+"<BR>");
}
String myNumberStr = request.getParameter("MYNUMBER");
String resultStr = String.valueOf(result);
if (myNumberStr!=null && myNumberStr.length()>0){
if (resultStr.equals(myNumberStr)){
out.println("You have got the correct answer!<BR>");
}
else{
out.println("Incorrect answer<BR>");
}
}
%>
My Guess is
<INPUT TYPE="TEXT" NAME="MYNUMBER">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="SUBMIT">
</FORM>
</HTML>
</code>

You need to have radio button to let user decide if new guess is needed. You may put my jsp file into your folder to test my program.
 
Code:
<%@page import="java.util.Random"%>
<%
String newNumValue = request.getParameter("NEWNUM");
boolean shouldBeChecked = true;
int result = 0;
if (newNumValue!=null && newNumValue.length()>0){
   if (newNumValue.equals("Y")){
      shouldBeChecked = true;
      Random generator = new Random();
      int maxNumber = 47;
      double r = generator.nextDouble();
      result = (int)(47*r +1);
   }
   else{
        shouldBeChecked = false;
   }
}
%>
<HTML>
<HEAD></HEAD>
<FORM METHOD="POST">
Generate new Number<INPUT TYPE="RADIO" NAME="NEWNUM" VALUE="Y" <%if (shouldBeChecked) {%>
CHECKED
<%}%>>
&nbsp;&nbsp;Let me Guess<INPUT TYPE="RADIO" NAME="NEWNUM" VALUE="N" <%if (!shouldBeChecked) {%>
CHECKED
<%}%>>
<BR>
<%
if (!shouldBeChecked) {
    out.println("The new number is "+result+"<BR>");
}
else{
    out.println("The number is "+result+"<BR>");
}
String myNumberStr = request.getParameter("MYNUMBER");
String resultStr = String.valueOf(result);
if (myNumberStr!=null && myNumberStr.length()>0){
   if (resultStr.equals(myNumberStr)){
       out.println("You have got the correct answer!<BR>");
   }
   else{
        out.println("Incorrect answer<BR>");
   }
}
%>
My Guess is
<INPUT TYPE="TEXT" NAME="MYNUMBER">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="SUBMIT">
</FORM>
</HTML>

You need to have radio button to let user decide if new guess is needed. You may put my jsp file into your folder to test my program.
 
I think there is some bug in my logic. I will fix it later.
 
Hi. Thank you for the help.
But I dont think I need a radio button. What I need simply is that a scrambled word straight away comes up when the user clicks a "new game" button and user tries to guess by entering into a text box and it keeps asking for new scrambled words without clicking any button and keeps the number of the right and wrong guesses and represents to the user.
And whenever the user clicks on "new game" button, it clears the count of the correct and wrong guesses.

Thank you
 
If you use "new game" button, you may need to use JavaScript and HTML hidden field.
The following jsp is called hidden.jsp
Code:
<HTML>
<HEAD>
<script lang="JavaScript">
function mySetHid(){
document.FORM1.HIDDENFIELD.value="setnew";
}
</script>
</HEAD>
<%
String hiddenFieldStr = request.getParameter("HIDDENFIELD");
if (hiddenFieldStr!=null && hiddenFieldStr.length()>0){
    out.println("hidden field value is "+hiddenFieldStr);
}
%>
<FORM METHOD="POST" NAME="FORM1" action="hidden.jsp">
<INPUT TYPE="HIDDEN" NAME="HIDDENFIELD">
<INPUT TYPE="BUTTON" VALUE="New Game" onClick="mySetHid()">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="SUBMIT">
</FORM>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top