8queens.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. -- Show the solutions to the Eight Queens problem with visualization
  2. love.window.setMode(800, 600)
  3. local size = 64
  4. local img = love.graphics.newImage('queen-black-64x64.png')
  5. local function clear(x, y)
  6. if (x + y) % 2 == 1 then
  7. love.graphics.setColor(0, 0, 0)
  8. else
  9. love.graphics.setColor(255, 255, 255)
  10. end
  11. love.graphics.rectangle("fill", x*size, y*size, size, size)
  12. love.graphics.setColor(255, 255, 255)
  13. end
  14. love.graphics.translate(love.graphics.getWidth()/2 - size*4,
  15. love.graphics.getHeight()/2 - size*4)
  16. love.graphics.rectangle("fill", -2, -2, size*8+4, size*8+4)
  17. for y = 0, 7 do
  18. for x = 0, 7 do
  19. clear(x, y)
  20. end
  21. end
  22. local abs = math.abs
  23. for q0 = 0, 7 do
  24. love.graphics.draw(img, q0*size, 0*size)
  25. -- Force display
  26. pause(0)
  27. for q1 = 0, 7 do
  28. if q1 ~= q0 and abs(q1 - q0) ~= 1 then
  29. love.graphics.draw(img, q1*size, 1*size)
  30. pause(0)
  31. for q2 = 0, 7 do
  32. if q2 ~= q0 and q2 ~= q1 and abs(q2 - q0) ~= 2 and abs(q2 - q1) ~= 1 then
  33. love.graphics.draw(img, q2*size, 2*size)
  34. pause(0)
  35. for q3 = 0, 7 do
  36. if q3 ~= q0 and q3 ~= q1 and q3 ~= q2
  37. and abs(q3 - q0) ~= 3 and abs(q3 - q1) ~= 2 and abs(q3 - q2) ~= 1
  38. then
  39. love.graphics.draw(img, q3*size, 3*size)
  40. pause(0)
  41. for q4 = 0, 7 do
  42. if q4 ~= q0 and q4 ~= q1 and q4 ~= q2 and q4 ~= q3
  43. and abs(q4 - q0) ~= 4 and abs(q4 - q1) ~= 3
  44. and abs(q4 - q2) ~= 2 and abs(q4 - q3) ~= 1
  45. then
  46. love.graphics.draw(img, q4*size, 4*size)
  47. pause(0)
  48. for q5 = 0, 7 do
  49. if q5 ~= q0 and q5 ~= q1 and q5 ~= q2 and q5 ~= q3
  50. and q5 ~= q4
  51. and abs(q5 - q0) ~= 5 and abs(q5 - q1) ~= 4
  52. and abs(q5 - q2) ~= 3 and abs(q5 - q3) ~= 2
  53. and abs(q5 - q4) ~= 1
  54. then
  55. love.graphics.draw(img, q5*size, 5*size)
  56. pause(0)
  57. for q6 = 0, 7 do
  58. if q6 ~= q0 and q6 ~= q1 and q6 ~= q2 and q6 ~= q3
  59. and q6 ~= q4 and q6 ~= q5
  60. and abs(q6 - q0) ~= 6 and abs(q6 - q1) ~= 5
  61. and abs(q6 - q2) ~= 4 and abs(q6 - q3) ~= 3
  62. and abs(q6 - q4) ~= 2 and abs(q6 - q5) ~= 1
  63. then
  64. love.graphics.draw(img, q6*size, 6*size)
  65. pause(0)
  66. for q7 = 0, 7 do
  67. if q7 ~= q0 and q7 ~= q1 and q7 ~= q2 and q7 ~= q3
  68. and q7 ~= q4 and q7 ~= q5 and q7 ~= q6
  69. and abs(q7 - q0) ~= 7 and abs(q7 - q1) ~= 6
  70. and abs(q7 - q2) ~= 5 and abs(q7 - q3) ~= 4
  71. and abs(q7 - q4) ~= 3 and abs(q7 - q5) ~= 2
  72. and abs(q7 - q6) ~= 1
  73. then
  74. love.graphics.draw(img, q7*size, 7*size)
  75. print("--------")
  76. print(string.rep(" ", q0) .. "Q")
  77. print(string.rep(" ", q1) .. "Q")
  78. print(string.rep(" ", q2) .. "Q")
  79. print(string.rep(" ", q3) .. "Q")
  80. print(string.rep(" ", q4) .. "Q")
  81. print(string.rep(" ", q5) .. "Q")
  82. print(string.rep(" ", q6) .. "Q")
  83. print(string.rep(" ", q7) .. "Q")
  84. pause(0.5)
  85. clear(q7, 7)
  86. end
  87. end
  88. clear(q6, 6)
  89. end
  90. end
  91. clear(q5, 5)
  92. end
  93. end
  94. clear(q4, 4)
  95. end
  96. end
  97. clear(q3, 3)
  98. end
  99. end
  100. clear(q2, 2)
  101. end
  102. end
  103. clear(q1, 1)
  104. end
  105. end
  106. clear(q0, 0)
  107. end
  108. quit()