Monday, July 28, 2008

Tic Tac Toe

This project is a rewrite of one I posted here. I don't want to show all the code, but I put the zipped files for the project (including the nib file) on the .mac server. The buttons have an image assigned depending on which player chose that square. This is done with bindings, but the buttons are bound individually to variables in the DisplayController, so there is a variable for each button. I've been trying to figure out a way to do this more elegantly, but no luck yet. I used one-based indexing for the board, which accounts for stuff like L = range(1,10). Here is the ugly code for that class:

class DisplayController(NSObject):
myBoard = objc.IBOutlet()

sq1 = objc.ivar('sq1')
sq2 = objc.ivar('sq2')
sq3 = objc.ivar('sq3')
sq4 = objc.ivar('sq4')
sq5 = objc.ivar('sq5')
sq6 = objc.ivar('sq6')
sq7 = objc.ivar('sq7')
sq8 = objc.ivar('sq8')
sq9 = objc.ivar('sq9')

def setsq1_(self,value): self.sq1 = value
def setsq2_(self,value): self.sq2 = value
def setsq3_(self,value): self.sq3 = value
def setsq4_(self,value): self.sq4 = value
def setsq5_(self,value): self.sq5 = value
def setsq6_(self,value): self.sq6 = value
def setsq7_(self,value): self.sq7 = value
def setsq8_(self,value): self.sq8 = value
def setsq9_(self,value): self.sq9 = value

def init(self):
self.bush = NSImage.imageNamed_('bush')
self.chimp = NSImage.imageNamed_('chimp')
self.blank = NSImage.imageNamed_('blank')
return self

def update_(self,sender,which):
fL = [None,

if which is not 'all': L = [which]
else: L = range(1,10)

for i in L:
marker = self.myBoard.b[i]
if not marker: fL[i](self.blank)
elif b marker == 'P': fL[i](self.chimp)
else: fL[i](self.bush)