[Tutor] connecting classes
Timothy Wilson
wilson@visi.com
Fri, 14 Jul 2000 17:41:43 -0500 (CDT)
Hi everyone,
My fledgling Python abilities are growning, but I've run into a puzzle as a
try to figure out OO techniques. Some may remember that I posted a while ago
about creating a simple program to play tic tac toe. I've finally gotten
back to it, 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
A lot of things work already, but I don't understand how I can get one class
to interact with another (it may be that my classes don't make any sense as
they're currently designed). Here's an example:
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
>>>
Suggestions?
I'd also love to hear any feedback about the overall design of this little
program. I still haven't settled on the best data structure for handling the
tic tac toe board. Also, I think the gameWon method is ugly.
-Tim
--
Tim Wilson | Visit Sibley online: | Check out:
Henry Sibley HS | http://www.isd197.k12.mn.us/ | http://www.zope.org/
W. St. Paul, MN | | http://slashdot.org/
wilson@visi.com | <dtml-var pithy_quote> | http://linux.com/