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

📄 701-704.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=701-704//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="699-701.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="704-706.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Now that moves can be made, all that is really essential is to be able to draw the board onto the screen, as shown in Listing 17-7.
</P>
<P><B>Listing 17-7</B> The game_board methods to draw the board onto the screen</P>
<!-- CODE //-->
<PRE>
public void draw_board(int x,int y,Graphics g)&#123; //draws the board
  int i,j;

  //now fill the squares
  for (i=0;i&lt;side_squares;i&#43;&#43;)
    for (j=0;j&lt;side_squares;j&#43;&#43;)
      fill_square(x&#43;i,y&#43;j,g);   //fill in all the appropriate squares
&#125;

public void fill_square(int x1,int y1,Graphics g)&#123;//fills the square based
  int x,y,side;  //the x and coordinates, and the length of the square

  //set appropriate colors

  if (board[x1][y1]==0)
    g.setColor(Color.lightGray);
  else if (board[x1][y1]==1)
    g.setColor(Color.red);
  else if (board[x1][y1]==2)
    g.setColor(Color.blue);
  else if (board[x1][y1]==3)
    g.setColor(Color.green);
  else if (board[x1][y1]==4)
    g.setColor(Color.pink);
  else if (board[x1][y1]==5)
    g.setColor(Color.yellow);

  x = x1 * (int)(side_length/side_squares)&#43;1;  //calculate the x and y
  y = y1 * (int)(side_length/side_squares)&#43;1;

  side = (int)(side_length/side_squares)-1;  //offset 1, leave outline
  g.fillRect(x,y,side,side); //fill the rectangle
&#125;
</PRE>
<!-- END CODE //-->
<P>These two methods, draw_board() and fill_square(), are all that is necessary to draw the board onto the screen. The basic method is simply to loop through all the squares and draw them to the screen in their appropriate location.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">The draw_board() Method</FONT></H4>
<P>The first method, draw_board(), is all that the outside world really needs to see to draw the whole board. One call to this high-level method will cause one call to fill_square() for each square on the board. The draw_board() method is passed an (x,y) coordinate pair though two integers and a Graphics on which to draw the square. The x and y coordinates specify where the upper-right-hand corner of the board should be placed on the Graphics, and the Graphics is passed so that the routine can place the board on any surface necessary. draw_board() then calls fill_square() for each square and passes the indices of the square, plus the coordinates of the board. In this way, the coordinates of the board act as an offset for each square&#146;s position, which places each square in its appropriate position on the Graphics.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">The fill_square() Method</FONT></H4>
<P>The fill_square() method performs the translation of a given square to its actual location on the given Graphics, and draws it accordingly. The color value is retrieved by comparing the square&#146;s color number to six possible colors, and the corresponding one is chosen. The Graphics position of the upper-right-hand corner is then determined by using the length of a side and the number of squares to a side, and the square is drawn. Since the square&#146;s size and location on the Graphics is calculated each time instead of being hardcoded, the size and number of squares in the board can be any number, and the draw_square() and draw_board() routines will work just as well.
</P>
<P>Only two more methods are needed to make the functional board class. It becomes necessary in the game to be able to copy boards simply, and the solve requires a method for determining whether a board is in a solved state or not.</P>
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">The copy_board() Method</FONT></H4>
<P>The copy_board() method (shown in Listing 17-8) simply copies the value of each square over to the destination board.
</P>
<P><B>Listing 17-8</B> The copy_board() method</P>
<!-- CODE //-->
<PRE>
public boolean copy_board(game_board source)&#123; //copy in a board
  if (source.side_squares != side_squares)  //if boards are not the same
                                            //size
    return false;           //return that couldn't complete

  int i,j;

  for (i=0;i&lt;side_squares;i&#43;&#43;)
     for (j=0;j&lt;side_squares;j&#43;&#43;)
       board[i][j] = source.board[i][j];  //copy over every square

  return true;   //everything succeeded

&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">The is_completed() Method</FONT></H4>
<P>This final board method, is_completed(), is a necessary method in order for the solve to operate. As might be expected, it returns a boolean value, indicating whether the board is completed or not. Since a completed board occurs when all of the squares are the same color, such a board can be tested for easily. The method merely gets the color in location (0,0) and compares this with all of the other squares. As soon as a square with a different color is found, the method knows that all the squares cannot be the same color, and the method returns <I>false</I>. If no such square is found, the routine returns <I>true</I> by default, and the calling method can be sure that the board is all one color. This method is shown in Listing 17-9.</P>
<P><B>Listing 17-9</B> The is_completed() method</P>
<!-- CODE //-->
<PRE>
public boolean is_completed()&#123; //return if this is a solved board
  int color;
  int i,j;

  color = board[0][0];  //compare this color with all others

  for (i=0;i&lt;side_squares;i&#43;&#43;) //go through, checking for if color not
same
    for(j=0;j&lt;side_squares;j&#43;&#43;)
      if (board[i][j] != color)
     return false;

  return true;
&#125;

&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="699-701.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="704-706.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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