[Tutor] connecting classes

Steve Spicklemire steve@spvi.com
Sat, 15 Jul 2000 06:20:30 -0500 (EST)


Hi Tim,

>>>>> "TW" == Timothy Wilson <wilson@visi.com> writes:

    TW> Hi everyone,

    TW> My fledgling Python abilities are growning, but I've run into
    TW> a puzzle as a try to figure out OO techniques. Some may
    TW> remember that I posted a while ago about creating a simple
    TW> program to play tic tac toe. I've finally gotten back to it,
    TW> and I have the following framework:

class Game:
	def __init__(self):
		self.board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
		self.winningLines = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4,
7), \
		(2, 5, 8), (3, 6, 9), (1, 5, 9), (3, 5, 7)]
	def play(self):
		self.displayOpening
		self.drawBoard
	def displayOpening(self):
		print "Let's play Tic Tac Toe!"
	def drawBoard(self):
		for i in range(0, 8, 3):
			for j in range(3):
				print self.board[(i+j)],
			print
	def updateBoard(self, position, marker):
		self.board[position - 1] = marker
	def gameWon(self, player1, player2):
		for lines in self.winningLines:
			player1Count = 0
			player2Count = 0
			for i in lines:
				if self.board[i-1] == player1.marker:
					player1Count = player1Count + 1
				elif self.board[i-1] == player2.marker:
					player2Count = player2Count + 1
			if player1Count == 3 or player2Count == 3:
				return 1
		return 0

class Player:
	def __init__(self, name, marker):
		self.name = name
		self.marker = marker
	def move(self, choice):
		pass

class HumanPlayer(Player):
	def __init__(self, name, marker):
		Player.__init__(self, name, marker)
	def chooseMove(self):
		choice = raw_input("Where would you like to put your
marker?")
		Game.updateBoard(int(choice), self.marker)

class ComputerPlayer(Player):
	def __init__(self, name, marker):
		Player.__init__(self, name, marker)
	def chooseMove(self):
		pass
		#  Computer chooses a move based on the following criteria:
        #  1. win the game if possible
        #  2. block opposing player if necessary
        #  3. take center square if available
        #  4. take corner square if available
        #  5. pick available square at random


Python 1.5.2 (#1, May  9 2000, 15:05:56)  [GCC 2.95.3 19991030 (prerelease)]
on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import ttt
>>> game = ttt.Game()
>>> player1 = ttt.HumanPlayer("Joe", "X")
>>> player2 = ttt.ComputerPlayer("HAL-9000", "O")
>>> game.drawBoard()
1 2 3
4 5 6
7 8 9
>>> player1.chooseMove()
Where would you like to put your marker?6
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "ttt.py", line 47, in chooseMove
    Game.updateBoard(int(choice), self.marker)
TypeError: unbound method must be called with class instance 1st argument
>>>

    TW> Suggestions?

Hmmm  'Game' is a class, 'game' is an instance of the class. The
human Player needs his 'game' instance, not the 'Game' class. ;-)

    TW> I'd also love to hear any feedback about the overall design of
    TW> this little program. I still haven't settled on the best data
    TW> structure for handling the tic tac toe board. Also, I think
    TW> the gameWon method is ugly.

What if you made the board a 'bitmap', rather than a list, so that
having won could be checked with a binary 'and'.. of course
this would complicate your 'drawBoard' logic.. but maybe
not too much, (e.g., you could shift, and check to print the
board....)

-steve

    TW> -Tim