PDA

View Full Version : نمونه کد حل هشت وزیر بدون استفاده از الگوریتم تکاملی



علیرضا.ا
دوشنبه 12 مرداد 1394, 18:17 عصر
سلام
داشتم توی سیستم میگشتم یه کدی پیدا کردم خیلی وقت پیش نوشتم - یادش بخیر واسه پروژه مبانی کامپیوترمون بود :)) -
گفتم بزارم شاید کسی لازم داشت

این کد هشت وزیر رو بدون الگوریتم تکاملی و با سعی و خطا حل میکنه
از ویژگی هاش هم میشه گفت اینه که
- هر بار که اجرا میکنی یه جواب جدید بهت میده - از 92 جواب ممکن هر بار اجرا یکیش رو میده-
- واسه حل توی سیستم من حداکثر زمان لازم یک هزارم ثانیه بود - گاهی هم صفر ثانیه!

این کد ساده و اصلی برنامه:



#Code By Alireza AfzalAghaei
#alirezaafzalaghaei@gmail.com


from random import shuffle
from time import time
def under_attack(list, x, y):
for item in list:
if item[0] == x or item[1] == y or abs(item[1]-y) == abs(item[0]-x):
return True
solution = []
while len(solution) != 8:
x = [i for i in range(1, 9)]
solution = []
shuffle(x)
for y in range(1, 9):
for item in x:
if not under_attack(solution, item, y):
solution.append((item, y))
x.remove(item)
break
print(solution)







اینم سورس برنامه برای پیدا کردن تمام 92 راه حل مختلف و رسم یکی از راه حل ها توی صفحه شطرنج:






# Code By Alireza AfzalAghaei
# alirezaafzalaghaei@gmail.com


from random import shuffle
from time import time




def under_attack(list, x, y):
for item in list:
if item[0] == x or item[1] == y or abs(item[1]-y) == abs(item[0]-x):
return True
solution = []
while len(solution) != 8:
x = [i for i in range(1, 9)]
solution = []
shuffle(x)
for y in range(1, 9):
for item in x:
if not under_attack(solution, item, y):
solution.append((item, y))
x.remove(item)
break
print(solution)


from random import shuffle
from time import time
from turtle import st, pu, goto, pd, speed, setup, title, begin_fill, fd, rt, end_fill, ht, dot, done




#_________________ draw a chess board _________________
def chess_board(li):
st()
pu()
goto(-200, 200)
pd()
speed(0)
setup(width=420, height=420)
title("8 Queen")


def square(coloring):
if coloring:
begin_fill()
for h in range(4):
fd(50)
rt(90)
end_fill()
else:
for k in range(4):
fd(50)
rt(90)
for i in range(1, 9):
for j in range(1, 9):
if ((i+j) % 2 == 0) or (i % 2 != 0 and j % 2 != 0):
square(True)
fd(50)
else:
square(False)
fd(50)
pu()
goto(-200, -50*i+200)
pd()
pu()
ht()
for item in li:
if item[0] <= 4 and item[1] <= 4:
goto(-50 * (5-item[0]) + 25, 50*(5-item[1])-25)
dot(25, 'red')
if item[1] <= 4 < item[0]:
goto(50 * (item[0]-4) - 25, 50*(5-item[1])-25)
dot(25, 'red')
if item[0] <= 4 < item[1]:
goto(-50 * (5-item[0]) + 25, -50*(item[1]-4)+25)
dot(25, 'red')
if item[0] > 4 and item[1] > 4:
goto(50 * (item[0]-4)-25, -50*(item[1]-4)+25)
dot(25, 'red')
done()




#_________________ eight queen puzzle _________________




def under_attack(li, x, y):
for item in li:
if item[0] == x or item[1] == y or abs(item[1]-y) == abs(item[0]-x):
return True




def solve():
solutions = []
# 92 :: all possible solutions for 8QUEEN
while len(solutions) != 92:
solution = []
while len(solution) != 8:
x = [x for x in range(1, 9)]
solution = []
shuffle(x)
for y in range(1, 9):
for item in x:
if not under_attack(solution, item, y):
solution.append((item, y))
x.remove(item)
break
if solution not in solutions:
solutions.append(solution) # append new solution to solutions list
return solutions # return all possible solutions


a = time() # get time


answer = solve() # solve 8 queen


print(answer) # print all solutions as list


print(time() - a) # print duration of calculation :)


chess_board(answer[0]) # draw first possible solution