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' >>> "
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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor