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

📄 699-701.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:The Magic Squares Puzzle</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=17//-->
<!--PAGES=699-701//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="693-698.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="701-704.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">The game_board Class Constructors</FONT></H4>
<P>Two constructors are implemented for the game_board class (see Listing 17-2) to act in different situations.
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The first constructor creates a board from given parameters and is used primarily when the applet is initialized, as illustrated later. This constructor allocates the memory for the board and sets the number of colors and the dimensions of the board, allowing for a board to be created to exact specifications.
<DD><B>&#149;</B>&nbsp;&nbsp;The second constructor is used, in most cases, to create a game_board exactly like an existing board. This one behaves exactly as the other constructor does; however, it gets the parameters from the existing <I>game_board</I> <I>source</I>. The colors to be stored in the <I>board</I> array are then copied from the source by the local copy_board() method.
</DL>
<P><B>Listing 17-2</B> The constructors for the game_board class</P>
<!-- CODE //-->
<PRE>
game_board(int squares,int side_pixels,int colors)&#123; //constructor
  board = new int[squares][squares]; //get the data for the board
  side_squares = squares;  //save the size of the board
  side_length = side_pixels;  //save the size in pixels of the board
  max_color = colors - 1;  //-1 because colors start at zero
&#125;

game_board(game_board source)&#123; //a copying constructor
  board = new int[source.side_squares][source.side_squares];
  side_squares = source.side_squares; //save the number of squares to side
  max_color = source.max_color;      //save the maximum color
  side_length = source.side_length; //save the length in pixels
  copy_board(source);     //copy over the board data
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">The randomize() Method</FONT></H4>
<P>The public method randomize() fulfills the role of randomizing an existing board. It simply goes through each square and uses a Random object to determine its color from zero to the maximum color allowed. The case of the color being returned negative has to be taken care of, because the Java modulo (%) operator returns a positive or negative value based on the dividend. Individuals with a mathematics background will realize that this does not make a great deal of sense, but it is that way nonetheless. The randomize() method is given in Listing 17-3.
</P>
<P><B>Listing 17-3</B> The randomize() method</P>
<!-- CODE //-->
<PRE>
public void randomize()&#123; //randomize the board
  Random num_gen = new Random(); //random number generator
  int i,j;

  for (i=0;i&lt;side_squares;i&#43;&#43;)
    for (j=0;j&lt;side_squares;j&#43;&#43;)&#123;
      board[i][j]=(num_gen.nextInt()%(max_color&#43;1));
      if (board[i][j] &lt; 0)
     board[i][j]*=-1;
    &#125;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">Three Methods Used to Apply a Move to the Board</FONT></H4>
<P>The following three methods listed and described are responsible for acting on a mouse click, and are what the calling program will use whenever it has to change the state of the board for any reason.
</P>
<P><FONT SIZE="+1"><B><I>The increment_square() Method</I></B></FONT></P>
<P>The increment_square() method (Listing 17-4) is responsible for the lowest level of board manipulation. Given an x and y coordinate of a square in the array, it increases the color value of this square by one and checks whether it is greater than the maximum color allowed, in which case the square&#146;s color is set to zero. This method does nothing if the square passed to it is out of bounds given the size of the board, and this simplifies the procedures that call this method.
</P>
<P><B>Listing 17-4</B> The increment_square() method</P>
<!-- CODE //-->
<PRE>
public  void increment_square (int x1,int y1)&#123;  //increments this square
    if (x1&lt;0 || x1 &gt; side_squares -1 || y1 &lt;0 || y1 &gt; side_squares-1)
      return;                           //return if not a valid square
    else&#123;                          //otherwise increment the square
      board[x1][y1]&#43;&#43;;
      if (board[x1][y1]&gt;max_color)
     board[x1][y1] = 0;
    &#125;
  &#125;
</PRE>
<!-- END CODE //-->
<P><FONT SIZE="+1"><B><I>The apply_move_s() and apply_move_p() Methods</I></B></FONT></P>
<P>The apply_move_s() and apply_move_p() are the brains of the board and actually apply the move. Both these methods perform the same task: to take in the coordinates of a square and apply the appropriate move to them. However, this action comes in two flavors:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The method apply_move_s() (Listing 17-5) takes in the absolute coordinates of a square in the grid. Thus, these parameters can be used to directly call increment_square() on the given squares and the four squares around it. The increment_square() method performs all out of bounds checking, so this method can blindly call increment_square() on all of the necessary squares whether they exist or not.
</DL>
<P><B>Listing 17-5</B> The apply_move_s() method</P>
<!-- CODE //-->
<PRE>
public  void apply_move_s (int x,int y)&#123;  //what to do on a click,takes
squares
  //first convert from pixels to squares

  //now increment the appropriate squares
  increment_square(x,y);     //invalid squares handled in increment_square
  increment_square(x-1,y);
  increment_square(x&#43;1,y);
  increment_square(x,y-1);
  increment_square(x,y&#43;1);
&#125;
</PRE>
<!-- END CODE //-->
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The apply_move_p() method (Listing 17-6) takes in the coordinate as pixel values relative to the top-right-hand corner of the board. These coordinates are translated into a square, and apply_move_s() is called to make the move.
</DL>
<P><B>Listing 17-6</B> The apply_move_p() method</P>
<!-- CODE //-->
<PRE>
public void apply_move_p(int xpix,int ypix)&#123; //apply click, takes pixels
  int x,y;

  if (xpix&gt;side_length || ypix &gt;side_length) //only take valid moves
    return;

   x = xpix/((int)(side_length/side_squares)); //convert pixels to location
   y = ypix/((int)(side_length/side_squares));

   apply_move_s(x,y);  //call the square version of apply_move
&#125;
</PRE>
<!-- END CODE //-->
<P>In the true spirit of object-oriented programming, the two methods apply_move_s() and apply_move_p() should have been overloaded into one method name. They have not been, because they both take the same types of parameters: two integers. For this reason, overloading cannot be used here.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="693-698.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="701-704.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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