Compare varible swift 4

0

I have my Grid class, where I created a multidimensional array

        placeBoatRandom(currentBoat: AircraftCarrier())
        placeBoatRandom(currentBoat: Destroyer())
        placeBoatRandom(currentBoat: SubMarine())

    func placeBoatRandom(currentBoat : Boat){
        var yRandom: Int?
        var xRandom: Int?
        var i: Int
        var isPlaced : Bool = false
        let directionRandom = getRandomValue(borneSup: 2)

        if(directionRandom == 0){
            //Case Horizontal
            while isPlaced == false {
                xRandom = getRandomValue(borneSup: self.WIDTH)
                yRandom = getRandomValue(borneSup: self.HEIGHT)
                while(xRandom! + currentBoat.healthy() > self.WIDTH){
                    // On dépasse pas
                    xRandom = getRandomValue(borneSup: self.WIDTH)
                    yRandom = getRandomValue(borneSup: self.HEIGHT)
                }
                i = xRandom!
                for i in xRandom!...(xRandom!+currentBoat.healthy()-1){
                    //Si les cases sont vides
                    if(self.grid[i][yRandom!].boat != nil){
                        isPlaced=true
                    }
                }
            }

            print("x",xRandom)
            print("y",yRandom)
            print("Horizontal")
            putBoat(boat: currentBoat, x: xRandom!, y: yRandom!, direction: .HORIZONTAL)
        }else{
            //cas VERTICAL
            while isPlaced == false {
                xRandom = getRandomValue(borneSup: self.WIDTH)
                yRandom = getRandomValue(borneSup: self.HEIGHT)
                while(yRandom! + currentBoat.healthy() > self.HEIGHT){
                    // On dépasse pas
                    xRandom = getRandomValue(borneSup: self.WIDTH)
                    yRandom = getRandomValue(borneSup: self.HEIGHT)
                }
                // On chevauche pas un autre bateau
                i = yRandom!
                for i in yRandom!...(yRandom!+currentBoat.healthy()-1){
                    //Si les cases sont vides
                    if(self.grid[xRandom!][i].boat != nil) {
                        isPlaced = true

                    }
                }
            }

            print("x",xRandom)
            print("y",yRandom)
            print("Vertical")
            putBoat(boat: currentBoat, x: xRandom!, y: yRandom!, direction: .VERTICAL)
        }
    }

The detail is that I need to compare in the placeBoatRandom function in my grid class to know if one of the boats occupies that case.

It is precisely in this part

for i in xRandom!...(xRandom!+currentBoat.healthy()-1){
                    //Si les cases sont vides
                    if(self.grid[i][yRandom!].boat != nil){
                        isPlaced=true
                    }
                }

in this way because it never leaves the loop and I can not think of how to solve

    
asked by Ernesto Emmanuel Yah Lopez 06.02.2018 в 15:14
source

0 answers