The first time I saw something about Stack Overflow was while I was investigating a way to find HTML elements in xpath, in order to use them with Selenium. Couple days ago I finally created my account there and started using it. So far my impression has been very positive.
Stack Overflow is basically just a question-answer site for software developers. But because of its active user mass good questions are replied rapidly and bad ones are moderated. Users receive more privileges the more they are writing good questions and answers.
What is a good question or answer then? That is up to other users who can vote, edit or close your entry.
miercuri, 2 decembrie 2009
miercuri, 1 aprilie 2009
JAVA - joc: ghiceste un numar !
Jocul Ghiceste numarul
Se genereaza un numar aleator intre 0 - 10 (inclusiv) care se posteaza intr-un cookie. Utilizatorul introduce numele sau si incepe jocul. Primeste raspunsuri in legatura cu raspunsul sau: numarul este mai mare sau mai mic decat cel introdus. La final se afiseaza numarul de incercari facute pentru a ghici numarul.
Codul primei pagini: startGuessGame.jsp
html
<%@page import="javax.servlet.http.Cookie" %>
html
Codul paginii a doua: guessGame.jsp
html
<%@page import="javax.servlet.http.Cookie" %>
Bine ai venit, <%if(request.getAttribute("playerName") != null){
<%
if(request.getAttribute("eroare") != null){
out.print(request.getAttribute("eroare"));
}
if(request.getAttribute("mesaj") != null){
out.print(request.getAttribute("mesaj"));
}
%>
html
Codul servletului GuessGame.java
package teste;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;
/**
* Servlet implementation class GuessGame
*/
public class GuessGame extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int searchCookie(HttpServletRequest request, String cookieName){
int value = 0;
Cookie[] cookieArray = request.getCookies();
if(cookieArray != null){
for(int i = 0; i < cookieArray.length; i++){
String name = cookieArray[i].getName();
if(name.equals(cookieName)){
//ia valoarea cookie-ului si o pune intr-un String, apoi o transf in int:
String valCookie = cookieArray[i].getValue();
value = Integer.parseInt(valCookie);
}
}
}
return value;
}
/**
* @see HttpServlet#HttpServlet()
*/
public GuessGame() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doRequest(request, response);
}
protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int guessingNumber = 0;
int number = 0;
int numarIncercari = 0;
String mesaj = "";
String eroare = "";
String aux = "";
String playerName = request.getParameter("playerName");
log("playerName: " + playerName);
Random randomNumber = new Random();
if(playerName != null){
guessingNumber = randomNumber.nextInt(11);
} else {
guessingNumber = searchCookie(request,"guessNumber");
}
Cookie trialNumber = new Cookie("trialNumber", "trialNumber");
Cookie guessNumber = new Cookie("guessNumber", "guessNumber");
String initGuessCookie = Integer.toString(numarIncercari);
trialNumber.setValue(initGuessCookie);
String initNumberCookie = Integer.toString(guessingNumber);
guessNumber.setValue(initNumberCookie);
try{
String nr = request.getParameter("number");
number = Integer.parseInt(nr);
if(number numarIncercari++;
log("< incercari: " + numarIncercari);
} else if(number>guessingNumber){
numarIncercari++;
log("> incercari: " + numarIncercari);
} else {
numarIncercari++;
log("= incercari: " + numarIncercari);
}
} catch (NumberFormatException nfe) {
eroare = "Eroare! Introduceti un numar valid!";
} finally {
int value = 0;
if(number value = searchCookie(request,"trialNumber");
value++;
aux = Integer.toString(value);
trialNumber.setValue(aux);
mesaj = "Numarul este mai mic! Mai incearca! Incercari: "+ trialNumber.getValue();
log("Numarul de incercari este: "+ trialNumber.getValue());
} else if (number>guessingNumber){
value = searchCookie(request,"trialNumber");
value++;
aux = Integer.toString(value);
trialNumber.setValue(aux);
mesaj = "Numarul este mai mare! Mai incearca! Incercari: "+ trialNumber.getValue();
log("Numarul de incercari este: "+ trialNumber.getValue());
} else if(number == guessingNumber){
value = searchCookie(request,"trialNumber");
value++;
aux = Integer.toString(value);
trialNumber.setValue(aux);
mesaj = "Ati ghicit numarul! Incercari: "+ trialNumber.getValue();
log("Numarul de incercari este: "+ trialNumber.getValue());
trialNumber.setValue("0");
}
request.setAttribute("mesaj", mesaj);
request.setAttribute("eroare", eroare);
request.setAttribute("playerName", playerName);
response.addCookie(trialNumber);
response.addCookie(guessNumber);
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( "/teste/guessGame.jsp" );
dispatcher.forward( request, response );
}
}
}
Se genereaza un numar aleator intre 0 - 10 (inclusiv) care se posteaza intr-un cookie. Utilizatorul introduce numele sau si incepe jocul. Primeste raspunsuri in legatura cu raspunsul sau: numarul este mai mare sau mai mic decat cel introdus. La final se afiseaza numarul de incercari facute pentru a ghici numarul.
Codul primei pagini: startGuessGame.jsp
html
<%@page import="javax.servlet.http.Cookie" %>
Introduceti numele dumneavoastra:
html
Codul paginii a doua: guessGame.jsp
html
<%@page import="javax.servlet.http.Cookie" %>
Bine ai venit, <%if(request.getAttribute("playerName") != null){
out.print(request.getAttribute("playerName")); } %> !
Introduceti in textfield un numar intre 0 - 10!
<%
if(request.getAttribute("eroare") != null){
out.print(request.getAttribute("eroare"));
}
if(request.getAttribute("mesaj") != null){
out.print(request.getAttribute("mesaj"));
}
%>
html
Codul servletului GuessGame.java
package teste;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;
/**
* Servlet implementation class GuessGame
*/
public class GuessGame extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int searchCookie(HttpServletRequest request, String cookieName){
int value = 0;
Cookie[] cookieArray = request.getCookies();
if(cookieArray != null){
for(int i = 0; i < cookieArray.length; i++){
String name = cookieArray[i].getName();
if(name.equals(cookieName)){
//ia valoarea cookie-ului si o pune intr-un String, apoi o transf in int:
String valCookie = cookieArray[i].getValue();
value = Integer.parseInt(valCookie);
}
}
}
return value;
}
/**
* @see HttpServlet#HttpServlet()
*/
public GuessGame() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doRequest(request, response);
}
protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int guessingNumber = 0;
int number = 0;
int numarIncercari = 0;
String mesaj = "";
String eroare = "";
String aux = "";
String playerName = request.getParameter("playerName");
log("playerName: " + playerName);
Random randomNumber = new Random();
if(playerName != null){
guessingNumber = randomNumber.nextInt(11);
} else {
guessingNumber = searchCookie(request,"guessNumber");
}
Cookie trialNumber = new Cookie("trialNumber", "trialNumber");
Cookie guessNumber = new Cookie("guessNumber", "guessNumber");
String initGuessCookie = Integer.toString(numarIncercari);
trialNumber.setValue(initGuessCookie);
String initNumberCookie = Integer.toString(guessingNumber);
guessNumber.setValue(initNumberCookie);
try{
String nr = request.getParameter("number");
number = Integer.parseInt(nr);
if(number
log("< incercari: " + numarIncercari);
} else if(number>guessingNumber){
numarIncercari++;
log("> incercari: " + numarIncercari);
} else {
numarIncercari++;
log("= incercari: " + numarIncercari);
}
} catch (NumberFormatException nfe) {
eroare = "Eroare! Introduceti un numar valid!";
} finally {
int value = 0;
if(number
value++;
aux = Integer.toString(value);
trialNumber.setValue(aux);
mesaj = "Numarul este mai mic! Mai incearca! Incercari: "+ trialNumber.getValue();
log("Numarul de incercari este: "+ trialNumber.getValue());
} else if (number>guessingNumber){
value = searchCookie(request,"trialNumber");
value++;
aux = Integer.toString(value);
trialNumber.setValue(aux);
mesaj = "Numarul este mai mare! Mai incearca! Incercari: "+ trialNumber.getValue();
log("Numarul de incercari este: "+ trialNumber.getValue());
} else if(number == guessingNumber){
value = searchCookie(request,"trialNumber");
value++;
aux = Integer.toString(value);
trialNumber.setValue(aux);
mesaj = "Ati ghicit numarul! Incercari: "+ trialNumber.getValue();
log("Numarul de incercari este: "+ trialNumber.getValue());
trialNumber.setValue("0");
}
request.setAttribute("mesaj", mesaj);
request.setAttribute("eroare", eroare);
request.setAttribute("playerName", playerName);
response.addCookie(trialNumber);
response.addCookie(guessNumber);
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( "/teste/guessGame.jsp" );
dispatcher.forward( request, response );
}
}
}
Abonați-vă la:
Postări (Atom)