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

📄 623-626.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:NetOthello</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=15//-->
<!--PAGES=623-626//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="617-623.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="626-629.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Controlling Game Play</FONT></H4>
<P>Here are the tasks that must be carried out by whatever class is in charge of handling the game itself (in this case, NetOthello.class).
</P>
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">Taking Turns</FONT></H4>
<P>In this program, we need to use variables to represent black and white. This is necessary to set the state of pieces, track whose turn it is, and so on. Because there are only two possibilities, we are going to use boolean variables to represent color. WHITE is <I>true</I> and BLACK is <I>false</I>. We will use constants (final variables) to represent these values, in case we ever want to change them. (For instance, if we had a five-color game, we might use integer instead of boolean.) Boolean is nice because it allows us to use a statement like <I>color=!color;</I> to switch the <I>color</I> of a variable.</P>
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Teaching a Computer the Rules</FONT></H4>
<P>To create this applet, we are going to have to teach the computer how to play Othello. Specifically, we must teach the computer about three things: what constitutes a valid move, how to make a move, and how the game ends. Here is how the computer does each of these things:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;To determine a valid move, the computer must check in all eight directions (up, down, left, right, plus four diagonals) to see if there is a contiguous line of opposite-colored pieces followed by one similarly colored piece. If so, this is a valid move. Otherwise, it is not.
<DD><B>&#149;</B>&nbsp;&nbsp;To make a move uses an algorithm very similar to the one above. The computer checks in the eight directions and, if it finds a valid line, it must flip all the pieces in between.
<DD><B>&#149;</B>&nbsp;&nbsp;An Othello game is over if there are no more valid moves for either player. This happens when there are no spaces left on the board or if all of the remaining spaces are not valid moves. This requires that the computer check all available spaces to see if they are valid after every move. When the game is over, the player with the most pieces is the winner.
</DL>
<P>Now that we have the fundamentals of writing the Othello game code down, let&#146;s go on to discuss the networking part.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">The Server Side</FONT></H4>
<P>The server we need is going to be very similar to the ChatServer we wrote in Chapter 9, Advanced Networking and Multiplayer Gaming Concepts. However, since each game of Othello can have only two players, we are going to need to change the server design slightly. Instead of putting everyone in the same Group object, we are going to create a special kind of group designed to hold a limited number of users (in this case, two, but you may want to write three- or four-player games in the future). As users connect to the server, they are placed in a Group, if one is waiting. If no Group exists, a new Group is created, and the user must wait for another player to log on.
</P>
<P>This system is going to be completely client-driven; all the server will do is echo any messages it receives. The server&#146;s sole responsibility will be to decide who plays white and who plays black at the start of each game. Other than that, the server just sits and waits for client input. Just like before, we can use threads to control each connection and each group, as well as do garbage collection on clients that have disconnected.</P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">The Client Side</FONT></H4>
<P>The client is responsible for doing all the work. The client interacts with the user, gets his or her move, and sends it to the server. The server echoes this input back to each client, which must process the move it receives and display it. In addition, the client will allow the two players to chat with each other (accomplished using a simplified version of the chat program we wrote in Chapter 9).
</P>
<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Writing the Client Code</FONT></H3>
<P>In writing the client, we will write the code in the same general order in which we discussed it. First, we must write the game-specific code (including the game classes). Next, we will discuss and build the Othello GUI. Last, but certainly not least, we will write the networking code.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Writing the Game Code</FONT></H4>
<P>The most complicated code, which we should finish first, is the code that controls the game itself. We discussed the different goals of this code already; now we must deal with the practical issue of implementing it.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">The GamePiece Class</FONT></H4>
<P>We already spent some time discussing the game-related classes we will need. Let&#146;s go about implementing them now. The first class we discussed was the GamePiece class, so start with a file called GamePiece.java and put in the basics:
</P>
<!-- CODE //-->
<PRE>
import java.awt.*;

public class GamePiece &#123;

final boolean BLACK = false;
final boolean WHITE = true;

public boolean color;
public Rectangle rect;

GamePiece( boolean c) &#123;
     color=c;
&#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>Notice the two final variables. We will declare these in each file so that we don&#146;t have to remember which value represents white and which represents black. This may seem unnecessary when there are only two colors, but if there were 10 or 100 colors, you would not want to try to remember all of the numeric constants, so we use final variables to make life easier. All GamePieces must have a color associated with them.
</P>
<P>The first real method we need is one that flips the piece from white to black or vice-versa. Luckily, all we have to do is use the &#147;!&#148; operator in order to invert the value of color like this:</P>
<!-- CODE SNIP //-->
<PRE>
public void flip() &#123;
       color=!color;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>To complete the GamePiece class, only one thing remains&#151;drawing. The GamePiece class must be able to paint itself in a specified rectangle. It should also remember what this rectangle is, in case it ever gets asked to draw itself without a rectangle specified. We use the now familiar two-methods-with-the-same-name trick to accomplish this.
</P>
<!-- CODE //-->
<PRE>
public void paintPiece(Graphics g) &#123;
       paintPiece( g, rect );
&#125;

public void paintPiece(Graphics g, Rectangle r ) &#123;

rect = r;

if( ! r.intersects(g.getClipRect()) ) return;

if ( color == WHITE )

       g.setColor(Color.white);

else

       g.setColor(Color.black);

g.fillOval( rect.x, rect.y, rect.width, rect.height );

&#125;
</PRE>
<!-- END CODE //-->
<P>Note that we <I>never</I> do any drawing unless our current rectangle intersects with the clipping rectangle of the Graphics context. That&#146;s all the GamePiece has to do; now on to the GameBoard.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="617-623.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="626-629.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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