Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for TicTacToe Program in Java (some bugs may occur)


I am using Netbeans 4.0 to create a midlet app. The problem is that when I compile it tells me that java.text does not exist and I have a problem with Java.io. I will post code and output. PLEASE HELP!!!

CODE
package tictactoeserver;

import java.io.*;
import java.lang.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*; //error

public class TTTServlet extends HttpServlet { //error
     private Vector matches = new Vector();

     public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
          int msg, iMatch, iPlayer;
          String str;
     
          // get parameters
          str = request.getParameter("msg");
          if (str != null)
               msg = Integer.parseInt(str);
          else
               msg = Messages.NONE;
          str = request.getParameter("mid");
          if (str != null)
               iMatch = Integer.parseInt(str);
          else
               iMatch = -1;
          str = request.getParameter("pid");
          if (str != null)
               iPlayer = Integer.parseInt(str);
          else
               iPlayer = -1;
     
          response.setContentType("text/html");
     
          PrintWriter out = response.getWriter();//error
          // output the return value from getResponse
          out.print(getResponse(msg,iMatch,iPlayer));
     }

     /*
     * get the response to the message received in the request
     * @param msg the message code
     * @param iMatch index of the match in the matches vector
     * @param iPlayer index of the player who sent the message
     * @return String to send back to client
     */
     private String getResponse(int msg, int iMatch, int iPlayer) {
          String res = (new Integer(Messages.ERROR)).toString();
          // map to appropiate function according to the message
          if (msg >= 0 && msg < 9) // play messages
               res = doPlay(msg, iMatch, iPlayer);
          // map service messages
          switch (msg) {
               case Messages.HELLO: // just logged in
                    res = doHello();
                    break;
               case Messages.GOODBYE: // leaving the match
                    res = doGoodBye(iMatch, iPlayer);
                    break;
               case Messages.CONTINUE: // opponent has left, but player wants to continue
                    res = doContinue(iMatch, iPlayer);
                    break;
               case Messages.WIN: // the player has just won a game, update score and get first move
                    res = doWin(iMatch, iPlayer);
                    break;
               case Messages.DRAW: // draw game, get first move in new match
                    res = doDraw(iMatch, iPlayer);
                    break;
               case Messages.WAIT: // player is waiting for opponent, send current message
                    res = doWait(iMatch,iPlayer);
                    break;
               case Messages.RESTARTSERVER: // for debugging
                    matches.removeAllElements();
                    res = "Server Restarted";
                    break;
          }
          return res;
     }

     /*
     * get a match where one player is waiting for the opponent
     * @return a free match, or null if no free matches exist
     */
     private Match getFreeMatch() {
          int i;
          Match m;
          // loop through matches Vector
          for (i = 0; i < matches.size(); i++) {
               m = (Match)matches.elementAt(i);
               // check if the match is not empty (an empty match is a
               // match where both of the players have left)
               if (!m.getEmpty()) {
                    // check that the match is not full (a full match is a
                    // a match that already has two players)
                    if (!m.getFull()) {
                         return m;
                    }
               }
          }
          return null;
     }

     /*
     * get an empty match (where both players have already left)
     * @return an empty match, or null if there are no empty matches
     */
     private Match getEmptyMatch() {
          int i;
          Match m;
          for (i = 0; i < matches.size(); i++) {
               m = (Match)matches.elementAt(i);
               if (m.getEmpty() && !m.getFull()) {
                    return m;
               }
          }
          return null;
     }

     /*
     * process a new player logging in
     * @return response to be sent to that player
     */
     private String doHello() {
          int iMatch, iPlayer, msg;
          // look for a free match
          Match m = getFreeMatch();
          if (m == null) { // no free matches
               // look for an empty match
               m = getEmptyMatch();
               if (m == null) { // no empty match
                    // make a new match
                    m = new Match();
                    iMatch = matches.size();
                    matches.addElement(m);
               }
               else { // found an empty match
                    // get the match index
                    iMatch = matches.indexOf(m);
                    // set match as free
                    m.setEmpty(false);
                    m.setFull(false);
               }
               // first player to join the match plays X (0)
               iPlayer = 0;
               // msg = WAIT , need to wait for another player
               msg = Messages.WAIT;
          }
          else { // found a free match, let's start the game
               // second player to join the match plays O (0)
               iPlayer = 1;
               // get the match index
               iMatch = matches.indexOf(m);
               // msg = READY, ready to begin playing
               msg = Messages.READY;
               // set match as full (has 2 players)
               m.setFull(true);
          }
          m.setMessage(msg);
          return "mid=" + iMatch + "&pid=" + iPlayer + "&msg=" + m.getMessage();
     }

     /*
     * process a player logging out
     * @return response to be sent to that player
     */
     private String doGoodBye(int iMatch, int iPlayer) {
          Match m = (Match)matches.elementAt(iMatch);
          if (m.getEmpty()) {
               // second player to leave the match
               m.setFull(false); // match is ready to be reused
          }
          m.setEmpty(true);
          m.setMessage(Messages.GOODBYE);
          return "msg=" + m.getMessage();
     }

     /*
     * process a player continuing
     * just pass on the message to the next player
     * @return response to be sent to that player
     */
     private String doContinue(int iMatch, int iPlayer) {
          Match m = (Match)matches.elementAt(iMatch);
          m.setMessage(Messages.CONTINUE);
          return "msg=" + m.getMessage();
     }

     /*
     * process a player acknowlegding a win
     * just pass on the message to the next player
     * @return response to be sent to that player
     */
     private String doWin(int iMatch, int iPlayer) {
          Match m = (Match)matches.elementAt(iMatch);
          m.setMessage(Messages.WIN);
          return "msg=" + m.getMessage();
     }

     /*
     * process a player acknowledging a draw
     * just pass on the message to the next player
     * @return response to be sent to that player
     */
     private String doDraw(int iMatch, int iPlayer) {
          Match m = (Match)matches.elementAt(iMatch);
          m.setMessage(Messages.DRAW);
          return "msg=" + m.getMessage();
     }

     /*
     * process a regular move
     * just pass on the message to the next player
     * @return response to be sent to that player
     */
     private String doPlay(int msg, int iMatch, int iPlayer) {
          Match m = (Match)matches.elementAt(iMatch);
          if (m.getMessage() != Messages.WIN) {
               m.setMessage(msg);
               return "msg=" + m.getMessage();
          }
          else
          {
               return "msg=" + Messages.RESEND;
          }
     }

     /*
     * process a WAIT message (the player is waiting for his opponent to move)
     * just return the current message
     * @return response to be sent to that player
     */
     private String doWait(int iMatch, int iPlayer) {
          Match m = (Match)matches.elementAt(iMatch);
          return "msg=" + m.getMessage();
     }
}


OUTPUT
Compiling 7 source files to C:\Documents and Settings\obioku obotette\tictactoe\build\compiled
C:\Documents and Settings\obioku obotette\tictactoe\src\tictactoeserver\TTTServlet.java:8: package java.text does not exist
import java.text.*;
C:\Documents and Settings\obioku obotette\tictactoe\src\tictactoeserver\TTTServlet.java:10: cannot access java.io.Serializable
file java\io\Serializable.class not found
public class TTTServlet extends HttpServlet {
C:\Documents and Settings\obioku obotette\tictactoe\src\tictactoeserver\TTTServlet.java:38: cannot resolve symbol
symbol : class PrintWriter
location: class tictactoeserver.TTTServlet
PrintWriter out = response.getWriter();
3 errors

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!