123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- -- Show the solutions to the Eight Queens problem with visualization
- love.window.setMode(800, 600)
- local size = 64
- local img = love.graphics.newImage('queen-black-64x64.png')
- local function clear(x, y)
- if (x + y) % 2 == 1 then
- love.graphics.setColor(0, 0, 0)
- else
- love.graphics.setColor(255, 255, 255)
- end
- love.graphics.rectangle("fill", x*size, y*size, size, size)
- love.graphics.setColor(255, 255, 255)
- end
- love.graphics.translate(love.graphics.getWidth()/2 - size*4,
- love.graphics.getHeight()/2 - size*4)
- love.graphics.rectangle("fill", -2, -2, size*8+4, size*8+4)
- for y = 0, 7 do
- for x = 0, 7 do
- clear(x, y)
- end
- end
- local abs = math.abs
- for q0 = 0, 7 do
- love.graphics.draw(img, q0*size, 0*size)
- -- Force display
- pause(0)
- for q1 = 0, 7 do
- if q1 ~= q0 and abs(q1 - q0) ~= 1 then
- love.graphics.draw(img, q1*size, 1*size)
- pause(0)
- for q2 = 0, 7 do
- if q2 ~= q0 and q2 ~= q1 and abs(q2 - q0) ~= 2 and abs(q2 - q1) ~= 1 then
- love.graphics.draw(img, q2*size, 2*size)
- pause(0)
- for q3 = 0, 7 do
- if q3 ~= q0 and q3 ~= q1 and q3 ~= q2
- and abs(q3 - q0) ~= 3 and abs(q3 - q1) ~= 2 and abs(q3 - q2) ~= 1
- then
- love.graphics.draw(img, q3*size, 3*size)
- pause(0)
- for q4 = 0, 7 do
- if q4 ~= q0 and q4 ~= q1 and q4 ~= q2 and q4 ~= q3
- and abs(q4 - q0) ~= 4 and abs(q4 - q1) ~= 3
- and abs(q4 - q2) ~= 2 and abs(q4 - q3) ~= 1
- then
- love.graphics.draw(img, q4*size, 4*size)
- pause(0)
- for q5 = 0, 7 do
- if q5 ~= q0 and q5 ~= q1 and q5 ~= q2 and q5 ~= q3
- and q5 ~= q4
- and abs(q5 - q0) ~= 5 and abs(q5 - q1) ~= 4
- and abs(q5 - q2) ~= 3 and abs(q5 - q3) ~= 2
- and abs(q5 - q4) ~= 1
- then
- love.graphics.draw(img, q5*size, 5*size)
- pause(0)
- for q6 = 0, 7 do
- if q6 ~= q0 and q6 ~= q1 and q6 ~= q2 and q6 ~= q3
- and q6 ~= q4 and q6 ~= q5
- and abs(q6 - q0) ~= 6 and abs(q6 - q1) ~= 5
- and abs(q6 - q2) ~= 4 and abs(q6 - q3) ~= 3
- and abs(q6 - q4) ~= 2 and abs(q6 - q5) ~= 1
- then
- love.graphics.draw(img, q6*size, 6*size)
- pause(0)
- for q7 = 0, 7 do
- if q7 ~= q0 and q7 ~= q1 and q7 ~= q2 and q7 ~= q3
- and q7 ~= q4 and q7 ~= q5 and q7 ~= q6
- and abs(q7 - q0) ~= 7 and abs(q7 - q1) ~= 6
- and abs(q7 - q2) ~= 5 and abs(q7 - q3) ~= 4
- and abs(q7 - q4) ~= 3 and abs(q7 - q5) ~= 2
- and abs(q7 - q6) ~= 1
- then
- love.graphics.draw(img, q7*size, 7*size)
- print("--------")
- print(string.rep(" ", q0) .. "Q")
- print(string.rep(" ", q1) .. "Q")
- print(string.rep(" ", q2) .. "Q")
- print(string.rep(" ", q3) .. "Q")
- print(string.rep(" ", q4) .. "Q")
- print(string.rep(" ", q5) .. "Q")
- print(string.rep(" ", q6) .. "Q")
- print(string.rep(" ", q7) .. "Q")
- pause(0.5)
- clear(q7, 7)
- end
- end
- clear(q6, 6)
- end
- end
- clear(q5, 5)
- end
- end
- clear(q4, 4)
- end
- end
- clear(q3, 3)
- end
- end
- clear(q2, 2)
- end
- end
- clear(q1, 1)
- end
- end
- clear(q0, 0)
- end
- quit()
|