dabo Commit
Revision 7166
Date: 2012-04-30 13:28:20 -0700 (Mon, 30 Apr 2012)
Author: Ed
Trac: http://trac.dabodev.com/changeset/7166
Changed:
U trunk/demo/samples/games/Minesweeper.py
A trunk/demo/samples/minesweeper.py
Log:
Added the Minesweeper game to DaboDemo.
The code was there, but the script to load it was missing from the samples
directory.
I also added some tweaks to the game code to prevent some edge conditions.
Diff:
Modified: trunk/demo/samples/games/Minesweeper.py
===================================================================
--- trunk/demo/samples/games/Minesweeper.py 2012-04-30 20:26:29 UTC (rev
7165)
+++ trunk/demo/samples/games/Minesweeper.py 2012-04-30 20:28:20 UTC (rev
7166)
@@ -239,7 +239,7 @@
def calcFontSize(self, sz):
try:
img = self.square_0_0.image
- except:
+ except AttributeError:
# Squares haven't been created yet
return self._squareFontSize
fs = img.FontSize
@@ -503,6 +503,9 @@
try:
bs = self._boardSize
except AttributeError:
+ bs = (0, 0)
+ if 0 in bs:
+ # Can't allow zero-dimension boards.
pfm = self.Application.PreferenceManager
if pfm.preset.id:
bs = pfm.preset.width, pfm.preset.height
@@ -510,7 +513,7 @@
bs = pfm.boardwidth, pfm.boardheight
try:
bs = int(bs[0]), int(bs[1])
- except:
+ except (IndexError, ValueError):
# Could be set to None or "None" somehow
bs = _defaultBoardSize
self.BoardSize = tuple(bs)
@@ -520,6 +523,9 @@
assert type(size) in (list, tuple)
assert len(size) == 2
assert (type(size[0]), type(size[1])) == (int, int)
+ if not (size[0] and size[1]):
+ dabo.log.error(_("Cannot set dimensions to zero."))
+ size = _defaultBoardSize
self._boardSize = size
self.Form.updateGameInfo()
self.Application.PreferenceManager.boardwidth = size[0]
@@ -533,13 +539,18 @@
bc = self.Application.PreferenceManager.minecount
try:
self.MineCount = int(bc)
- except:
+ except ValueError:
# could be None or "None" somehow
self.MineCount = _defaultMineCount
return bc
def _setMineCount(self, count):
assert type(count) == int
+ # Guarantee at least one mine
+ if count < 1:
+ dabo.log.error(_("Mine count must be at least 1."))
+ count = 1
+ count = max(1, count)
self._mineCount = count
self.Form.updateGameInfo()
self.Application.PreferenceManager.minecount = count
@@ -1018,7 +1029,8 @@
for name in ("Width", "Height", "Mines"):
hs = dabo.ui.dSizer("horizontal")
l = p1.addObject(lbl, Name="lbl%s" % name,
Caption="%s:" % name)
- s = p1.addObject(dabo.ui.dSpinner, "o%s" % name,
Value=preset[name], Enabled=False)
+ s = p1.addObject(dabo.ui.dSpinner, "o%s" % name,
Value=preset[name], Enabled=False,
+ Min=4, Max=50)
hs.append(l, "fixed", alignment="right", border=b)
hs.append(s, border=b)
vs.append(hs)
@@ -1031,7 +1043,8 @@
for name in ("Width", "Height", "Mines"):
hs = dabo.ui.dSizer("horizontal")
l = p2.addObject(lbl, Name="lbl%s" % name,
Caption="%s:" % name)
- s = p2.addObject(dabo.ui.dSpinner, "o%s" % name,
Value=eval("self.board%s" % name))
+ s = p2.addObject(dabo.ui.dSpinner, "o%s" % name,
Value=eval("self.board%s" % name),
+ Min=4, Max=50)
hs.append(l, "fixed", alignment="right", border=b)
hs.append(s, border=b)
vs.append(hs)
Added: trunk/demo/samples/minesweeper.py
===================================================================
--- trunk/demo/samples/minesweeper.py (rev 0)
+++ trunk/demo/samples/minesweeper.py 2012-04-30 20:28:20 UTC (rev 7166)
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+import datetime
+import dabo
+dabo.ui.loadUI("wx")
+import dabo.dEvents as dEvents
+from dabo.dLocalize import _
+from samples.games import MinesweeperForm
+
+
+class TestPanel(dabo.ui.dPanel):
+ def afterInit(self):
+ sz = self.Sizer = dabo.ui.dSizer("v")
+ sz.appendSpacer(40)
+
+ lbl = dabo.ui.dLabel(self, Caption="The classic game of
Minesweeper, implemented in the Dabo UI.\n\nFor instructions, please see the
Overview tab.")
+ sz.append(lbl, halign="center")
+ sz.appendSpacer(30)
+ btn = dabo.ui.dButton(self, Caption="Play Minesweeper",
+ OnHit=self.runGame)
+ sz.append(btn, halign="center")
+
+
+ def runGame(self, evt):
+ frm = MinesweeperForm(self.Form, Size=(980,514), Centered=True)
+ frm.show()
+
+
+
+category = "Games.Minesweeper"
+
+overview = """
+<h3>About Minesweeper</h3>
+<p> <b>Minesweeper</b> is a classic computer game, implemented here in the
Dabo UI. </p>
+
+<h3>Object of the Game</h3>
+<p> To identify all the mines without getting yourself blown up. </p>
+
+<h3>Starting a Game</h3>
+<p> Click the 'New' button in the toolbar at the top of the form. </p>
+
+<h3>Playing the Game</h3>
+<p> Click any square. If it is a mine, you are dead, and the game is over. If
it is not a mine,
+the number of mines on the squares immediately adjacent to that square will be
displayed. Use
+those numbers to judge the likelihood that mines will be located nearby. </p>
+
+<p> Right-clicking on a square cycles it between three states: Marked (i.e.,
identified as a mine),
+Uncertain (might be a mine; be careful about clicking!), or Normal. </p>
+
+<h3>Winning the Game</h3>
+<p> When you have correctly identified all the mines without getting yourself
blown up. </p>
+"""
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message:
http://leafe.com/archives/byMID/[email protected]