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

📄 ch16.htm

📁 对于程序员来说可以利用JAVA来开发网络游戏!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BR>
&nbsp;&nbsp;// Draw the pieces<BR>
&nbsp;&nbsp;int[][] board = gameEngine.getBoard();<BR>
&nbsp;&nbsp;for (int i = 0; i &lt; 7; i++)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 0; j &lt; 6; j++)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch(board[i][j]) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 0:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offGrfx.drawImage(pieceImg[0],
(i + 1) * 4 + i *<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[0].getWidth(this),
(6 - j) * 4 + (5 - j) *<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[0].getHeight(this)
+ 67, this);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 1:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offGrfx.drawImage(pieceImg[1],
(i + 1) * 4 + i *<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[1].getWidth(this),
(6 - j) * 4 + (5 - j) *<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[1].getHeight(this)
+ 67, this);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offGrfx.setColor(Color.white);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offGrfx.fillOval((i
+ 1) * 4 + i *<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[0].getWidth(this),
(6 - j) * 4 + (5 - j) *<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[0].getHeight(this)
+ 67,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[0].getWidth(this),
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[0].getHeight(this));
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
<BR>
&nbsp;&nbsp;// Draw the hand selector<BR>
&nbsp;&nbsp;if (!gameOver &amp;&amp; myMove)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;offGrfx.drawImage(handImg, (curXPos +
1) * 4 + curXPos *<BR>
&nbsp;&nbsp;&nbsp;&nbsp;pieceImg[0].getWidth(this) + (pieceImg[0].getWidth(this)
-<BR>
&nbsp;&nbsp;&nbsp;&nbsp;handImg.getWidth(this)) / 2, 63 - handImg.getHeight(this),
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;this);<BR>
<BR>
&nbsp;&nbsp;// Draw the game status<BR>
&nbsp;&nbsp;offGrfx.setColor(Color.black);<BR>
&nbsp;&nbsp;offGrfx.setFont(statusFont);<BR>
&nbsp;&nbsp;offGrfx.drawString(status, (size().width -<BR>
&nbsp;&nbsp;&nbsp;&nbsp;statusMetrics.stringWidth(status)) / 2,
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;statusMetrics.getHeight());<BR>
<BR>
&nbsp;&nbsp;// Draw the image onto the screen<BR>
&nbsp;&nbsp;g.drawImage(offImage, 0, 0, null);<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The <TT><FONT FACE="Courier">update</FONT></TT> method draws the
board, the pieces, the hand selector, and the status text. The
only tricky part of <TT><FONT FACE="Courier">update</FONT></TT>
is in drawing the pieces in the correct locations, which takes
a few calculations. Other than that, <TT><FONT FACE="Courier">update</FONT></TT>
is pretty straightforward.
<P>
The <TT><FONT FACE="Courier">mouseMove</FONT></TT> method is used
to update the hand selector via the <TT><FONT FACE="Courier">curXPos</FONT></TT>
member variable:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean mouseMove(Event evt, int
x, int y) {<BR>
&nbsp;&nbsp;// Update the current X position (for the hand selector)
<BR>
&nbsp;&nbsp;if (!gameOver &amp;&amp; myMove) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;curXPos = x / 28;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;repaint();<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
In <TT><FONT FACE="Courier">mouseMove</FONT></TT>, <TT><FONT FACE="Courier">curXPos</FONT></TT>
is set to the currently selected column (0-6), based on the position
of the mouse. The <TT><FONT FACE="Courier">mouseDown</FONT></TT>
method is a little more interesting in that it handles making
moves for the human player. Listing 16.6 contains the source code
for the <TT><FONT FACE="Courier">mouseDown</FONT></TT> method.
<HR>
<BLOCKQUOTE>
<B>Listing 16.6. The </B><TT><B><FONT FACE="Courier">Connect4</FONT></B></TT><B>
class's </B><TT><B><FONT FACE="Courier">mouseDown</FONT></B></TT><B>
method.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean mouseDown(Event evt, int
x, int y) {<BR>
&nbsp;&nbsp;if (gameOver) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Start a new game<BR>
&nbsp;&nbsp;&nbsp;&nbsp;newGame();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;return true;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;else if (myMove) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Make sure the move is valid<BR>
&nbsp;&nbsp;&nbsp;&nbsp;Point pos = gameEngine.makeMove(0, x /
28);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (pos.y &gt;= 0) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!gameEngine.isWinner(0))
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!gameEngine.isTie())
{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redSnd.play();
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status
= new String(&quot;Thinking...&quot;);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myMove
= false;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sadSnd.play();
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status
= new String(&quot;It's a tie!&quot;);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameOver
= true;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;applauseSnd.play();
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = new String(&quot;You
won!&quot;);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level++;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameOver = true;
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repaint();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;badMoveSnd.play();<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;badMoveSnd.play();<BR>
&nbsp;&nbsp;return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The <TT><FONT FACE="Courier">mouseDown</FONT></TT> method first
checks to see whether the game is over, in which case a mouse
click starts a new game. If the game is not over, <TT><FONT FACE="Courier">mouseDown</FONT></TT>
attempts to make the human player's move in the specified column.
If the move is valid, <TT><FONT FACE="Courier">mouseDown</FONT></TT>
checks for a win or tie, and then it plays the appropriate sound
and updates the status text. Notice that if the human player has
won, the level of the game is increased.
<P>
The last method in Connect4 is <TT><FONT FACE="Courier">newGame</FONT></TT>,
which (surprise!) sets up a new game:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public void newGame() {<BR>
&nbsp;&nbsp;// Setup a new game<BR>
&nbsp;&nbsp;newGameSnd.play();<BR>
&nbsp;&nbsp;gameEngine = new Connect4Engine();<BR>
&nbsp;&nbsp;gameOver = false;<BR>
&nbsp;&nbsp;myMove = true;<BR>
&nbsp;&nbsp;status = new String(&quot;Your turn.&quot;);<BR>
&nbsp;&nbsp;repaint();<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
In <TT><FONT FACE="Courier">newGame</FONT></TT>, the game engine
is re-created and all the game status member variables are reinitialized,
except for <TT><FONT FACE="Courier">level</FONT></TT>. This is
important, because it results in the level increasing after each
win by the human player. The only drawback is that the computer
player plays considerably slower (because of the increased depth
search) with each increasing level. On the other hand, the computer
player gets much smarter after each human player victory, so don't
expect to win more than a couple of games.
<P>
That concludes the dissection of the Connect4 applet. You now
have a complete Java AI strategy game to add to your growing list
of Java game accomplishments!
<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>
</H2>
<P>
In today's lesson, you learned how to apply the AI theory from
yesterday's lesson toward a real game. You began by developing
a preliminary design for a Connect4 game and then progressed into
implementing the support classes as well as the main applet class.
You learned how to use strategic AI to implement a very capable
computer player for the Connect4 game. You also used much of the
experience acquired during the past two weeks to add creative
graphics and sound to the game.
<P>
Today's lesson focused primarily on implementing a computer player
for a Connect4 game using AI techniques. But what if you want
to play against another real person rather than the computer?
Well, you could try to connect two mice to your computer and figure
out a way to convince Java to recognize the two. After that approach
failed, you would probably come to the conclusion that multiplayer
games require an entirely different approach to game design. Over
the next few days, you'll learn all about multiplayer network
game development, culminating in a network version of Connect4
that enables you to play other people over the Internet. Can you
feel the suspense building? I sure can!
<H2><A NAME="QA"><B><FONT SIZE=5 COLOR=#FF0000>Q&amp;A</FONT></B></A>
<BR>
</H2>

<TABLE>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Could a similar AI approach be used in another board game, such as Checkers?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Yes, but the technique of calculating a score for each player would differ, because winning Checkers is based on jumping pieces instead of lining them up in a row.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Is this the best AI approach to take for implementing the Connect4 computer player?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Probably not, but it depends on how you define &quot;best.&quot; In terms of simplicity, this approach might well be one of the best. I found a few other Connect4 AI strategies on the Web, but I settled on this 
one because it is relatively easy to implement and understand. No doubt smarter AI strategies exist, but they are almost certainly more difficult to implement. Keep in mind, however, that most other AI strategies in games like Connect4 still use a 
conceptually similar approach involving look-ahead depth searching. The primary difference usually lies in how the scores are calculated.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Will the Connect4 engine work with a larger board or a different number of pieces to connect in a series?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Absolutely. You just need to alter the sizes of the game state arrays as well as all the loops that work with them, along with recalculating the values of the <TT><FONT FACE="Courier">winPlaces</FONT></TT> and 
<TT><FONT FACE="Courier">maxPieces</FONT></TT> member variables in the <TT><FONT FACE="Courier">Connect4State</FONT></TT> class.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><B><FONT SIZE=5 COLOR=#FF0000>Workshop</FONT></B></A>
</H2>
<P>
The Workshop section provides questions and exercises to help
you get a better feel for the material you learned today. Try
to answer the questions and at least ponder the exercises before
moving on to tomorrow's lesson. You'll find the answers to the
questions in appendix A, &quot;Quiz Answers.&quot;
<H3><A NAME="Quiz"><B>Quiz</B></A></H3>
<OL>
<LI>What is the map used for?
<LI>Why is the game engine broken into two classes?
<LI>Why does the hand selector disappear when the computer player
is thinking?
</OL>
<H3><A NAME="Exercises"><B>Exercises</B></A></H3>
<OL>
<LI>Integrate the sprite classes to animate dropping the pieces.
<LI>Modify the game engine so that the AI algorithms are executed
in a thread.
<LI>Modify the applet so that two computer players battle it out.
<LI>In the computer-versus-computer version you just created,
try out different values for the level used by each player, and
watch the results.
</OL>
<P>
<HR WIDTH="100%"></P>

<CENTER><P><A HREF="ch15.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch17.htm"><IMG 
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>

<P>
<HR WIDTH="100%"></P>

</BODY>
</HTML>

⌨️ 快捷键说明

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