1

Hello I have been building a tic tac toe game and I am having a hard time with the winning conditions. I am storing all of the squares in play in a list of tuples, each player also has a list of tuples to store the "captured squares"

class Board():
    def __init__(self,window, tilesize ,padding ,spaces): # Self is class instance, tilesize is the size of your tiles in pixels, padding is side padding in pixels, spaces is how many rows and colums
        self.spaces   = spaces        # [(x,y)for x in range(0,spaces) for y in range(0,spaces)]
        self.padding  = padding
        self.paddingBottom = self.padding // 2 
        self.paddingTop = int(self.padding * 1.25)
        self.tilesize = tilesize
        self.tiles = [(x,y)for x in range(0,3) for y in range(0,3)]
        self.window = window

    def DrawBoard(self):
        for space in range(self.spaces + 1):
            #Up and Downs
            pygame.draw.line(self.window, (255,0,0),
             ( self.padding+(space * self.tilesize) ,self.paddingTop ), #start point
             ( self.padding + (space * self.tilesize), self.paddingTop + ((self.spaces* self.tilesize)) ),) #end point

            # Side To Sides
            pygame.draw.line(window, (255,0,0),
             ( self.padding ,self.paddingTop+(space * self.tilesize) ), #start point
             ( win_width - self.padding , self.paddingTop + (space * self.tilesize)),) #end point

    def CheckWin(self,players):
        self.player1 = players[0]
        self.player2 = players[1]
        self.inplay = self.player2.pos + self.player1.pos
        
        print(f'Player1: {self.player1.pos}     Player2: {self.player2.pos}')
        if type(self.inplay) == 'None':
            pass
        if type(self.inplay) == 'list':
            if len(self.inplay) == len(self.tiles):
                print('TIE')
            if (0,0) and (1,0) and (2,0) in self.player1.pos.sort():
                print('Player 1 wins')
            


class Player():
    def __init__(self, icon, player): #Icon = pygame image Player = string
        self.score = 0
        self.name = str(player)
        self.icon = pygame.transform.scale(icon, (iconSize,iconSize))
        self.pos = []

    def Draw(self,window,board):
        for square in self.pos:
            x = ((square[0] * 125 ) + board.padding ) + ((1/8) * tileSize)
            y = ((square[1] * 125 ) + board.paddingTop) + ((1/8) * tileSize)
            window.blit(self.icon, (x,y))
            pygame.display.update()

    def CaptureSquare(self, pos, board, enemy):
        newpos = round((pos[0] - board.padding)//125), round((pos[1] -board.paddingTop)//125)
        

        if newpos in self.pos:
            pass
        elif newpos in enemy.pos:
            pass

        elif newpos[0] >= 0 and newpos[0] < 3 and newpos[1] >= 0 and newpos[1] < 3 :
            self.pos.append(newpos)
            

how can I go through the list and compare it to some win conditions?

Wardunc1
  • 21
  • 3
  • 1
    This isn't your issue, but `if (0,0) and (1,0) and (2,0) in self.player1.pos.sort():` isn't right. The "in" only applies to `(2,0)`, so (0,0) will always be true and the `if` will be taken. – Tim Roberts Jan 21 '22 at 04:13
  • i tried doing it like this too: ' if [( x, 0 ) for x in range(0,3) ]' and ' if [(1,0)(2,0)(3,0)] in self.player1pos.sort() ' and neither option will print when those values are in the list – Wardunc1 Jan 21 '22 at 04:20
  • `if all( (i,0) in self.player1pos for i in range(3))` is the kind of thing you need. – Tim Roberts Jan 21 '22 at 04:24
  • I'm not sure that storing the list of positions is the right design. For tic-tac-toe, I think it makes more sense to store the grid as a list of 9 things, then set the elements to 'X' or 'O'. – Tim Roberts Jan 21 '22 at 05:34
  • Figured out the problem! the problem was in the if statement when i checked if my 'type(self.inplay)' was == 'list', which it is not it is == list (not as a string) – Wardunc1 Jan 21 '22 at 23:45
  • shouldn't `range(self.spaces + 1)` be `range(1,self.spaces + 1)` in order not to return 0? – The_spider Jan 22 '22 at 09:45

0 Answers0