⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 284-287.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Implementing a High Score Server on a Network</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=8//-->
<!--PAGES=284-287//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="282-284.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="287-290.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Implementing Server Features</FONT></H4>
<P>Now that we understand the way the client will work, we must be sure that we have an effective model for our server, so we can certify that the two will work together nicely. Our server model should be one that compliments the strengths of the client.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Effect of Client Design on Server Performance</FONT></H4>
<P>The advantage of our client design above is that it requires very little processing by the server. This is ideal for a networking environment for two reasons.
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;First, the server could be handling potentially thousands of requests, and if it had to perform calculations on each one, it would quickly become bogged down. By distributing the computational load among all of the client computers, we decrease the burden on the server considerably.
<DD><B>&#149;</B>&nbsp;&nbsp;Second, the server itself need not be implemented in Java. Several methods of doing simple server requests are available, most commonly the Common Gateway Interface (CGI), which allows for server programs to be written in many languages. We will explore different methods of implementing our server later.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Tasks Performed by the Server</FONT></H4>
<P>No matter what language our server is written in, it still must perform the following tasks:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Provide a string of data representing all high scores upon request. This list may be in numerical order, but not necessarily so (remember that our client will parse and order the data later).
<DD><B>&#149;</B>&nbsp;&nbsp;Receive score and name information from clients and, if applicable, add them to the high score list.
<DD><B>&#149;</B>&nbsp;&nbsp;Keep track of the last time a change was made to the list of high scores, so that the client can check if it needs to request the data again.
</DL>
<H3><A NAME="Heading15"></A><FONT COLOR="#000077">Creating the High Score Objects</FONT></H3>
<P>The first step in creating both the client and the server is to create a series of object classes that can be used to effectively store, retrieve, and transmit high scores. For this, we will create a special class for a single high score, and a class for a list of many high scores.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">The HighScoreList Class</FONT></H4>
<P>The first class we need to write is one that will be used by both the server and the client. This class keeps track of a bunch of high scores and keeps them in order. Let&#146;s start with the most basic skeleton of the HighScoreList class, which we can put in a file called HighScoreList.java:
</P>
<!-- CODE //-->
<PRE>
import java.util.*;         // We will need these Java classes
import java.lang.*;


public class HighScoreList extends Object &#123;
int NUM_SCORES = 10;  // Number of scores - default value is ten
public long lastChange = 0;  //Last time a change was made to the list

HighScoreList() &#123;
&#125;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">Scoring Variables</FONT></H4>
<P>We have started with two important variables in HighScoreList. <I>NUM_SCORES</I>, as the name implies, will keep track of the number of scores in our list. The other number is a &#147;long&#148; integer (which means it can get really really really big) called <I>lastChange</I>. Even though Java provides a very extensive Date class that can be used to display dates and times, we don&#146;t need all of that functionality. Instead, we are only going to keep track of the number of milliseconds since the beginning of the current epoch. This is a huge number, and you probably would not want to have to memorize it, but computers <I>love</I>big numbers. Tracking this number gives us a convenient way to see if we need to ask for new data from the server. More on this later.</P>
<H4 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">The HSob Object Class</FONT></H4>
<P>Before we can have a list of high scores, we are going to need our high score object class, HSob, which is part of the HighScoreList class. Let&#146;s add the code for that.
</P>
<!-- CODE //-->
<PRE>
class HSob extends Object &#123; // High Score Object class

       public String name;  // Player's name
       public float score;  // Player's score
/* Remember that we can always add more information about the player&#8656;
later */

       HSob(String n, float sc) &#123;  //Initialization routines
             name=n;
             score=sc;
       &#125;

       HSob() &#123;
             name="noname";
             score=0;
       &#125;
       &#125;
</PRE>
<!-- END CODE //-->
<P>You may have noticed that this class has two different initialization routines. The first is used to construct a new object, based on data passed in the new() method. The second is used in case we want to create an object and then add the data later. Next, we should add the HighScoreList itself, which will be an array of HSob. Declare it like this:
</P>
<!-- CODE SNIP //-->
<PRE>
HSob scores[];
</PRE>
<!-- END CODE SNIP //-->
<P>Now let&#146;s write the initialization routines for the HighScoreList. We are going to provide two different ones, just as we did with HSob. The first will allow us to initialize a HighScoreList that will track a certain number of high scores. It looks like this:
</P>
<!-- CODE SNIP //-->
<PRE>
HighScoreList(int num) &#123;
       NUM_SCORES = num;
       scores = new HSob[NUM_SCORES];
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>The other routine will allow us to create a new HighScoreList that starts out with a certain number of high scores already in it. However, before we can create this routine, we are going to need a new method for parsing data.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Data Parsing</FONT></H4>
<P>When we get data from the server, it is going to be one long string of values. The art of &#147;parsing&#148; means taking a string of raw data and turning it into something useful (in this case, an array of high scores). Here is what a typical raw data string might look like:
</P>
<!-- CODE SNIP //-->
<PRE>
"Joe&#38;1000|Bob&#38;250|Gary&#38;52.3|Mary&#38;23|Gabe&#38;5|null|null"
</PRE>
<!-- END CODE SNIP //-->
<P>This is a string of seven scores. You may have noticed that they are in the form &#147;Name1&#38;score1|Name2&#38;score2|&#133;.&#148; We have rather arbitrarily chosen relatively uncommon sets of characters as <I>delimiters</I> to separate meaningful data. Each name/score pair is separated by &#147;|&#148;, and every name and score is separated by &#147;&#38;&#148;. Also, &#147;null&#148; is used to represent empty slots in the list.</P>
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">The StringTokenizer Class</FONT></H4>
<P>To break up a string of data into discrete tokens, we use a class already built into Java, called a StringTokenizer. The StringTokenizer class is used to break up a string into smaller substrings (called &#147;tokens&#148;), based on a common separator (now you see why I chose to break up our data with &#147;|&#148;). Here is a summary of the useful methods in the StringTokenizer class:
</P>
<!-- CODE //-->
<PRE>
new StringTokenizer(String str, String delim)
/* Creates a new StringTokenizer object based on the String str and the&#8656;
"delimeter" (or separater) delim*/

hasMoreTokens()
/* Returns true if the Tokenizer has more "tokens" left (each substring&#8656;
is called a "token") */

nextToken()
/* Returns the next substring token */

countTokens()
/* Returns the total number of tokens in the tokenizer */
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="282-284.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="287-290.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -