[Tutor] Tic-Tac-Toe

bob gailer bgailer at gmail.com
Sun Feb 20 01:35:23 CET 2011


On 2/19/2011 3:49 PM, Ben Ganzfried wrote:
> Hey,
>
> I *think* that everything is working except my function gameWon.  I 
> keep getting the following error: " line 67, in gameWon
>     if (cell % 3 == 0) and (self.__mycells[cell] ==  
> self.__mycells[cell + 1]) and (self.__mycells[cell + 1]== 
> self.__mycells[cell + 2]):
> TypeError: unsupported operand type(s) for %: 'instance' and 'int'

Learn to fish? unsupported operand type(s) for %: 'instance' and 'int'

What is that telling you? Take a moment to read each word / symbol and 
relate it to the statement.

PAUSE while you actually do that. If you figure it out hooray. The 
message is very explicit.

If you are stuck then scroll down.






















Is there a % operator in the statement? Yes.

The message tells you that its operand are of type instance and int.

instance is the left operand type, int is the right operand type.

Which of those types is not valid for %?

PAUSE while you actually do that. If you figure it out hooray. The 
message is very explicit.

If you are still stuck then scroll down.


















the expression being complained about is cell % 0

0 is an int, and that is OK for %.
cell is an instance. Do you know why? instances are not valid for %.

All of your references to cell in that statement and the ones following 
expect integers, not instances.

You want to, as you say below, examine the cell value.

> My thinking was the following: I wanted to check the verticals, 
> horizontals, and diagonals to see if they were the same.  If so, then 
> the game is won.  I'm not sure why I'm not actually comparing the 
> value inside the appropriate cells, but clearly the error means that 
> what I'm trying to do is not what is actually happening.  My full code 
> is below and I would greatly appreciate any help you can provide.
>
> Thanks,
>
> Ben
>
> _____________-
>
> #Ben Ganzfried
> #2/18/11
> #Tic-Tac-Toe
>
> class Player:
>
>
>     def __init__(self, name, type):
>         self.__name = name
>         self.__type = type
>
>     def move(self):
>         cell_index = input("%s's (%s) move: " & (self.__name, 
> self.__type))
>         cell_index = int(cell_index)
>         while cell_index > 8 and cell_index < 0:
>             print("Please retry...")
>             cell_index = input("%s's (%s) move: " & (self.__name, 
> self.__type))
>         return cell_index
>
>     def getName(self):
>         return self.__name
>
>     def getType(self):
>         return self.__type
>
> class Board:
>
>     def __init__(self):
>         self.__mycells = []
>         for i in range(0, 9):
>             current_cell = Cell()
>             self.__mycells.append(current_cell)
>
>     def print1(self):
>         counter = 0
>         for cell in self.__mycells:
>             cell.print1()
>             counter +=1
>             if counter % 3 == 0:
>                 print("\n----")
>
>     def setCellToX(self, cell_number):
>         cell = self.__mycells[cell_number]
>         if not cell.isOccupied():
>             cell.setToX()
>             return True
>         else:
>             return False
>
>     def setCelltoO(self, cell_number):
>         cell = self.__mycells[cell_number]
>         if not cell.isOccupied():
>             cell.setToO()
>             return True
>         else:
>             return False
>
>     def isTied(self):
>         for cell in self.__mycells:
>             if not cell.isOccupied():
>                 return False
>         return True
>
>     def gameWon(self):
>         for cell in self.__mycells:
>             #horizontals
>             if (cell % 3 == 0) and (self.__mycells[cell] ==  
> self.__mycells[cell + 1]) and (self.__mycells[cell + 1]== 
> self.__mycells[cell + 2]):
>                 return True
>             #verticals
>             elif (cell < 3) and (self._mycells[cell] == 
> self._mycells[cell + 3]) and (self._mycells[cell] == 
> self.__mycells[cell + 6]):
>                 return True
>             #diagonals
>             elif (cell == 0) and (self.__mycells[cell] == 
> self.__mycells[cell + 4]) and (self.__mycells[cell] == 
> self.__mycells[cell + 8]):
>                 return True
>             elif (cell == 2) and (self.__mycells[cell] == 
> self.__mycells[cell + 2]) and (self.__mycells[cell] == 
> self.mycells[cell + 4]):
>                 return True
>         return False
>
>
> class Cell:
>
>     def __init__(self):
>         self.__value = " "
>
>     def setToX(self):
>         self.__value = "X"
>
>     def setToO(self):
>         self.__value = "O"
>
>     def print1(self):
>         print(self.__value,)
>     #add a predicate (start in terms of question, not action)
>     def isOccupied(self):
>         return not self.__value == " "
>
> def main():
>     #initialize game
>         #draw board
>     #create two players
>     #enter a loop-- prompt each player for a loop...stops when a 
> player wins or a tie
>     board = Board()
>     board.print1()
>     pX = Player("Ben", "X")
>     pO = Player("Sam", "O")
>     while not board.gameWon() and not board.isTied():
>         c1 = pX.move()
>         success = board.setCellToX(c1)
>         while not success:
>             c1 = pX.move()
>             success = board.setCellToX(c1)
>         board.print1()
>         c1 = pO.move()
>         success = board.setCellToO(c1)
>         while not success:
>             c1 = pO.move()
>             success = board.setCellToO(c1)
>         board.print1()
>     print("Game Over")
>
>
>
> if __name__ == "__main__":
>     main()
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


-- 
Bob Gailer
919-636-4239
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110219/51b28061/attachment-0001.html>


More information about the Tutor mailing list