How to create the AI for my Code?

All I have is this:

-- Car Race
displayMode(OVERLAY)
displayMode(FULLSCREEN)

function setup()
    c = Car()
    c.x, c.y = 150, 200
    c.move, c.color = 2, 2
    
    d = Car()
    d.x, d.y = 100, HEIGHT-200
    d.move, d.color = 2, 1
    
    streets = {Street(95, 0, 110, HEIGHT), Street(0, 345, WIDTH, 110), Street(445, 345, 110, HEIGHT-350), Street(445, 600, WIDTH-445, 110)}
    
    fields = {Field(0, 0, 95, 345), Field(205, 0, WIDTH-205, 345), Field(0, 455, 95, HEIGHT-205)}
end

function draw()
    background(0, 0, 0, 255)
    
    for a, b in ipairs(streets) do
        b:draw()
    end
    
    for a,b in ipairs(fields) do
        b:draw()
    end
    
    c:draw()
    d:draw()
    
    -- Car "c" moving
    if c.y < 347.5 then
        c:moveTo(0, 10, 2)
    else
        c:moveTo(10, 0, 1)
        if c.x > WIDTH then
            c.x = 150
            c.y = -100
        end
    end
    
    -- Car "d" moving
    if d.y > 350 and d.x < 445 then
        d:moveTo(0, -10, 2)
    else
        if d.x < 500 then
            d:moveTo(10-(0.975/3), 0, 1)
        else
            if d.y < 604 then
                d:moveTo(0, 7.5, 2)
            else
                d:moveTo(10, 0, 1)
            end
        end
    end
end

function touched(t)
    
end

Car = class()

function Car:init()
    self.x, self.y = 0, 0
    self.move = 1
    -- 1 is horizontal, 2 is vertical
    self.color = 1
    --[[
    1 - Red
    2 - Brown
    ]]
end

function Car:draw()
    -- Body Color
    if self.color == 1 then
        fill(255, 0, 0, 255)
    elseif self.color == 2 then
        fill(175, 125, 50, 255)
    end
    
    -- Body
    if self.move == 1 then
        rect(self.x, self.y, 100, 50)
    elseif self.move == 2 then
        rect(self.x, self.y, 50, 100)
    end
    
    -- Lights
    if self.color == 1 then
        fill(255, 200, 50, 255)
    elseif self.color == 2 then
        fill(0, 175, 255, 255)
    end
    
    if self.move == 1 then
        rect(self.x, self.y + 5, 10, 15)
        rect(self.x, self.y + 30, 10, 15)
        rect(self.x + 90, self.y + 5, 10, 15)
        rect(self.x + 90, self.y + 30, 10, 15)
    elseif self.move == 2 then
        rect(self.x + 5, self.y, 15, 10)
        rect(self.x + 30, self.y, 15, 10)
        rect(self.x + 5, self.y + 90, 15, 10)
        rect(self.x + 30, self.y + 90, 15, 10)
    end
    
    -- Window
    
    if self.color == 1 then
        fill(137, 196, 196, 255)
    elseif self.color == 2 then
        fill(158, 129, 204, 255)
    end
    
    if self.move == 1 then
        rect(self.x + 15, self.y + 3, 70, 44)
    elseif self.move == 2 then
        rect(self.x + 3, self.y + 15, 44, 70)
    end
end

function Car:moveTo(x, y, TYPE)
    if TYPE == nil then TYPE = self.move end
    -- Transform X and y into km per hours.
    x = x/6
    y = y/6  
    -- Get x and y
    self.x = self.x + x
    self.y = self.y + y
    self.move = TYPE
end

Street = class()

function Street:init(x, y, w, h)
    self.x, self.y, self.w, self.h = x, y, w, h
end

function Street:draw()
    fill(86, 93, 101, 73)
    stroke(255, 255, 255, 255)
    strokeWidth(3)
    rect(self.x, self.y, self.w, self.h)
    
    -- Street Markings [[
    
    -- First calculate horizontal or vertical position
    
    local pos = nil
    if self.w > self.h then
        -- Horizontal
        pos = 1
    elseif self.h > self.w then
        -- Vertical
        pos = 2
    elseif self.h == self.w then
        -- Cannot chose
        pos = 0
    end
    
    -- Now we shall divide the street to get the perfect marking
    
    if pos == 1 then
        local a = math.ceil(self.h/10) -- Horizontal
        
        for i = 1, a do
            line(self.x, self.y + self.h/2, self.x + self.w, self.y + self.h/2)
        end
        
    elseif pos == 2 then
        local a = math.ceil(self.w/15) -- Vertical
        
        for i = 1, a do
            line(self.x + self.w/2, self.y, self.x + self.w/2, self.y + self.h)
        end
    end
    
    -- Back to Normal ]]
    strokeWidth(0)
    stroke(127, 127, 127, 255)
end

Field = class()

function Field:init(x, y, w, h)
    self.x, self.y, self.w, self.h = x, y, w, h
    
    -- Tree
    self.values = {}
    for a = 2, math.random(3, 7) do
        local value_1 = math.ceil(math.random(self.x, self.x + self.w))
        local value_2 = math.ceil(math.random(self.y, self.y + self.h))
        table.insert(self.values, {a = value_1, b = value_2})
    end
    
    self.values2 = vec2(math.random(self.x + 35, self.w + self.x - 35), math.random(self.y + 15, self.h + self.y - 15))
end

function Field:draw()
    fill(42, 83, 36, 255)
    rect(self.x, self.y, self.w, self.h)
    
    -- Decorations
    for tab = 2, #self.values do
        sprite("Small World:Bush", self.values[tab].a, self.values[tab].b)
    end
    sprite("Small World:Fountain", self.values2.x, self.values2.y)
    sprite()
end

I need an AI, that the cars shall not drive in themselves or in the park. Just wondering how to do it…

You don’t need AI for this. Since the cars drive on the road, then just check if the car is on the road. As for the cars running into each other, just check the x or y distance between them depending on which way they’re going.