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}
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}