tag:blogger.com,1999:blog-67460915284249345892026-04-04T06:04:52.159-04:00PythonByExampleAnonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-6746091528424934589.post-49355787052835111892013-01-14T12:54:00.000-05:002013-01-14T12:54:04.740-05:00Python Training "Text Movies"I have cobbled together a script that creates training "text movies" that use a instructions text file and some javascript.<br />
<br />
The javascript creates a simlated video of an interactive interpreter session along with comments and instructions:<br />
<br />
<a href="http://lightbird.net/larks/tmovies.html">http://lightbird.net/larks/tmovies.html</a>Anonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.com8tag:blogger.com,1999:blog-6746091528424934589.post-43134712713483253742013-01-11T14:19:00.002-05:002013-01-11T14:19:33.392-05:00Versi TutorialA new tutorial was added to Lark's Python Guide: <a href="http://lightbird.net/larks/versi.html">Versi Tutorial</a> -- a clone of the Reversi game.<br />
<br />
Some of the highlights:<br />
<br />
A rather neat approach is used to capture enemy pieces -- a 'ray' is cast from the starting location (ray creates the list of all pieces in line, horizontal, vertical or diagonal, to the end of board), then I use the itertools.groupby() function to group tiles into player's, enemy's and blanks; finally a custom nextgroup() function is used two times to get two next groups.<br />
<br />
The nextgroup(groupby_iterator, default=None) returns a convenient wrapper object with key and group attributes; or a default value if there is no next group.<br />
<br />
This makes it very easy to create the piece-capture logic: to be successful, first group needs to be enemy tiles and the second, player's; on success, first group returned to be captured.<br />
<br />
Turns handling is a little tricky in this game, and, I think, nicely handled here: a player may have no turns available, then the enemy takes turns until the player can make a move, the game only ends when either player can make no turn (which may happen when some blank tiles are still left on the board).<br />
<br />
The full list of tutorials is here: <a href="http://lightbird.net/larks/index.html">Lark's Guide</a>.Anonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.com1tag:blogger.com,1999:blog-6746091528424934589.post-33737689449926607552013-01-10T14:28:00.001-05:002013-01-10T14:28:20.172-05:00Betelgeuse TutorialI've just uploaded a new tutorial: this time it's a simple space conquest strategy game -- <a href="http://lightbird.net/larks/betelgeuse.html">Betelgeuse Tutorial</a>.<br />
<br />
One of the things this tutorial illustrates is the use of a base class to allow comparison between various objects that belong to players.<br />
<br />
It also provides an example of how to implement a game with each player having arbitrary number of moves per turn (sometimes you have to skip a few turns to let your space army build up, other times you may want to send multiple fleets from different stars or even from the same star system.)<br />
<br />
Stars and fleets have a neat, consistent interface which allows convenient handling of both.<br />
<br />
As with most other games in the guide, you can configure it to have ai vs ai play, ai vs player, or player vs player.<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.com1tag:blogger.com,1999:blog-6746091528424934589.post-27170529155032500652013-01-09T14:01:00.001-05:002013-01-09T14:02:04.171-05:00Mines TutorialI've just added the Mines (clone of Minesweeper game) Tutorial: <a href="http://lightbird.net/larks/mines.html">Mines</a><br />
<br />
The main url with all tutorials is here: <a href="http://lightbird.net/larks/index.html">Lark's Tongue Guide to Python</a><br />
<br />
It's a neat classic game and is quite fun to play, even though the interface is primitive.<br />
<br />
The game contains a nice example of floodfill algorithm which is used to mark empty tiles (tiles with no neighbouring mines -- with a twist: tiles that border empty tiles also have to be revealed, but <i>their</i> neighbours stay hidden.Anonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.com1tag:blogger.com,1999:blog-6746091528424934589.post-56799195143793671322013-01-08T01:12:00.001-05:002013-01-08T01:12:33.638-05:00Python Lists Part II<br />
This post continues where I left off in my Introduction to Python Lists. This time I'm going to<br />
look at some examples of use of lists in the games I've written for my series of tutorials.<br />
<br />
TicTacToe uses lists to store winning lines (which, in turn, contain lists of board locations). A<br />
list is a good type of a collection to use in this case because when I use winlines, I iterate<br />
over the entire list and check if a winline is completed by the player. That's what lists are<br />
really good for: going over each item in a sequence.<br />
<br />
<a href="http://lightbird.net/larks/tictactoe.html#tictactoe-class">TicTacToe Tutorial</a><br />
<br />
<br />
RockPaperScissors uses a list to keep track of player scores: it's a list of two integers<br />
(initially, both are 0), corresponding to players #1 and #2. That's a good example of the second<br />
thing lists are really good at -- getting and modifying items by index.<br />
<br />
<a href="http://lightbird.net/larks/rockpaper.html#code">RockPaperScissors Tutorial</a><br />
<br />
<br />
Flashcards script uses lists to store a master copy of cards and a temporary randomized copy which<br />
has the card removed from it after it's shown. This illustrates another useful property of lists:<br />
they can be randomized using the built-in random.shuffle() function (I'm using the slightly<br />
modified utils.shuffled() function in my example). Lists can also be "exhaused" using the pop()<br />
method which returns a list item and removes it from the list at the same time (last item by<br />
default, but you can also provide any other valid index as an argument).<br />
<br />
<a href="http://lightbird.net/larks/flashcards.html#id1">Flashcards Tutorial</a><br />
<br />
<br />
In the Words game, I'm using a list to store indexes of hidden letters for each word. This list is<br />
used in a few different ways:<br />
<br />
- when displaying a word, each letter's index is checked against the list using 'in' operator<br />
- when revealing a random letter, it is picked using random.choice() function<br />
- when revealing a letter, it's index is removed using remove() method<br />
- when generating the list, indexes are added using the append() method, which is probably the<br />
first list method you learn when reading about lists in a Python tutorial<br />
<br />
<a href="http://lightbird.net/larks/words.html#word">Words Tutorial</a><br />
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.com0tag:blogger.com,1999:blog-6746091528424934589.post-61528363326623605442013-01-03T00:37:00.001-05:002013-01-03T00:37:56.324-05:00I'm glad to announce the addition of a new chapter, covering the Tictactoe game, to my series of Python tutorials: <a href="http://lightbird.net/larks/tictactoe2.html">Tictactoe Tutorial</a>.<br />
<br />
The main page with a listing of all tutorials is located here: <a href="http://lightbird.net/larks/">http://lightbird.net/larks/</a>.<br />
<br />
Unlike the previous chapters, this one goes into much greater detail and (I hope) will be helpful to readers who are new to Python and Programming.<br />
<br />
I plan to make an expanded version of a few other chapters in the near future.<br />
<br />
I'm still working out the kinks out of the format of pseudo-code I use in this chapter; once I go through a few chapters I'm sure it will change, perhaps dramatically!<br />
<br />
I'll need to look at more examples of pseudo-code people use in books and guides and pick & choose the best, most expressive bits and pieces.Anonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.com0tag:blogger.com,1999:blog-6746091528424934589.post-67393709925545030522012-12-22T20:00:00.000-05:002012-12-22T20:00:16.029-05:00Python Lists part I
<p>A list is one of the most important data structures in Python. In this (and subsequent)
posts I will go over the methods, tips, and examples of using lists in your programs.</p>
<p>I will be using Python 3.3 but most of the examples should work with Python 2.7.</p>
<p>There are already some excellent tutorials that cover usage of lists:</p>
<p><a class="reference external" href="http://docs.python.org/3.3/tutorial/datastructures.html#more-on-lists">http://docs.python.org/3.3/tutorial/datastructures.html#more-on-lists</a></p>
<p>and</p>
<p><a class="reference external" href="http://effbot.org/zone/python-list.htm">http://effbot.org/zone/python-list.htm</a></p>
<p>In this series of posts my goal is to cover some helpful utility functions for list
handling, go into more detail on list sorting and give some examples of list use in my
game tutorials:</p>
<p><a class="reference external" href="http://lightbird.net/larks/">http://lightbird.net/larks/</a></p>
<div class="section" id="customizing-lists">
<h2>Customizing lists<a class="headerlink" href="#customizing-lists" title="Permalink to this headline">ΒΆ</a></h2>
<p>Lists are a kind of a default go-to container structure in Python: if you’re not sure what
to use, it’s always a safe choice to start with and to change later as needed.</p>
<p>It’s very easy to make a list-like container to add custom functionality. If you only need
to iterate over the list, you need to add the special <cite>__iter__()</cite> method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># (star, brightness)</span>
<span class="n">starlst</span> <span class="o">=</span> <span class="p">[[</span><span class="s">"Deneb"</span><span class="p">,</span> <span class="mf">1.25</span><span class="p">],</span> <span class="p">[</span><span class="s">"Aldebaran"</span><span class="p">,</span> <span class="mf">0.85</span><span class="p">],</span> <span class="p">[</span><span class="s">"Betelgeuse"</span><span class="p">,</span> <span class="mf">0.42</span><span class="p">],</span> <span class="p">[</span><span class="s">"Fomalhaut"</span><span class="p">,</span> <span class="mf">1.16</span><span class="p">]]</span>
<span class="k">class</span> <span class="nc">Stars</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stars</span><span class="p">)</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span> <span class="o">=</span> <span class="n">stars</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">)</span>
<span class="n">stars</span> <span class="o">=</span> <span class="n">Stars</span><span class="p">(</span><span class="n">starlst</span><span class="p">)</span>
<span class="k">for</span> <span class="n">star</span> <span class="ow">in</span> <span class="n">stars</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">star</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also use the ‘in’ test:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="n">starlst</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="n">stars</span><span class="p">)</span>
</pre></div>
</div>
<p>Once you have your custom container, it’s easy to add convenience methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Stars</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stars</span><span class="p">)</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span> <span class="o">=</span> <span class="n">stars</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">names</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">brightness</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">bright_stars</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span> <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o"><</span> <span class="mi">1</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="n">stars</span><span class="o">.</span><span class="n">names</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="n">stars</span><span class="o">.</span><span class="n">bright_stars</span><span class="p">())</span>
</pre></div>
</div>
<p>The way visual brightness is measured is that the lower the value.</p>
<p>Of course, you might also want to make a <cite>Star</cite> class to wrap each star with name and
brightness attributes, but this is a good example of making a wrapper around a list of
tuples which may be more than enough in many cases.</p>
<p>Now, if I need to access items by index, I’ll just need to add one more special method –
<cite>__getitem__()</cite> (naturally, if I did not need to iterate but ONLY needed access-by-index,
I could add this special method alone – my point is that it’s VERY easy to add these
special methods as you experiment with your code as you go and worry about the rest
later):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
</pre></div>
</div>
<p>... letting me do print(stars[0]):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">"Deneb"</span><span class="p">,</span> <span class="mf">1.25</span><span class="p">]</span>
</pre></div>
</div>
<p>Often, you’ll want to accompany your <cite>__getitem__</cite> with a <cite>__setitem__</cite>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
</pre></div>
</div>
<p>Now I can change the star’s brightness (ok not the real star’s but the one in my program):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">i</span><span class="p">,</span> <span class="n">brightness</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">"Enter star # and brightness: "</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
<span class="n">i</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">stars</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">brightness</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">stars</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
</pre></div>
</div>
<p>If I enter ‘1 0.9’, I get the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">'Aldebaran'</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">]</span>
</pre></div>
</div>
<p>In this manner, I can get little bits and pieces of the full list’s functionality as I
need them. If I wanted to have a full set of list’s methods, I need to inherit from
<cite>collections.MutableSequence</cite> and override the following special methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Stars</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">MutableSequence</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stars</span><span class="p">)</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span> <span class="o">=</span> <span class="n">stars</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="p">:</span> <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">insert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">stars</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
</pre></div>
</div>
<p>Now you’ll be able to use methods like <cite>append,</cite> <cite>count,</cite> <cite>extend,</cite> <cite>pop,</cite> <cite>reverse,</cite> <cite>sort,</cite> et
cetera.</p>
<p>My guide to Python has many examples of usage of lists, built-in and customized, used in
many simple games along with helpful explanations:</p>
<p><a class="reference external" href="http://lightbird.net/larks/">http://lightbird.net/larks/</a></p>
<p>In my next post I’ll go over some of the uses of lists in my guide’s games.</p>
</div>
Anonymoushttp://www.blogger.com/profile/08092600039516399728noreply@blogger.com1