Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Fri Jul 21 16:16:24 2017
- @author: lollilol
- """
- import time
- import random
- """
- _ is an empty place
- a is a wpion
- b is a wtower
- c is a whorse
- d is a wcrazy
- e is a wking
- f is a wqueen
- g is a bpion
- h is a btower
- i is a bhorse
- j is a bcrazy
- k is a bking
- l is a bqueen
- """
- start_time = time.time()
- memory = open('C:\\Users\\lolli\\Desktop\\Mehdi\\Travail\\Test Code\\ChessIA')
- chess = [['h','i','j','k','l','j','i','h'],
- ['g','g','g','g','g','g','g','g'],
- ['_','_','_','_','_','_','_','_'],
- ['_','_','_','_','_','_','_','_'],
- ['_','_','_','_','_','_','_','_'],
- ['_','_','_','_','_','_','_','_'],
- ['a','a','a','a','a','a','a','a'],
- ['b','c','d','e','f','d','c','b']]
- def iswhite(x,y,grid):
- 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'
- def isblack(x,y,grid):
- 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'
- def listwhitepos(grid):
- res = []
- for i in range(8):
- for j in range(8):
- if iswhite(i,j,grid):
- res.append((i,j))
- return res
- def listblackpos(grid):
- res = []
- for i in range(8):
- for j in range(8):
- if isblack(i,j,grid):
- res.append((i,j))
- return res
- def listmouvpiondisp(x,y,grid):
- res = []
- if iswhite(x,y,grid) and y-1<0:
- if x>0 and not iswhite(x-1,y-1,grid):
- res.append((x-1,y-1))
- if isblack(x,y-1):
- res.append((x,y-1))
- if x<8 and not iswhite(x+1,y-1,grid):
- res.append((x+1,y-1))
- if isblack(x,y,grid) and y+1>8:
- if inarea(x,y) and not isblack(x-1,y+1,grid):
- res.append((x-1,y+1))
- if isblack(x,y-1):
- res.append((x,y+1))
- if x<8 and not isblack(x+1,y+1,grid):
- res.append((x+1,y+1))
- return res
- def inarea(x,y):
- return (x>-1 or x<9) and (y>-1 or y<9)
- def listmouvtowerdisp(x,y,grid):
- res = []
- if iswhite(x,y,grid):
- for step in range(4):
- i,j = x,y
- blocked = False
- while not blocked:
- if step == 0:
- i -= 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 1:
- i += 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 2:
- j -= 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 3:
- j += 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if isblack(x,y,grid):
- for step in range(4):
- i,j = x,y
- blocked = False
- while not blocked:
- if step == 0:
- i -= 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 1:
- i += 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 2:
- j -= 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 3:
- j += 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- return res
- def listmouvcrazydisp(x,y,grid):
- res = []
- if iswhite(x,y,grid):
- for step in range(4):
- i,j = x,y
- blocked = False
- while not blocked:
- if step == 0:
- i -= 1
- j -= 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 1:
- i += 1
- j -= 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 2:
- i -= 1
- j += 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 3:
- i += 1
- j += 1
- if inarea(i,j) and not iswhite(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if isblack(x,y,grid):
- for step in range(4):
- i,j = x,y
- blocked = False
- while not blocked:
- if step == 0:
- i -= 1
- j -= 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 1:
- i += 1
- j -= 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 2:
- i -= 1
- j += 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- if step == 3:
- i += 1
- j += 1
- if inarea(i,j) and not isblack(i,j,grid):
- res.append((i,j))
- else:
- blocked = True
- return res
- def listmouvqueendisp(x,y,grid):
- return listmouvcrazydisp(x,y,grid) + listmouvtowerdisp(x,y,grid)
- def listmouvkingdisp(x,y,grid):
- res = []
- if iswhite(x,y,grid):
- if inarea(x-1,y-1,grid) and not iswhite(x-1,y-1,grid):
- res.append((x-1,y-1))
- if inarea(x,y-1,grid) and not iswhite(x,y-1,grid):
- res.append((x,y-1))
- if inarea(x+1,y-1,grid) and not iswhite(x+1,y-1,grid):
- res.append((x+1,y-1))
- if inarea(x-1,y,grid) and not iswhite(x-1,y,grid):
- res.append((x-1,y))
- if inarea(x+1,y,grid) and not iswhite(x+1,y,grid):
- res.append((x+1,y))
- if inarea(x-1,y+1,grid) and not iswhite(x-1,y+1,grid):
- res.append((x-1,y+1))
- if inarea(x,y+1,grid) and not iswhite(x,y+1,grid):
- res.append((x,y+1))
- if inarea(x+1,y+1,grid) and not iswhite(x+1,y+1,grid):
- res.append((x+1,y+1))
- if isblack(x,y,grid):
- if inarea(x-1,y-1,grid) and not isblack(x-1,y-1,grid):
- res.append((x-1,y-1))
- if inarea(x,y-1,grid) and not isblack(x,y-1,grid):
- res.append((x,y-1))
- if inarea(x+1,y-1,grid) and not isblack(x+1,y-1,grid):
- res.append((x+1,y-1))
- if inarea(x-1,y,grid) and not isblack(x-1,y,grid):
- res.append((x-1,y))
- if inarea(x+1,y,grid) and not isblack(x+1,y,grid):
- res.append((x+1,y))
- if inarea(x-1,y+1,grid) and not isblack(x-1,y+1,grid):
- res.append((x-1,y+1))
- if inarea(x,y+1,grid) and not isblack(x,y+1,grid):
- res.append((x,y+1))
- if inarea(x+1,y+1,grid) and not isblack(x+1,y+1,grid):
- res.append((x+1,y+1))
- return res
- def listmouvhorsedisp(x,y,grid):
- res = []
- if iswhite(x,y,grid):
- if inarea(x-2,y-1,grid) and not iswhite(x-2,y-1,grid):
- res.append(x-2,y-1,grid)
- if inarea(x-2,y+1,grid) and not iswhite(x-2,y+1,grid):
- res.append(x-2,y+1,grid)
- if inarea(x-1,y-2,grid) and not iswhite(x-1,y-2,grid):
- res.append(x-1,y-2,grid)
- if inarea(x-1,y+2,grid) and not iswhite(x-1,y+2,grid):
- res.append(x-1,y+2,grid)
- if inarea(x+1,y+2,grid) and not iswhite(x+1,y+2,grid):
- res.append(x+2,y+2,grid)
- if inarea(x+1,y-2,grid) and not iswhite(x+1,y-2,grid):
- res.append(x+1,y-2,grid)
- if inarea(x+2,y-1,grid) and not iswhite(x+2,y-1,grid):
- res.append(x+2,y-1,grid)
- if inarea(x+2,y+1,grid) and not iswhite(x+2,y+1,grid):
- res.append(x+2,y+1,grid)
- if isblack(x,y,grid):
- if inarea(x-2,y-1,grid) and not isblack(x-2,y-1,grid):
- res.append(x-2,y-1,grid)
- if inarea(x-2,y+1,grid) and not isblack(x-2,y+1,grid):
- res.append(x-2,y+1,grid)
- if inarea(x-1,y-2,grid) and not isblack(x-1,y-2,grid):
- res.append(x-1,y-2,grid)
- if inarea(x-1,y+2,grid) and not isblack(x-1,y+2,grid):
- res.append(x-1,y+2,grid)
- if inarea(x+1,y+2,grid) and not isblack(x+1,y+2,grid):
- res.append(x+2,y+2,grid)
- if inarea(x+1,y-2,grid) and not isblack(x+1,y-2,grid):
- res.append(x+1,y-2,grid)
- if inarea(x+2,y-1,grid) and not isblack(x+2,y-1,grid):
- res.append(x+2,y-1,grid)
- if inarea(x+2,y+1,grid) and not isblack(x+2,y+1,grid):
- res.append(x+2,y+1,grid)
- return res
- print("--- %s seconds ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement