1 <?xml version="1.0" encoding="iso-8859-1"?>
3 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
8 <title>play (GameHandler)</title>
9 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10 <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
12 <body class="standalone-code">
13 <pre><span class="ruby-comment cmt"># File src/play.rb, line 28</span>
14 <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">play</span>
15 <span class="ruby-keyword kw">begin</span>
16 <span class="ruby-keyword kw">while</span> <span class="ruby-ivar">@game</span>.<span class="ruby-identifier">history</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator"><</span> <span class="ruby-ivar">@limit</span>
17 <span class="ruby-identifier">roll</span> = <span class="ruby-identifier">rand</span>(<span class="ruby-value">6</span>) <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
18 <span class="ruby-identifier">move_to_apply</span> = <span class="ruby-ivar">@named_players</span>[<span class="ruby-ivar">@game</span>.<span class="ruby-identifier">current_player</span>].<span class="ruby-identifier">best_move</span>(<span class="ruby-ivar">@game</span>, <span class="ruby-identifier">roll</span>)
19 <span class="ruby-identifier">puts</span> <span class="ruby-node">"Move #{@game.history.length + 1}: Player #{@game.current_player} rolled #{roll}: making move #{move_to_apply}"</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@verbose</span>
20 <span class="ruby-ivar">@game</span>.<span class="ruby-identifier">apply_moves!</span> [<span class="ruby-identifier">move_to_apply</span>]
21 <span class="ruby-identifier">puts</span> <span class="ruby-ivar">@game</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@verbose</span>
22 <span class="ruby-keyword kw">end</span>
23 <span class="ruby-identifier">puts</span> <span class="ruby-node">"Game terminated after #{@game.history.length} moves"</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@verbose</span>
24 [<span class="ruby-identifier">:draw</span>, <span class="ruby-value">0</span>]
25 <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">GameWonNotice</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">win_notification</span>
26 <span class="ruby-identifier">winner</span> = <span class="ruby-identifier">win_notification</span>.<span class="ruby-identifier">message</span>[<span class="ruby-value">-1</span>,<span class="ruby-value">1</span>]
27 <span class="ruby-identifier">puts</span> <span class="ruby-node">"Game won by #{winner} in #{@game.history.length} moves"</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@verbose</span>
28 [<span class="ruby-ivar">@named_players</span>[<span class="ruby-identifier">winner</span>], <span class="ruby-ivar">@game</span>.<span class="ruby-identifier">history</span>.<span class="ruby-identifier">length</span>]
29 <span class="ruby-keyword kw">end</span>
30 <span class="ruby-keyword kw">end</span></pre>