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 33</span>
14 <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">play</span>
15 <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>
16 <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>
17 <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>)
18 <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>
19 <span class="ruby-ivar">@game</span>.<span class="ruby-identifier">apply_moves!</span> [<span class="ruby-identifier">move_to_apply</span>]
20 <span class="ruby-identifier">puts</span> <span class="ruby-ivar">@game</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@very_verbose</span>
21 <span class="ruby-keyword kw">end</span>
22 <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>
23 [<span class="ruby-identifier">:draw</span>, <span class="ruby-ivar">@limit</span>]
24 <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>
25 <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>]
26 <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>
27 [<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>]
28 <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">InvalidCaptureError</span>, <span class="ruby-constant">InvalidMoveError</span>
29 <span class="ruby-identifier">puts</span> <span class="ruby-node">"Disqualifying player #{@game.current_player}"</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@verbose</span>
30 <span class="ruby-ivar">@named_players</span>.<span class="ruby-identifier">delete</span> <span class="ruby-ivar">@game</span>.<span class="ruby-identifier">current_player</span>
31 <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@named_players</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">></span> <span class="ruby-value">1</span>
32 <span class="ruby-keyword kw">retry</span>
33 <span class="ruby-keyword kw">else</span>
34 <span class="ruby-identifier">puts</span> <span class="ruby-node">"Game won by #{@named_players.keys[0]} by default"</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@verbose</span>
35 [<span class="ruby-ivar">@named_players</span>[<span class="ruby-ivar">@named_players</span>.<span class="ruby-identifier">keys</span>[<span class="ruby-value">0</span>]], <span class="ruby-value">0</span>]
36 <span class="ruby-keyword kw">end</span>
37 <span class="ruby-keyword kw">end</span></pre>