Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.14 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jul 21 16:16:24 2017
  4.  
  5. @author: lollilol
  6. """
  7. import time
  8. import random
  9.  
  10. """
  11. _  is an empty place
  12. a  is a wpion
  13. b  is a wtower
  14. c  is a whorse
  15. d  is a wcrazy
  16. e  is a wking
  17. f  is a wqueen
  18. g  is a bpion
  19. h  is a btower
  20. i  is a bhorse
  21. j is a bcrazy
  22. k is a bking
  23. l is a bqueen
  24. """
  25.  
  26. start_time = time.time()
  27.  
  28. memory = open('C:\\Users\\lolli\\Desktop\\Mehdi\\Travail\\Test Code\\ChessIA')
  29.  
  30. chess = [['h','i','j','k','l','j','i','h'],
  31.          ['g','g','g','g','g','g','g','g'],
  32.          ['_','_','_','_','_','_','_','_'],
  33.          ['_','_','_','_','_','_','_','_'],
  34.          ['_','_','_','_','_','_','_','_'],
  35.          ['_','_','_','_','_','_','_','_'],
  36.          ['a','a','a','a','a','a','a','a'],
  37.          ['b','c','d','e','f','d','c','b']]
  38.  
  39. def iswhite(x,y,grid):
  40.     return grid[x][y] == 'a' or grid[x][y] == 'b' or grid[x][y] == 'c' or grid[x][y] == 'd' or grid[x][y] == 'e' or grid[x][y] == 'f'
  41.  
  42. def isblack(x,y,grid):
  43.     return grid[x][y] == 'g' or grid[x][y] == 'h' or grid[x][y] == 'i' or grid[x][y] == 'j' or grid[x][y] == 'k' or grid[x][y] == 'l'
  44.  
  45. def listwhitepos(grid):
  46.     res = []
  47.     for i in range(8):
  48.         for j in range(8):
  49.             if iswhite(i,j,grid):
  50.                 res.append((i,j))
  51.     return res
  52.  
  53. def listblackpos(grid):
  54.     res = []
  55.     for i in range(8):
  56.         for j in range(8):
  57.             if isblack(i,j,grid):
  58.                 res.append((i,j))
  59.     return res
  60.  
  61. def listmouvpiondisp(x,y,grid):
  62.     res = []
  63.     if iswhite(x,y,grid) and y-1<0:
  64.         if x>0 and not iswhite(x-1,y-1,grid):
  65.             res.append((x-1,y-1))
  66.         if isblack(x,y-1):
  67.             res.append((x,y-1))
  68.         if x<8 and not iswhite(x+1,y-1,grid):
  69.             res.append((x+1,y-1))
  70.     if isblack(x,y,grid) and y+1>8:
  71.         if inarea(x,y) and not isblack(x-1,y+1,grid):
  72.             res.append((x-1,y+1))
  73.         if isblack(x,y-1):
  74.             res.append((x,y+1))
  75.         if x<8 and not isblack(x+1,y+1,grid):
  76.             res.append((x+1,y+1))
  77.     return res
  78.  
  79. def inarea(x,y):
  80.     return (x>-1 or x<9) and (y>-1 or y<9)
  81.  
  82. def listmouvtowerdisp(x,y,grid):
  83.     res = []
  84.     if iswhite(x,y,grid):
  85.         for step in range(4):
  86.             i,j = x,y
  87.             blocked = False
  88.             while not blocked:
  89.                 if step == 0:
  90.                     i -= 1
  91.                     if inarea(i,j) and not iswhite(i,j,grid):
  92.                         res.append((i,j))
  93.                     else:
  94.                         blocked = True
  95.                 if step == 1:
  96.                     i += 1
  97.                     if inarea(i,j) and not iswhite(i,j,grid):
  98.                         res.append((i,j))
  99.                     else:
  100.                         blocked = True
  101.                 if step == 2:
  102.                     j -= 1
  103.                     if inarea(i,j) and not iswhite(i,j,grid):
  104.                         res.append((i,j))
  105.                     else:
  106.                         blocked = True
  107.                 if step == 3:
  108.                     j += 1
  109.                     if inarea(i,j) and not iswhite(i,j,grid):
  110.                         res.append((i,j))
  111.                     else:
  112.                         blocked = True
  113.     if isblack(x,y,grid):
  114.         for step in range(4):
  115.             i,j = x,y
  116.             blocked = False
  117.             while not blocked:
  118.                 if step == 0:
  119.                     i -= 1
  120.                     if inarea(i,j) and not isblack(i,j,grid):
  121.                         res.append((i,j))
  122.                     else:
  123.                         blocked = True
  124.                 if step == 1:
  125.                     i += 1
  126.                     if inarea(i,j) and not isblack(i,j,grid):
  127.                         res.append((i,j))
  128.                     else:
  129.                         blocked = True
  130.                 if step == 2:
  131.                     j -= 1
  132.                     if inarea(i,j) and not isblack(i,j,grid):
  133.                         res.append((i,j))
  134.                     else:
  135.                         blocked = True
  136.                 if step == 3:
  137.                     j += 1
  138.                     if inarea(i,j) and not isblack(i,j,grid):
  139.                         res.append((i,j))
  140.                     else:
  141.                         blocked = True
  142.     return res
  143.  
  144. def listmouvcrazydisp(x,y,grid):
  145.     res = []
  146.     if iswhite(x,y,grid):
  147.         for step in range(4):
  148.             i,j = x,y
  149.             blocked = False
  150.             while not blocked:
  151.                 if step == 0:
  152.                     i -= 1
  153.                     j -= 1
  154.                     if inarea(i,j) and not iswhite(i,j,grid):
  155.                         res.append((i,j))
  156.                     else:
  157.                         blocked = True
  158.                 if step == 1:
  159.                     i += 1
  160.                     j -= 1
  161.                     if inarea(i,j) and not iswhite(i,j,grid):
  162.                         res.append((i,j))
  163.                     else:
  164.                         blocked = True
  165.                 if step == 2:
  166.                     i -= 1
  167.                     j += 1
  168.                     if inarea(i,j) and not iswhite(i,j,grid):
  169.                         res.append((i,j))
  170.                     else:
  171.                         blocked = True
  172.                 if step == 3:
  173.                     i += 1
  174.                     j += 1
  175.                     if inarea(i,j) and not iswhite(i,j,grid):
  176.                         res.append((i,j))
  177.                     else:
  178.                         blocked = True
  179.     if isblack(x,y,grid):
  180.         for step in range(4):
  181.             i,j = x,y
  182.             blocked = False
  183.             while not blocked:
  184.                 if step == 0:
  185.                     i -= 1
  186.                     j -= 1
  187.                     if inarea(i,j) and not isblack(i,j,grid):
  188.                         res.append((i,j))
  189.                     else:
  190.                         blocked = True
  191.                 if step == 1:
  192.                     i += 1
  193.                     j -= 1
  194.                     if inarea(i,j) and not isblack(i,j,grid):
  195.                         res.append((i,j))
  196.                     else:
  197.                         blocked = True
  198.                 if step == 2:
  199.                     i -= 1
  200.                     j += 1
  201.                     if inarea(i,j) and not isblack(i,j,grid):
  202.                         res.append((i,j))
  203.                     else:
  204.                         blocked = True
  205.                 if step == 3:
  206.                     i += 1
  207.                     j += 1
  208.                     if inarea(i,j) and not isblack(i,j,grid):
  209.                         res.append((i,j))
  210.                     else:
  211.                         blocked = True
  212.     return res
  213.                
  214. def listmouvqueendisp(x,y,grid):
  215.     return listmouvcrazydisp(x,y,grid) + listmouvtowerdisp(x,y,grid)
  216.  
  217. def listmouvkingdisp(x,y,grid):
  218.     res = []
  219.     if iswhite(x,y,grid):
  220.         if inarea(x-1,y-1,grid) and not iswhite(x-1,y-1,grid):
  221.             res.append((x-1,y-1))
  222.         if inarea(x,y-1,grid) and not iswhite(x,y-1,grid):
  223.             res.append((x,y-1))
  224.         if inarea(x+1,y-1,grid) and not iswhite(x+1,y-1,grid):
  225.             res.append((x+1,y-1))
  226.         if inarea(x-1,y,grid) and not iswhite(x-1,y,grid):
  227.             res.append((x-1,y))
  228.         if inarea(x+1,y,grid) and not iswhite(x+1,y,grid):
  229.             res.append((x+1,y))
  230.         if inarea(x-1,y+1,grid) and not iswhite(x-1,y+1,grid):
  231.             res.append((x-1,y+1))
  232.         if inarea(x,y+1,grid) and not iswhite(x,y+1,grid):
  233.             res.append((x,y+1))
  234.         if inarea(x+1,y+1,grid) and not iswhite(x+1,y+1,grid):
  235.             res.append((x+1,y+1))
  236.     if isblack(x,y,grid):
  237.         if inarea(x-1,y-1,grid) and not isblack(x-1,y-1,grid):
  238.             res.append((x-1,y-1))
  239.         if inarea(x,y-1,grid) and not isblack(x,y-1,grid):
  240.             res.append((x,y-1))
  241.         if inarea(x+1,y-1,grid) and not isblack(x+1,y-1,grid):
  242.             res.append((x+1,y-1))
  243.         if inarea(x-1,y,grid) and not isblack(x-1,y,grid):
  244.             res.append((x-1,y))
  245.         if inarea(x+1,y,grid) and not isblack(x+1,y,grid):
  246.             res.append((x+1,y))
  247.         if inarea(x-1,y+1,grid) and not isblack(x-1,y+1,grid):
  248.             res.append((x-1,y+1))
  249.         if inarea(x,y+1,grid) and not isblack(x,y+1,grid):
  250.             res.append((x,y+1))
  251.         if inarea(x+1,y+1,grid) and not isblack(x+1,y+1,grid):
  252.             res.append((x+1,y+1))
  253.     return res
  254.  
  255. def listmouvhorsedisp(x,y,grid):
  256.     res = []
  257.     if iswhite(x,y,grid):
  258.         if inarea(x-2,y-1,grid) and not iswhite(x-2,y-1,grid):
  259.             res.append(x-2,y-1,grid)
  260.         if inarea(x-2,y+1,grid) and not iswhite(x-2,y+1,grid):
  261.             res.append(x-2,y+1,grid)
  262.         if inarea(x-1,y-2,grid) and not iswhite(x-1,y-2,grid):
  263.             res.append(x-1,y-2,grid)
  264.         if inarea(x-1,y+2,grid) and not iswhite(x-1,y+2,grid):
  265.             res.append(x-1,y+2,grid)
  266.         if inarea(x+1,y+2,grid) and not iswhite(x+1,y+2,grid):
  267.             res.append(x+2,y+2,grid)
  268.         if inarea(x+1,y-2,grid) and not iswhite(x+1,y-2,grid):
  269.             res.append(x+1,y-2,grid)
  270.         if inarea(x+2,y-1,grid) and not iswhite(x+2,y-1,grid):
  271.             res.append(x+2,y-1,grid)
  272.         if inarea(x+2,y+1,grid) and not iswhite(x+2,y+1,grid):
  273.             res.append(x+2,y+1,grid)
  274.     if isblack(x,y,grid):
  275.         if inarea(x-2,y-1,grid) and not isblack(x-2,y-1,grid):
  276.             res.append(x-2,y-1,grid)
  277.         if inarea(x-2,y+1,grid) and not isblack(x-2,y+1,grid):
  278.             res.append(x-2,y+1,grid)
  279.         if inarea(x-1,y-2,grid) and not isblack(x-1,y-2,grid):
  280.             res.append(x-1,y-2,grid)
  281.         if inarea(x-1,y+2,grid) and not isblack(x-1,y+2,grid):
  282.             res.append(x-1,y+2,grid)
  283.         if inarea(x+1,y+2,grid) and not isblack(x+1,y+2,grid):
  284.             res.append(x+2,y+2,grid)
  285.         if inarea(x+1,y-2,grid) and not isblack(x+1,y-2,grid):
  286.             res.append(x+1,y-2,grid)
  287.         if inarea(x+2,y-1,grid) and not isblack(x+2,y-1,grid):
  288.             res.append(x+2,y-1,grid)
  289.         if inarea(x+2,y+1,grid) and not isblack(x+2,y+1,grid):
  290.             res.append(x+2,y+1,grid)
  291.     return res
  292.  
  293.  
  294.  
  295. print("--- %s seconds ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement