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

📄 719-724.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 Internet MahJong Server</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=18//-->
<!--PAGES=719-724//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch17/717-718.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="724-728.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 18<BR>The Internet MahJong Server
</FONT></H2>
<P><I>Zuwei Thomas Feng</I></P>
<P>MahJong is an ancient Chinese game of strategy and luck. It is very popular in East Asian countries, and is attracting many American players as well. It is similar to some card games, except that it is played with tiles instead of cards. In a typical game, players take turns playing and try to win by arranging their tiles into certain &#147;MahJong&#148; patterns.
</P>
<P>My Internet MahJong Server is a software package that allows people to play MahJong with each other online. The concept is not new; there are such servers for games like chess, bridge, Go, and so on, and even a text-based package for MahJong exists. What makes my program unique is that it&#146;s written entirely in Java. It comes with a graphical user interface (GUI), and is portable. The biggest advantage, as far as players are concerned, is that they can use any Java-capable Web browser and don&#146;t need to compile or install a special client program. Figure 18-1 shows a screenshot of a typical session on the server.</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/18-01.jpg',1024,767 )"><IMG SRC="images/18-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/18-01.jpg',1024,767)"><FONT COLOR="#000077"><B>Figure 18-1</B></FONT></A>&nbsp;&nbsp;A typical session on the MahJong server</P>
<P>In this chapter, I will lead you through the design and implementation of my programs step by step. There is really nothing special about MahJong here; the same principles apply to other multiplayer games as well.
</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">A Brief Introduction to MahJong</FONT></H3>
<P>MahJong has very complicated rules. What I will attempt to give here is a minimal version that suffices for playing online. For more complete rules, the reader is referred to standard textbooks on MahJong.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Game Pieces and Components</FONT></H4>
<P>The game is played with a total of 144 tiles, which can be arranged into three categories. First, there are three <I>suits</I>: the <I>characters</I>, the <I>balls</I>, and the <I>bamboos</I>, each suit containing the values 1 to 9 and four tiles for each value. Then there are seven kinds of <I>honor</I> tiles (four <I>winds</I> and three <I>dragons</I>,) again with four tiles for each kind. Finally, there are eight distinct <I>flower</I> tiles.</P>
<P>A number of tiles can form a <I>set</I>. There are three types of sets in MahJong. A <I>chow</I> (or connection, sequence) is a set of three tiles in the same suit with consecutive values. A <I>pong</I> (or triplet) is a set of three identical tiles. And finally, a <I>gong</I> (quadruplet) is a set of four identical tiles. There is also the notion of a <I>pair</I>, which obviously consists of two identical tiles. A <I>MahJongpattern</I> consists of four sets and one pair, plus any number of flower tiles. Thus, there are 14 or more tiles, depending on the number of gongs and flower tiles. See Figure 18-2 for details.</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/18-02.jpg',577,606 )"><IMG SRC="images/18-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/18-02.jpg',577,606)"><FONT COLOR="#000077"><B>Figure 18-2</B></FONT></A>&nbsp;&nbsp;MahJong tiles and patterns</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Rules of Play</FONT></H4>
<P>Typically, four players are involved in a MahJong game. All the tiles are made into a stockpile, and each player initially draws 13 tiles from it to form a concealed hand. If someone gets a flower tile, he immediately reveals it and draws another tile. The whole game evolves around drawing and discarding tiles, and constructing sets to get a MahJong pattern. One player is the dealer, and he gets a 14th tile and the <I>turn</I> to play. The player, in turn, has to eventually discard a tile. After he does so, the other three players can make <I>bids</I> for the next turn:</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The player sitting to the right of the discarding person can bid to <I>draw</I> a new tile, or to <I>grab</I> the discarded tile if it would make a set with two (or three in the case of a gong) other tiles in his concealed hand.
<DD><B>&#149;</B>&nbsp;&nbsp;The other two players can also bid to grab, but only if they can form a pong or a gong (no chows allowed for them).
<DD><B>&#149;</B>&nbsp;&nbsp;A player can also claim to win the game if by taking the discarded tile, he would have a MahJong pattern.
</DL>
<P>Among the various bids, winning has the highest precedence, followed by pong or gong, with chow or draw the lowest. The player with the highest bid gets the next turn. If his bid involves grabbing the discard, he has to reveal his new set, and draw another tile if the set is a gong. Revealed tiles are fixed for the remainder of the game. Now he has the chance to make the following plays:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;If he has four identical concealed tiles, he may form a concealed gong. These tiles are not actually revealed but have the same fixed status. He then draws another tile.
<DD><B>&#149;</B>&nbsp;&nbsp;If he has a revealed pong and the fourth tile in his concealed hand, he may form a gong with it. Doing this might let other players win by grabbing this fourth tile. If the play is safe, he draws another tile.
<DD><B>&#149;</B>&nbsp;&nbsp;If he has a MahJong pattern, he wins the game.
</DL>
<P>Eventually, if the player cannot win yet, he has to make a discard, and we are back to the bidding stage. This process is repeated until either a winner is produced or the stockpile is exhausted, in which case the game is a draw.
</P>
<P>That&#146;s it. The most complicated thing is perhaps drawing an extra tile after a gong. This is to ensure that right before a player makes a discard, the number of his concealed tiles is always two modulo three.</P>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Overall Game Design</FONT></H3>
<P>The software package is designed to allow many people to play online with each other, and to act as a referee. This design is based on a client-server architecture, where a central server manages everything from players to game play, and each player runs a client program that interacts with him through a GUI. Players would connect with the client program, and log in under a certain user name. They should be able to communicate with other players using messages, join or serve a table, and play games. Usually, in such a client-server model, it is the server&#146;s responsibility to keep track of everything. The client, despite its fancy graphics, is only a messenger between the player and the server.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Classes, Protocols, and Packets</FONT></H4>
<P>It&#146;s a good idea to decide now what sort of &#147;things&#148; we will be dealing with on the server. Under the object-oriented paradigm, we are talking about <I>classes</I>. A couple of classes should immediately come to mind: We need a Player class to encapsulate information about each player, and a Table class for a virtual MahJong table. We also need classes such as Tile and Hand in order to keep a MahJong game going.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch17/717-718.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="724-728.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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