[Edu-sig] introduction and more on Cards

Bruce Cohen bic@cgl.ucsf.edu
Fri, 30 Mar 2001 19:19:03 -0800


	It looks like the time has come to join this list.  The thread on
Card classes is just what I am doing right now.  I am a math teacher at
Lowell High School in San Francisco.  I have not taught programming since
1981, but I was asked to teach a C++ course this year.  The one semester
course is supposed to prepare students who want to take the 1 year AP
computer science class next year.  Despite the fact that the College Board
uses C++ as the AP vehicle (until they switch to Java in 2003?), I don't
think C++ is the place to begin.  Through my connections with the Computer
Graphics Lab at UCSF (where the new molecular modeling package is written
in Python <http://www.cgl.ucsf.edu/chimera/>), I became aware of Python.
The more I learn about Python, the more I am convinced that it is a
wonderful environment for students.

	I too recently thought about using a deck of cards to introduce
objects to my students.  In fact, I really wanted this for myself.  Having
done a good deal of C programming in the early '80s and Lisp in the late
'80s, I had a decent understanding of programming paradigms, but no
experience with OOP.  My Card, Deck, and Hand classes look similar to the
ones I have just discovered in the archives of this SIG.  My first thought
is to have students build a simulation of the game of "war" (see the
description at the end of this e-mail).  The boring nature of the game
makes it ripe for computer data collection. (See
http://home.att.net/~dambrosia/programming/games/war/ and
http://www-ugrad.cs.colorado.edu/~kelloggp/War/index.html)

     As a novice with respect to object oriented programming, I would
appreciate reactions to these two ideas:

1) using __cmp__ to overload comparisons. e.g.

class Card:
	def __init__(self, Suit="spades", Value="Ace"):
		self._suit = Suit
		self._value = Value
	def __repr__(self):
		return str(self._value)+" of "+self._suit
	def __cmp__(self,other):
		if values.index(self._value) < values.index(other._value):
			return -1
		elif values.index(self._value) > values.index(other._value):
			return 1
		else:
			return 0

2) using inheritance:
	My Card class is good for war.  Another game may require a
	different ordering (e.g. suits come into play).  A second Card
	class (say SuitCard) could be based on Card but have a different
	overloading of __cmp__


	Thanks.

-Bruce


-----------------------------------------------------------
(from http://www-ugrad.cs.colorado.edu/~kelloggp/War/index.html)
The Rules of War (from Hoyle's Book of Card Games)

Primarily a two-player game, preferably for juveniles, with a
fifty-two-card pack ranking A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2 with
suits disregarded. Each is dealt twenty-six cards face down, and each turns
his top card face up.  The higher value takes the lower and the cards are
placed face down beneath the winner's packet. This continues indefinitely,
until one player has won all the other's cards. If two cards happen to be
tied - as two tens - they are laid aside and go to the winner of the next
turnup. The player who gathers in all the cards wins the game.

As an added rule, after a tie, each player lays a card face down before
turning up another; and these cards also go to the winner of the next
turnup.