[Edu-sig] More Pythonic Soccer Ball Geometry (was HexaPent)

kirby urner kirby.urner at gmail.com
Sun Jul 23 01:45:08 CEST 2006


So now I've written a second module which imports from the first.  So
gm.globaldata is a given.

The goal here is to simulate "walking a hexapent" (think of an ant
walking around on a soccer ball).  At each tile or cell, we have 5 or
6 neighbors.  Pick from the list of valid choices, and go there.  And
so on.  Pretty primitive, but suggestive nevertheless.

Here the code gets a little harder, even throwing in a property for
good measure.

I'll start with what the session looks like, followed by source

(note: on my IDLE screen, these are pretty good looking polygons, and
I'm just cutting and pasting into gmail -- let's see what happens...)

>>> reload(hpwalker)

		     ____
                    /    \
                   /      \
                   \      /
                    \____/

                 Cell: FHK

Neigboring Pentagons: F, H, K
Neigboring Hexagons : BFK, HJK, EFH


Which neighbor? (x=quit): H

		     ____
                    /    \
                    \    /
                     \  /
                      \/

                 Cell: H

Neigboring Hexagons : GHJ, FHK, HJK, EFH, EGH


Which neighbor? (x=quit): FHK

		     ____
                    /    \
                   /      \
                   \      /
                    \____/

                 Cell: FHK

Neigboring Pentagons: F, H, K
Neigboring Hexagons : BFK, HJK, EFH


Which neighbor? (x=quit): EFH

		     ____
                    /    \
                   /      \
                   \      /
                    \____/

                 Cell: EFH

Neigboring Pentagons: E, F, H
Neigboring Hexagons : FHK, EFL, EGH


Which neighbor? (x=quit): EGH

		     ____
                    /    \
                   /      \
                   \      /
                    \____/

                 Cell: EGH

Neigboring Pentagons: E, G, H
Neigboring Hexagons : EGI, GHJ, EFH


Which neighbor? (x=quit): GHJ

		     ____
                    /    \
                   /      \
                   \      /
                    \____/

                 Cell: GHJ

Neigboring Pentagons: G, H, J
Neigboring Hexagons : CGJ, HJK, EGH


Which neighbor? (x=quit): J

		     ____
                    /    \
                    \    /
                     \  /
                      \/

                 Cell: J

Neigboring Hexagons : CDJ, GHJ, CGJ, DJK, HJK


Which neighbor? (x=quit): x
<module 'hpwalker' from 'D:\Python25\lib\site-packages\hpwalker.py'>

Source code:

from gm import globaldata

class Cell (object):

    def __init__(self, address):
	self.address = address
	self.neighbors = {}
	h = p = 0
	for f in globaldata[address]:
	    if len(f)==3:
	        self.neighbors['h'+str(h)] = f
	        h += 1
	    else:
		self.neighbors['p'+str(p)] = f
		p += 1	
	if len(self.neighbors)==6:
  	    self.type = 'hexagon'
	else:
	    self.type = 'pentagon'
	
    def _templ(self):
	if self.type == 'hexagon':
	    return self._hex_templ()
	else:
	    return self._pent_templ()

    ascii = property(_templ)

    def __repr__(self):
	return "Cell %s (%s)" % (self.address, self.type)

    def _hex_templ(self):
        panel = """
		     ____
                    /    \\
                   /      \\
                   \\      /
                    \\____/

                 Cell: %s
""" % self.address
        legend = """
Neigboring Pentagons: %(p0)s, %(p1)s, %(p2)s
Neigboring Hexagons : %(h0)s, %(h1)s, %(h2)s

""" % self.neighbors
        return panel + legend

    def _pent_templ(self):
        panel = """
		     ____
                    /    \\
                    \\    /
                     \\  /
                      \\/

                 Cell: %s
""" % self.address
        legend = """
Neigboring Hexagons : %(h0)s, %(h1)s, %(h2)s, %(h3)s, %(h4)s

""" % self.neighbors
	return panel + legend

whichcell = globaldata.keys()[0]
looping = True

while looping:
    thecell = Cell(whichcell)
    print thecell.ascii
    whichcell = raw_input("Which neighbor? (x=quit): ")
    if whichcell in 'qxQX':
        looping = False


More information about the Edu-sig mailing list