Re: [Edu-sig] more card play
by Edward Cherlin other posts by this author
Nov 2 2009 8:30PM messages near this date
[Edu-sig] more card play
|
Re: [Edu-sig] more card play
Cards: You are right on the merits for combinatory math, but you will
run into strong cultural aversions.
Reverse string in place with new iterator: There is a great deal more
of this. In the 1980s Hewlett-Packard built an APL system around such
transformations in place and lazy evaluation rules, including
matrix/table transpose, take, drop, catenate, laminate, and a number
of other transformations and combinations. I can probably dig out
Harry Saal's paper on the subject. Indeed, that and several others.
http://portal.acm.org/citation.cfm?id=801218&dl=GUIDE&coll=GUIDE&CFID=59626041&CFTOKEN=28525
913
An APL compiler for the UNIX timesharing system
http://portal.acm.org/citation.cfm?id=586034&dl=GUIDE&coll=GUIDE&CFID=59626133&CFTOKEN=77609
109
Considerations in the design of a compiler for APL
http://portal.acm.org/citation.cfm?id=804441
A software high performance APL interpreter
On Sun, Nov 1, 2009 at 19:55, kirby urner <kirby.urner@[...].com> wrote:
> I'm becoming more enamored of the idea of using playing cards as a
> standard feature in my Python pedagogy and andragogy (means teaching
> adults). Â Not only do we have the standard deck, but also Tarot which
> could get us more into text files, string substitution
> (string.Template) and so forth.
>
> Cards have all the elements
>
> Mathematically, a Deck suggests the difference between Cardinality
> (yes, a silly pun) and Ordinality. Â You might imagine a deck in which
> you can't decipher the cards, don't know their "face value", and so
> have no clear idea of their ranking (ordinality).
>
> On the other hand, you know which cards are the same and which are
> different across multiple decks (le'ts just say), which is the meaning
> of "cardinality" (difference without any implied ordering).
>
> Midhat Gazele dwells on this difference some in his book 'Number'.
> You might have a set of objects, say stones or wasp specimens, and a
> way of cataloging them that doesn't implement > or < or even ==.
> There is only the Python "is" and "is not" for determining of two
> objects have the same identity or not. Â License plates on cars, proper
> names, have this purpose of distinguishing.
>
> However, very quickly just about any set beyond a certain size needs
> an ordering, perhaps simply alphabetical, so that one might flip
> through a lookup table in a hurry and get to the desired object.
> People used to use ledgers, thick books, for such cataloging.
>
> This is the beginning of data structuring or data structures. Â The
> idea of "ordering" (ordinality) is very closely associated with that
> of cardinality.
>
> Anyway, over on Pyfora I was noticing a member suggesting reversing a
> string might be best accomplished by thestring[::-1], i.e. extended
> slicing with from the end to the beginning step with a step of -1.
> However PEP 322 suggests this will be inefficient compared a a built
> in function introduced in 2.4: Â reversed.
>
> According to the PEP, all reversed needs is for the consumed object to
> support __getitem__ and __len__. Â If those two are present, the
> function will do the rest, and return an iterator object in which the
> contents of a sequence are iterated over in reverse order.
>
> >>> a = 'the rain in spain stays mainly in the plain'
> >>> ''.join(reversed(a))
> 'nialp eht ni ylniam syats niaps ni niar eht'
>
> So for our Deck to be reversible by means of this built in function,
> the only methods we need to implement are these two, __getitem__ and
> __len__. Â I do this below, plus make shuffling the deck upon
> instantiation an option, not de rigueur (not mandatory). Â This makes
> it easier to see what reverse order is like.
>
> Note that the returned iterable is not itself a Deck, nor is it
> subscriptable, as the whole point of an iterable is it returns its
> contents "just in time" as you iterate over it. Â The cards are not
> "already present" in reversed order, are simply returned in reverse
> order.
>
> Of course you can force the iterable to dump all its contents by
> coercing it into a list. Â I do this as a part of the clone_deck
> method. Â Playing Game of War with clone decks, one the reverse of the
> other, results in a draw if always dealing from the top (now the
> default, yet still optional).
>
> The code below is just my Game of War again, with a few wrinkles.
> I've upgraded all string printing to use the
> print(thestring.format(args)) approach, versus the old percent sign
> string substitution codes. Â Other small improvements.
>
> >>> from sillygame import Deck
> >>> d = Deck(10)
> >>> newd = d.clone_deck()
> >>> newd[0] is d[0]
> True
> >>> id(newd[0])
> 22884944
> >>> id(d[0])
> 22884944
> >>> d.shuffle()
> >>> str(d)
> "['7 of Diamonds', 'Jack of Spades', 'Queen of Clubs', 'King of
> Spades', '5 of Clubs', '3 of Spades', '6 of Hearts', 'Ace of Clubs',
> '2 of Hearts', '9 of Spades']"
> >>> str(newd)
> "['2 of Hearts', 'Queen of Clubs', '6 of Hearts', '9 of Spades', '5 of
> Clubs', '7 of Diamonds', 'Ace of Clubs', '3 of Spades', 'Jack of
> Spades', 'King of Spades']"
> >>> d[0]
> Card(Diamonds, ('7', 7))
> >>> newd[5]
> Card(Diamonds, ('7', 7))
> >>> d[0] == newd[5]
> True
> >>> d[0] is newd[5]
> True
>
> Kirby
>
> For further reading:
> http://www.python.org/dev/peps/pep-0322/
>
>
> from random import shuffle, randint
>
> thesuits = ['Hearts','Diamonds','Clubs','Spades']
> theranks = ['Ace'] + [str(v) for v in range(2,11)] + ['Jack','Queen','King']
> rank_values = list(zip(theranks, range(1,14)))
>
> class Card:
>
> Â Â def __init__(self, suit, rank_value ):
> Â Â Â Â self.suit = suit
> Â Â Â Â self.rank = rank_value[0]
> Â Â Â Â self.value = rank_value[1]
>
> Â Â def __lt__(self, other):
> Â Â Â Â if self.value < other.value:
> Â Â Â Â Â Â return True
> Â Â Â Â else:
> Â Â Â Â Â Â return False
>
> Â Â def __gt__(self, other):
> Â Â Â Â if self.value > other.value:
> Â Â Â Â Â Â return True
> Â Â Â Â else:
> Â Â Â Â Â Â return False
>
> Â Â def __eq__(self, other):
> Â Â Â Â if self.value == other.value:
> Â Â Â Â Â Â return True
> Â Â Â Â else:
> Â Â Â Â Â Â return False
>
> Â Â def __repr__(self):
> Â Â Â Â return "Card({0}, {1})".format(self.suit, (self.rank, self.value))
>
> Â Â def __str__(self):
> Â Â Â Â return "{0} of {1}".format(self.rank, self.suit)
>
> class Deck:
>
> Â Â def __init__(self, numcards = 52, shuffle = True):
> Â Â Â Â # build a complete deck then slice
> Â Â Â Â try:
> Â Â Â Â Â Â assert 0 < numcards <= 52
> Â Â Â Â except AssertionError:
> Â Â Â Â Â Â print("Defaulting to 52 cards")
> Â Â Â Â Â Â numcards = 52
>
> Â Â Â Â self.numcards = numcards
> Â Â Â Â self.cards = [Card(suit, rank_value)
> Â Â Â Â Â Â Â Â Â Â Â for suit in thesuits
> Â Â Â Â Â Â Â Â Â Â Â for rank_value in rank_values
> Â Â Â Â Â Â Â Â Â Â Â ]
> Â Â Â Â if shuffle: self.shuffle()
> Â Â Â Â self.cards = self.cards[ : self.numcards]
>
> Â Â def __getitem__(self, index):
> Â Â Â Â return self.cards[index]
>
> Â Â def shuffle(self):
> Â Â Â Â shuffle(self.cards)
>
> Â Â def spit_card(self, top=True):
> Â Â Â Â try:
> Â Â Â Â Â Â assert self.numcards > 0
> Â Â Â Â except AssertionError:
> Â Â Â Â Â Â raise Exception("Out of cards!")
>
> Â Â Â Â if top:
> Â Â Â Â Â Â some_card = self.cards.pop(0)
> Â Â Â Â else:
> Â Â Â Â Â Â some_card = self.cards.pop( randint( 0, self.numcards - 1 ))
>
> Â Â Â Â self.numcards = len(self.cards)
> Â Â Â Â return some_card
>
> Â Â def clone_deck(self, reverse=False):
> Â Â Â Â newdeck = Deck(numcards=1)
> Â Â Â Â newdeck.numcards = self.numcards
> Â Â Â Â if reverse:
> Â Â Â Â Â Â newdeck.cards = list(reversed(self.cards))
> Â Â Â Â else:
> Â Â Â Â Â Â newdeck.cards = self.cards[:]
> Â Â Â Â return newdeck
>
> Â Â def __repr__(self):
> Â Â Â Â return "Deck({0})".format(self.numcards)
>
> Â Â def __str__(self):
> Â Â Â Â return str([str(card) for card in self.cards])
>
> Â Â def __len__(self):
> Â Â Â Â return len(self.cards)
>
> def test():
> Â Â thedeck = Deck()
> Â Â print (str(thedeck))
>
> def game_of_war():
> Â Â deckA = Deck(10)
> Â Â # deckB = Deck(10)
> Â Â deckB = deckA.clone_deck(reverse=True) # play a reversed clone
> Â Â PlayerA_score = 0
> Â Â PlayerB_score = 0
>
> Â Â try:
> Â Â Â Â assert deckA.numcards == deckB.numcards
> Â Â except AssertionError:
> Â Â Â Â raise Exception("Decks don't have same number of cards")
>
> Â Â for i in range(deckA.numcards):
> Â Â Â Â playerA_card = deckA.spit_card(top=False) # deal from anywhere
> Â Â Â Â playerB_card = deckB.spit_card(top=False)
>
> Â Â Â Â if playerA_card > playerB_card:
> Â Â Â Â Â Â PlayerA_score += 1
> Â Â Â Â Â Â print("A's {0} beats B's {1}".format(playerA_card, playerB_card))
> Â Â Â Â if playerA_card < playerB_card:
> Â Â Â Â Â Â PlayerB_score += 1
> Â Â Â Â Â Â print("B's {0} beats A's {1}".format(playerB_card, playerA_card))
> Â Â Â Â if playerA_card == playerB_card:
> Â Â Â Â Â Â print("B's {0} matches A's {1}".format(playerB_card, playerA_card))
>
> Â Â if PlayerA_score > PlayerB_score:
> Â Â Â Â print("Game Over: Â A wins")
> Â Â if PlayerA_score < PlayerB_score:
> Â Â Â Â print("Game Over: Â B wins")
> Â Â if PlayerA_score == PlayerB_score:
> Â Â Â Â print("Game Over: Â it's a draw!")
>
> if __name__ == '__main__':
> Â Â # test()
> Â Â game_of_war()
> _______________________________________________
> Edu-sig mailing list
> Edu-sig@[...].org
> http://mail.python.org/mailman/listinfo/edu-sig
>
--
Edward Mokurai (é»é·/धरà¥à¤®à¤®à¥à¤à¤¶à¤¬à¥à¤¦à¤à¤°à¥à¤/دھرÙ
Ù
ÛگھشبدÚ
¯Ø± ج) Cherlin
Silent Thunder is my name, and Children are my nation.
The Cosmos is my dwelling place, the Truth my destination.
http://www.earthtreasury.org/
_______________________________________________
Edu-sig mailing list
Edu-sig@[...].org
http://mail.python.org/mailman/listinfo/edu-sig
Thread:
Kirby Urner
Edward Cherlin
Laura Creighton
Edward Cherlin
DeanG
Kirby Urner
Travis Vaught
Kirby Urner
Laura Creighton
Edward Cherlin
Kirby Urner
Kirby Urner
|