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

📄 706-710.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=706-710//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="704-706.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="710-713.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">The action() Method</FONT></H4>
<P>The action() Method is given in Listing 17-14 and diagrammed visually in Figure 17-3. I use this method to trap all of the input events that occur when the player clicks on buttons to modify the game or to start or restore from a solve. It begins by converting the <I>Object</I> parameter into a string so that its name can be compared with the names of the buttons that have been defined. This allows the method to determine which button has been pressed by using a simple comparison by way of the <I>Equals</I> method of the <I>String</I> class. If the solve button has been pressed, the thread for the solve is stopped if it is active and a new instance of the <I>solver</I> class is allocated and started. The current board is also saved in <I>save_board</I> to be restored later, and the method <I>returns</I> from there. The saved board is restored when the Restore button is called, in which case the old <I>main_board</I> is replaced by the saved version. If there has been no board saved, nothing is done, and the method ends. For the buttons pertaining to the number of colors and number of squares, the variable that stores the number of squares or colors is incremented and decremented as necessary, checked for limits, and a new board is created with the new specification.</P>
<P><B>Listing 17-14</B> The action() event handler</P>
<!-- CODE //-->
<PRE>
public boolean action (Event evt, Object arg)&#123; //process buttons
  String label = (String)arg;  //converts the button to a string
  if (label.equals("Solve")) &#123; //if request to start solving the board
    if (solve_thread != null)&#123; //potentially running solve
      solve_thread.stop();  //stop the thread
      solve_thread = null;  //allow garbage collecting
    &#125;
    save_board = new game_board(main_board);  //save the board

    solve_thread = new solver(main_board,solve_area); //create the solver
    solve_thread.setPriority(Thread.MIN_PRIORITY); //lower priority
    solve_thread.start();                  //start solving in background
    return true;
  &#125;
  if (label.equals("Restore"))&#123; //restore the board to when started solve
    if (save_board == null) //don't do anything if no board saved
      return true;

    main_board = new game_board(save_board); //otherwise create a new
                                             //board
    repaint();
    return true;    //return here so we don't randomize

  &#125;

  if (label.equals("More Colors"))&#123; //if request for more colors
    colors &#43;&#43;;
  &#125;
  else if (label.equals("Less Colors"))&#123;
    colors --;
  &#125;
  else if (label.equals("More Squares"))&#123;
    squares &#43;= 2;
  &#125;
  else if (label.equals("Less Squares"))&#123;
    squares -= 2;
  &#125;
  if (squares &gt; 35)  //max 35x35 squares
    squares = 35;
  if (squares &lt; 3)
    squares = 3;
  if (colors &gt; 5)  //maximum 5 colors
    colors = 5;
  if (colors &lt; 2)  //at least two colors
    colors = 2;

  main_board = new game_board(squares,length,colors); //create new board
  main_board.randomize();  //randomize the new game board

  repaint();  //copy new board to screen
  return true;
&#125;
</PRE>
<!-- END CODE //-->
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/17-03.jpg',397,497 )"><IMG SRC="images/17-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/17-03.jpg',397,497)"><FONT COLOR="#000077"><B>Figure 17-3</B></FONT></A>&nbsp;&nbsp;The inner workings of the action() method</P>
<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">Buttons</FONT></H4>
<P>On a press of the solve Button, any thread that is working on a solve is stopped to allow for a new thread to be started on the current board. The board that is being solved is saved in <I>save_board</I> so that it can be restored later if the user wants to try out the solve. The board can then be restored by clicking on the restore Button, which merely loads in the new board, draws this board, and returns.</P>
<P>The Buttons pertaining to the number of squares and colors on a board are straightforward in that they modify the global variables that hold the attributes for the current board, so that they can be applied at the end of the method. At the end of the method, the attribute bounds are checked and applied, and the board is randomized and redrawn.</P>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">The solver Class</FONT></H4>
<P>The <I>solver</I> class (see Listing 17-15) is the most interesting class in this otherwise elementary puzzle game. The techniques involved in solving this puzzle can be applied to almost any puzzle or board game from tic-tac-toe to chess (to a limited extent), and although inefficient, it is an important concept that should be understood. The solve algorithm is very simple to understand if you leave the implementation details aside and just look at it logically.</P>
<P><B>Listing 17-15</B> The data items defined in the solver class</P>
<!-- CODE SNIP //-->
<PRE>
class solver extends Thread&#123;  //a class to solve a board,run in background
  game_board start_board;  //the board to start from
  TextArea solve_window;  //The textarea to put the solve info into
  int max_depth = 12;  //the maximum depth to probe to
  int current_depth = 1;  //current depth in the solve
  int visited = 0;  //the number of boards that have been visited
  int solved[][] = new int[max_depth&#43;1][2]; //hold the moves
</PRE>
<!-- END CODE SNIP //-->

<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>The Basic Idea Behind the Solve Algorithm</B></FONT>
<P>To discover how to get from any board to a finished state, you simply have to try every combination of moves from the starting board until you find one that makes all of the squares the same color.
</P>
</TABLE>

<P>This technique used by the solve algorithm is known as an exhaustive search in computing science terms and is aptly named. It is as inefficient, as you might think, but it should also be clear that it would work in any case given enough time, if the board can be solved. Anyone who was very meticulous could even employ this method manually and be guaranteed to always succeed, although maybe not in the person&#146;s lifetime.
</P>
<P>Unfortunately, even with today&#146;s fast computers, this method is not really very practical. In fact, I&#146;ve never been able to solve a board with more than nine squares using the solver class, but I am currently trying to look at the problem from different angles to see if a better method exists. One nice feature of the solve algorithm is that it always finds the shortest, or one of the shortest, possible sequences of moves to solve the puzzle. This stems from the fact that the solve routine first looks for a solve of one move, then two moves, then three, and so on until one is found, or until a preset maximum depth has been hit. This will be discussed as it is seen in the run() method.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="704-706.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="710-713.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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