Created
December 5, 2011 11:23
-
-
Save kaldas/1433283 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Atom { | |
private final int atomValue; | |
public Atom() { | |
this.atomValue = (int) (1+Math.random()*1000); | |
} | |
public Atom(int playerInput) { | |
this.atomValue = playerInput; | |
} | |
public int getAtomValue() { | |
return atomValue; | |
} | |
} | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
@WebServlet(name = "GameHandler", urlPatterns = {"/GameHandler"}) | |
public class GameHandler extends HttpServlet { | |
/*************************************************** | |
* User input should be something like: | |
* ex: parseInListToArray("4-8-15-16-23-42") | |
*/ | |
private static MemGame memGameObj = new MemGame(); | |
private String[] parseInStringToArray(String playerGameInput) { | |
String[] tokens = playerGameInput.split("-"); | |
return tokens; | |
} | |
private ArrayList<Atom> parseInArrayToAtom(String[] playerGameArray) { | |
ArrayList<Atom> playerInputAtom = new ArrayList<Atom>(); | |
for(int cnt=0;cnt < playerGameArray.length;cnt++) { | |
Atom newAtom = new Atom(Integer.parseInt(playerGameArray[cnt])); | |
playerInputAtom.add(newAtom); | |
} | |
return playerInputAtom; | |
} | |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
response.setContentType("text/html;charset=UTF-8"); | |
PrintWriter out = response.getWriter(); | |
ArrayList<Atom> playerGameList = parseInArrayToAtom | |
(parseInStringToArray("1-2-3")); | |
out.println("LNSTART:" + memGameObj.checkMove(playerGameList) + "LNEND"); | |
/* | |
if(memGameObj.checkMove(playerGameList)) { | |
out.println("true"); | |
} else { | |
out.println("false"); | |
}*/ | |
for(Atom nAtom : playerGameList) { | |
out.println("IN:" + nAtom.getAtomValue() + "-"); | |
} | |
out.println("OUT:" + memGameObj.dbug()); | |
out.close(); | |
} | |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> | |
/** | |
* Handles the HTTP <code>GET</code> method. | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Handles the HTTP <code>POST</code> method. | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Returns a short description of the servlet. | |
* @return a String containing servlet description | |
*/ | |
@Override | |
public String getServletInfo() { | |
return "Short description"; | |
}// </editor-fold> | |
} | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
public class MemGame { | |
private ArrayList<Atom> atomLocal = new | |
ArrayList<Atom>(); | |
public MemGame() { | |
//this.newAtom(); | |
Atom newAtom = new Atom(1); | |
atomLocal.add(newAtom); | |
Atom newAtom2 = new Atom(2); | |
atomLocal.add(newAtom2); | |
Atom newAtom3 = new Atom(2); | |
atomLocal.add(newAtom3); | |
} | |
private void newAtom() { | |
Atom newAtom = new Atom(); | |
this.atomLocal.add(newAtom); | |
} | |
public static void main() { | |
System.out.println("lol"); | |
} | |
public String checkMove(ArrayList<Atom> atomRemote) { | |
String str = ""; | |
Iterator<Atom> itLocal = atomLocal.iterator(); | |
Iterator<Atom> itRemote = atomRemote.iterator(); | |
while(itLocal.hasNext() && itRemote.hasNext()) { | |
Atom localAtom = itLocal.next(); | |
Atom remoteAtom = itRemote.next(); | |
if(localAtom.getAtomValue() != remoteAtom.getAtomValue()) { | |
str = str + "<error: " + localAtom.getAtomValue() + "!=" + | |
remoteAtom.getAtomValue(); | |
return str; | |
} | |
} | |
str = "funcionou;"; | |
return str; | |
} | |
public String dbug() { | |
String d = ""; | |
for(Atom nAtom : atomLocal) { | |
d = d + nAtom.getAtomValue(); | |
} | |
return d; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment