server/game/board.py
def count_ships(self) -> set:
        '''
        Returns set of cells with errors in placing
        '''
        def check_ship_vertical(x, y):
            # returns True if ship is vertical
            if 0 <= y-1 < 10:
                if self.board[y-1][x] == Cell.SHIP: return True
            if 0 <= y+1 < 10:
                if self.board[y+1][x] == Cell.SHIP: return True
            return False

        def check_ship_end(x, y, swap):
            # checks if next cell is empty or border
            if swap == 1:
                if y == 9: return True
                if self.board[y+1][x] == Cell.EMPTY: return True
                return False
            else:
                if x == 9: return True
                if self.board[y][x+1] == Cell.EMPTY: return True
                return False

        def check_corners(x, y):
            # checks if ships are touching corners
            corners_shift = ((-1, -1),
                             (+1, -1),
                             (-1, +1),
                             (+1, +1))
            for i in range(4):
                dx = x + corners_shift[i][1]
                dy = y + corners_shift[i][0]
                if 0 <= dx < 10 and 0 <= dy < 10:
                    if self.board[dy][dx] == Cell.SHIP: return True
            return False

        self.ships = [0, 0, 0, 0]
        errors = set()  # set of cells with errors
        for swap in range(2):
            # swap 0-count horisontal ships 1-vertical ships
            for py in range(10):
                curr_ship_len = 0
                for px in range(10):
                    x, y = px, py
                    if swap == 1: y, x = px, py  # swap x and y to count vertical ships
                    if self.board[y][x] == Cell.SHIP:
                        curr_ship_len += 1
                        if check_corners(x, y):
                            errors.add((x, y))
                    if curr_ship_len > 0 and check_ship_end(x, y, swap):
                        if curr_ship_len == 1:
                            # ship is single-celled or vertical
                            if swap or check_ship_vertical(x, y):
                                '''
                                Ship is vertical or swap=1
                                Single-celled ships are counted only once, in horsontal mode(swap=0)
                                In either case - ignore ship
                                '''
                                curr_ship_len = 0
                        if 0 < curr_ship_len <= 4:
                            # count ship
                            self.ships[curr_ship_len-1] += 1
                            if self.ships[curr_ship_len-1] > 5-curr_ship_len:
                                # more ships than allowed -> errors
                                for c in range(curr_ship_len):
                                    if swap == 1:
                                        errors.add((x, y-c))
                                    else:
                                        errors.add((x-c, y))
                        elif curr_ship_len > 0:
                            # ship longer than 4 cells -> error
                            errors.add((x, y))
                        curr_ship_len = 0
        return errors

        Returns set of cells with errors in placing
        
Returns set of cells with errors in placing
Returns set of cells with errors in placing
def count_ships(self) -> set:
        def check_ship_vertical(x, y):
            if 0 <= y-1 < 10:
                if self.board[y-1][x] == Cell.SHIP: return True
            if 0 <= y+1 < 10:
                if self.board[y+1][x] == Cell.SHIP: return True
            return False
        def check_ship_end(x, y, swap):
            if swap == 1:
                if y == 9: return True
                if self.board[y+1][x] == Cell.EMPTY: return True
                return False
            else:
                if x == 9: return True
                if self.board[y][x+1] == Cell.EMPTY: return True
                return False
        def check_corners(x, y):
            corners_shift = ((-1, -1),
                             (+1, -1),
                             (-1, +1),
                             (+1, +1))
            for i in range(4):
                dx = x + corners_shift[i][1]
                dy = y + corners_shift[i][0]
                if 0 <= dx < 10 and 0 <= dy < 10:
                    if self.board[dy][dx] == Cell.SHIP: return True
            return False
        self.ships = [0, 0, 0, 0]
        errors = set()  
        for swap in range(2):
            for py in range(10):
                curr_ship_len = 0
                for px in range(10):
                    x, y = px, py
                    if swap == 1: y, x = px, py  
                    if self.board[y][x] == Cell.SHIP:
                        curr_ship_len += 1
                        if check_corners(x, y):
                            errors.add((x, y))
                    if curr_ship_len > 0 and check_ship_end(x, y, swap):
                        if curr_ship_len == 1:
                            if swap or check_ship_vertical(x, y):
                                curr_ship_len = 0
                        if 0 < curr_ship_len <= 4:
                            self.ships[curr_ship_len-1] += 1
                            if self.ships[curr_ship_len-1] > 5-curr_ship_len:
                                for c in range(curr_ship_len):
                                    if swap == 1:
                                        errors.add((x, y-c))
                                    else:
                                        errors.add((x-c, y))
                        elif curr_ship_len > 0:
                            errors.add((x, y))
                        curr_ship_len = 0
        return errors
def count_ships ( self ) -> set : def check_ship_vertical ( x , y ) : if 0 <= y - 1 < 10 : if self . board [ y - 1 ] [ x ] == Cell . SHIP : return True if 0 <= y + 1 < 10 : if self . board [ y + 1 ] [ x ] == Cell . SHIP : return True return False def check_ship_end ( x , y , swap ) : if swap == 1 : if y == 9 : return True if self . board [ y + 1 ] [ x ] == Cell . EMPTY : return True return False else : if x == 9 : return True if self . board [ y ] [ x + 1 ] == Cell . EMPTY : return True return False def check_corners ( x , y ) : corners_shift = ( ( - 1 , - 1 ) , ( + 1 , - 1 ) , ( - 1 , + 1 ) , ( + 1 , + 1 ) ) for i in range ( 4 ) : dx = x + corners_shift [ i ] [ 1 ] dy = y + corners_shift [ i ] [ 0 ] if 0 <= dx < 10 and 0 <= dy < 10 : if self . board [ dy ] [ dx ] == Cell . SHIP : return True return False self . ships = [ 0 , 0 , 0 , 0 ] errors = set ( ) for swap in range ( 2 ) : for py in range ( 10 ) : curr_ship_len = 0 for px in range ( 10 ) : x , y = px , py if swap == 1 : y , x = px , py if self . board [ y ] [ x ] == Cell . SHIP : curr_ship_len += 1 if check_corners ( x , y ) : errors . add ( ( x , y ) ) if curr_ship_len > 0 and check_ship_end ( x , y , swap ) : if curr_ship_len == 1 : if swap or check_ship_vertical ( x , y ) : '''
                                Ship is vertical or swap=1
                                Single-celled ships are counted only once, in horsontal mode(swap=0)
                                In either case - ignore ship
                                ''' curr_ship_len = 0 if 0 < curr_ship_len <= 4 : self . ships [ curr_ship_len - 1 ] += 1 if self . ships [ curr_ship_len - 1 ] > 5 - curr_ship_len : for c in range ( curr_ship_len ) : if swap == 1 : errors . add ( ( x , y - c ) ) else : errors . add ( ( x - c , y ) ) elif curr_ship_len > 0 : errors . add ( ( x , y ) ) curr_ship_len = 0 return errors
Returns set of cells with errors in placing
Returns set of cells with errors in placing
'''
        Returns set of cells with errors in placing
        ''' # returns True if ship is vertical # checks if next cell is empty or border # checks if ships are touching corners # set of cells with errors # swap 0-count horisontal ships 1-vertical ships # swap x and y to count vertical ships # ship is single-celled or vertical '''
                                Ship is vertical or swap=1
                                Single-celled ships are counted only once, in horsontal mode(swap=0)
                                In either case - ignore ship
                                ''' # count ship # more ships than allowed -> errors # ship longer than 4 cells -> error
self