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

📄 629-633.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=629-633//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="626-629.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="633-636.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>This will allow you to click on any square and place an alternatively black or white piece on that square. One of the really cool programming things you should notice is that this method calls repaint() for a specific section of the screen only. This allows the paint methods to be called with a very small clipping rectangle, so that only the affected square will redraw itself. This will substantially decrease the amount of time required for a repaint. In fact, this makes painting so efficient that using double-buffered graphics is unnecessary. Of course, you could still use an offscreen drawing area, but in this case it&#146;s a waste of memory, as long as you always remember to call a limited repaint() whenever you want a square redrawn.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">Creating Game-Specific Functionality</FONT></H4>
<P>Once you&#146;re done playing with this version of NetOthello, it&#146;s time to get down to some serious work. As we discussed earlier, we have some game-specific tasks to complete. First, there is the method that determines if a certain move is valid. To make this a bit easier, we are going to add two utility functions: one that converts screen coordinates to board coordinates, and another that returns the total number of valid moves for a specific player:
</P>
<!-- CODE //-->
<PRE>
/* check if the screen coordinates x,y are in a valid square */
/* validMove() does the work, all we do is convert the numbers  */
boolean validMoveXY( int x, int y, boolean color) &#123;
      return validMove( (int) (x/theBoard.pieceWidth), (int)&#8656;
(y/theBoard.pieceHeight), color );
&#125;

/* ok, check if x,y is a valid square for color */
boolean validMove(int x, int y, boolean color) &#123;

/* if there already is a piece at x,y */
if( theBoard.pieceAt(x,y) != null ) return false;

int a,b,i,j, num;
GamePiece temp=null;

/* check in all four directions */
for(i=-1;i&lt;=1;i&#43;&#43;)
for(j=-1;j&lt;=1;j&#43;&#43;)
if( !(j == 0 &#38;&#38; i == 0) ) &#123;  /* can&#146;t check in the 0 direction */
a=x;
b=y;
num=0;
          do &#123;
                        a&#43;=i;
                        b&#43;=j;
                        temp = theBoard.pieceAt(a,b);
                        if( temp != null &#38;&#38; temp.color == !color )
                           num&#43;&#43;;
                       if( temp != null &#38;&#38; temp.color == color )
                       if( num &gt; 0) return true; /* this is the only thing&#8656;
that returns true */
                       else temp = null;
             &#125; while (temp != null);
&#125;
return false;

&#125;

/* count the number of valid moves for color */
int validMoves( boolean color ) &#123;
int i,j,num=0;

for(i=0; i&lt;8; i&#43;&#43;)
       for( j=0; j&lt;8; j&#43;&#43;)
              if( validMove( i,j, color ) )
                             num&#43;&#43;;

return num;
&#125;
</PRE>
<!-- END CODE //-->
<P>Does all of that make sense? Even if you are still confused, don&#146;t worry. The goal of this chapter is not to teach you specifics of Othello programming, but the fundamentals of networked game programming. Keep this in mind as you read through the next two game-specific functions, which actually do most of the Othello work in the applet:
</P>
<!-- CODE //-->
<PRE>
/* this actually makes a move. Very similar to validMove() */
public void doMove(int x, int y, boolean color) &#123;

int a=x,b=y,i,j, num;
GamePiece temp=null;

theBoard.addPiece( x,y,color );
repaint( a * theBoard.pieceWidth, b * theBoard.pieceHeight,&#8656;
theBoard.pieceWidth, theBoard.pieceHeight);

for(i=-1;i&lt;=1;i&#43;&#43;)
for(j=-1;j&lt;=1;j&#43;&#43;)
if( !(j == 0 &#38;&#38; i == 0) ) &#123;
a=x;
b=y;
num=0;
        do &#123;
              a&#43;=i;
              b&#43;=j;
              temp = theBoard.pieceAt(a,b);
              if( temp != null) &#123;
              if( temp.color == !color )
                     num&#43;&#43;;
              else if( temp.color == color ) &#123;
                     if( num &gt; 0) &#123;
                       a=x&#43;i;
                              b=y&#43;j;
                              temp = theBoard.pieceAt(a,b);
                                     while( temp.color == !color) &#123;
                                     theBoard.pieceAt(a,b).flip();
                                          repaint( a * theBoard.pieceWidth,&#8656;
b * theBoard.pieceHeight, theBoard.pieceWidth, theBoard.pieceHeight);
                                       a&#43;=i;
                                       b&#43;=j;
                                     temp = theBoard.pieceAt(a,b);
                                     &#125;
                               &#125;
                               temp = null;
                            &#125;
                     &#125;
               &#125;
               &#125; while (temp != null);
&#125;

int bl,wh;

if( validMoves( !turn ) &gt; 0 )
/* if the other player has valid moves, switch turns */
       turn = !turn;

/* if the game is over */
       if( endGame() )
              if( (wh=theBoard.count(WHITE)) &gt; (bl=theBoard.count(BLACK)) )
                     display("White wins!");
              else if(wh&lt;bl)
                     display("Black wins!");
              else if(bl==wh)
                    display("It's a TIE!");

/* check if the game is over */
public boolean endGame() &#123;

       if( theBoard.empty == 0)
              return true;
       else
       if( theBoard.count( BLACK ) == 0 || theBoard.count(WHITE) ==0 )
              return true;
       else
       if( validMoves( BLACK ) ==0 &#38;&#38; validMoves( WHITE ) ==0)
             return true;
return false;
&#125;
</PRE>
<!-- END CODE //-->
<P>Phew! All the Othello code is now done! However, before you try to compile it, you had better provide it with a method called display(), which is referenced in doMove(). Eventually, this will display a message with the GUI, but for now we&#146;ll just send it to System.out:
</P>
<!-- CODE SNIP //-->
<PRE>
/* display a string in the TextArea (for now just System.out) */
public void display(String str) &#123;
       System.out.println(str);
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Congratulations! You are now totally capable of playing a game of Othello with yourself, if you change the mouse-handling code to this:
</P>
<!-- CODE //-->
<PRE>
public boolean mouseDown(Event evt, int x, int y) &#123;

if( validMoveXY(x,y,turn) ) &#123; /* if we got a valid move */
         int xx,yy;
                xx= (int)(x/theBoard.pieceWidth);
                yy=(int)(y/theBoard.pieceHeight);
         doMove( xx,yy, turn);
         turn=!turn;
         return true;
         &#125;
&#125;

return false;
&#125;
</PRE>
<!-- END CODE //-->
<P>Well done! Go play around a bit. You deserve it!
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="626-629.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="633-636.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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