-
Notifications
You must be signed in to change notification settings - Fork 6
/
board.html
48 lines (46 loc) · 12.5 KB
/
board.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html> <html> <head> <title>board.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="board.html"> board.js </a> <a class="source" href="game.html"> game.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> board.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">_</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'underscore'</span><span class="p">),</span>
<span class="nx">Backbone</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'backbone'</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>A Board is comprised of an n X n array of cells
that can contain a symbol</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Board</span> <span class="o">=</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Model</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Boards have a specified n X n dimension, default is 3</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">initialize</span><span class="o">:</span><span class="kd">function</span><span class="p">(</span><span class="nx">dimension</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">dimension</span> <span class="o">=</span> <span class="nx">dimension</span> <span class="o">||</span> <span class="mi">3</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">dimension</span> <span class="o">=</span> <span class="nx">dimension</span> <span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">cells</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">dimension</span><span class="o">*</span><span class="nx">dimension</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">){</span>
<span class="k">this</span><span class="p">.</span><span class="nx">cells</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>debug method</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">dumpBoard</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">row</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">row</span> <span class="o"><</span> <span class="k">this</span><span class="p">.</span><span class="nx">dimension</span><span class="p">;</span> <span class="nx">row</span><span class="o">++</span><span class="p">){</span>
<span class="kd">var</span> <span class="nx">rowString</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">col</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">col</span> <span class="o"><</span> <span class="k">this</span><span class="p">.</span><span class="nx">dimension</span><span class="p">;</span> <span class="nx">col</span><span class="o">++</span><span class="p">){</span>
<span class="kd">var</span> <span class="nx">curCell</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getCell</span><span class="p">(</span><span class="nx">row</span><span class="p">,</span><span class="nx">col</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">curCell</span><span class="p">){</span>
<span class="nx">rowString</span> <span class="o">+=</span> <span class="nx">curCell</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">rowString</span> <span class="o">+=</span> <span class="s1">'_'</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="nx">rowString</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>provide 2d indexing into collection</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">getCell</span><span class="o">:</span><span class="kd">function</span><span class="p">(</span><span class="nx">row</span><span class="p">,</span> <span class="nx">col</span><span class="p">){</span>
<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="p">(</span><span class="nx">row</span> <span class="o">*</span> <span class="k">this</span><span class="p">.</span><span class="nx">dimension</span><span class="p">)</span> <span class="o">+</span> <span class="nx">col</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">>=</span> <span class="k">this</span><span class="p">.</span><span class="nx">cells</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="s1">'index out of range getCell'</span><span class="p">);</span>
<span class="k">throw</span> <span class="s1">'indexOutOfRange'</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">cells</span><span class="p">[</span><span class="nx">index</span><span class="p">];</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>wrapper function to set cell values on Board</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">setCell</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">row</span><span class="p">,</span> <span class="nx">col</span><span class="p">,</span> <span class="nx">symbol</span><span class="p">){</span>
<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="p">(</span><span class="nx">row</span> <span class="o">*</span> <span class="k">this</span><span class="p">.</span><span class="nx">dimension</span><span class="p">)</span> <span class="o">+</span> <span class="nx">col</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">>=</span> <span class="k">this</span><span class="p">.</span><span class="nx">cells</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="s1">'index out of range getCell'</span><span class="p">);</span>
<span class="k">throw</span> <span class="s1">'indexOutOfRange'</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">this</span><span class="p">.</span><span class="nx">cells</span><span class="p">[</span><span class="nx">index</span><span class="p">]</span> <span class="o">=</span> <span class="nx">symbol</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>trigger change event</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'change'</span><span class="p">,</span> <span class="p">{</span><span class="nx">row</span><span class="o">:</span> <span class="nx">row</span><span class="p">,</span> <span class="nx">col</span><span class="o">:</span> <span class="nx">col</span><span class="p">});</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>helper to check for open spots</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isCellAvailable</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="k">this</span><span class="p">.</span><span class="nx">cells</span><span class="p">){</span>
<span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">cells</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">==</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">});</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>