Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- """
- wn = turtle.Screen()
- wn.setworldcoordinates(-100,-100,100,100)
- wn.bgcolor('lightyellow')
- wn.register_shape("Car.gif")
- Bug = turtle.Turtle()
- Spider = turtle.Turtle()
- Bug.penup()
- Bug.goto(0,5)
- Bug.pendown()
- Spider.goto(0,0)
- """
- def BoardMaker():
- # Creating the board
- board = []
- a = []
- for i in range(8):
- for j in range(8):
- a.append(['pos',0])
- board.append(a)
- a = []
- return board
- #print(BoardMaker())
- def SetPawns(board):
- #Placing the pawns on the board
- global wn,white_man,black_man
- wn.tracer(False)
- turtle.showturtle()
- for i in range(8):
- for j in range(0,8,2):
- if i == 0 or i == 2:
- a = turtle.Turtle()
- a.shape(white_man)
- a.penup()
- a.goto(board[i][j][0])
- a.onclick(getpos1)
- board[i][j][1] = a
- if i == 1:
- a = turtle.Turtle()
- a.shape(white_man)
- a.penup()
- a.goto(board[i][j+1][0])
- a.onclick(getpos1)
- board[i][j+1][1] = a
- if i == 6:
- a = turtle.Turtle()
- a.shape(black_man)
- a.penup()
- a.goto(board[i][j][0])
- a.onclick(getpos1)
- board[i][j][1] = a
- if i == 5 or i == 7:
- a = turtle.Turtle()
- a.shape(black_man)
- a.penup()
- a.goto(board[i][j+1][0])
- a.onclick(getpos1)
- board[i][j+1][1] = a
- wn.tracer(True)
- turtle.showturtle()
- return board
- def SetPawns1(board):
- global wn,white_man,black_man
- wn.tracer(False)
- turtle.showturtle()
- for j in range(8):
- for i in range(0,8,2):
- if j == 0 or j == 2:
- a = turtle.Turtle()
- a.shape(white_man)
- a.turtlesize(90,90)
- a.penup()
- a.goto(board[i][j][0])
- a.onclick(getpos1)
- board[i][j][1] = a
- if j == 1:
- a = turtle.Turtle()
- a.shape(white_man)
- a.penup()
- a.goto(board[i+1][j][0])
- a.onclick(getpos1)
- board[i+1][j][1] = a
- if j == 5 or j == 7:
- a = turtle.Turtle()
- a.shape(black_man)
- a.penup()
- a.goto(board[i+1][j][0])
- a.onclick(getpos1)
- board[i+1][j][1] = a
- if j == 6:
- a = turtle.Turtle()
- a.shape(black_man)
- a.penup()
- a.goto(board[i][j][0])
- a.onclick(getpos1)
- board[i][j][1] = a
- wn.tracer(True)
- turtle.showturtle()
- return board
- def SetCoordinates(board):
- x=-315
- y=-315
- for i in range(8):
- for j in range(8):
- board[i][j][0] = (x,y)
- y+=90
- x+=90
- y=-315
- return board
- def DrawBoard():
- wn.tracer(False)
- # Draw chess board borders
- # turtle.tracer(False)
- turtle.pensize(3) # Set pen thickness to 3 pixels
- turtle.penup() # Pull the pen up
- turtle.goto(-360, -360)
- turtle.pendown() # Pull the pen down
- turtle.color("black")
- for i in range(4):
- turtle.forward(720) # Draw a line
- turtle.left(90) # Turn left 90 degrees
- # Draw chess board inside
- turtle.color("dark slate gray")
- #turtle.color("brown")
- for j in range(-360, 270, 180):
- for i in range(-360, 360, 180):
- turtle.penup()
- turtle.goto(i, j)
- turtle.pendown()
- # Draw a small rectangle
- turtle.begin_fill()
- for k in range(4):
- turtle.forward(90) # Draw a line
- turtle.left(90) # Turn left 90 degrees
- turtle.end_fill()
- for j in range(-270, 360, 180):
- for i in range(-270, 360, 180):
- turtle.penup()
- turtle.goto(i, j)
- turtle.pendown()
- # Draw a small rectangle
- turtle.begin_fill()
- for k in range(4):
- turtle.forward(90) # Draw a line
- turtle.left(90) # Turn left 90 degrees
- turtle.end_fill()
- wn.tracer(True)
- #turtle.hideturtle()
- #turtle.done()
- def setboard():
- global board,wn
- wn.tracer(False)
- tiles = []
- for i in range(8):
- for j in range(8):
- a = turtle.Turtle()
- a.penup()
- #a.ht()
- a.shape('square')
- a.turtlesize(90,90)
- a.goto(board[i][j][0])
- a.onclick(settar)
- tiles.append(a)
- wn.tracer(True)
- return tiles
- def turnKing(target,color):
- global board,white_king,black_king
- if 'white' in color:
- board[target[0]][target[1]][1].shape(white_king)
- if 'black' in color:
- board[target[0]][target[1]][1].shape(black_king)
- board[target[0]][target[1]][1].onclick(getpos1)
- def move(target):
- global board,p1,counter
- moved = False
- i,j = getpos(p1)
- legalst = legal(p1)
- if target in legalst:
- p1.goto(board[target[0]][target[1]][0])
- board[target[0]][target[1]][1] = board[i][j][1]
- board[i][j][1] = 0
- p1 = 0
- counter += 1
- moved = True
- if counter%2 == 0:
- print('white turn')
- else:
- print('black turn')
- if moved == True:
- if 'white' in legalst:
- print('KING')
- turnKing(target,'white')
- if 'black' in legalst:
- print('KING')
- turnKing(target,'black')
- return(moved)
- def moveking(target):
- global board,counter,p1
- moved = False
- i,j = getpos(p1)
- legalst = kinglegal(p1)
- print(legalst,'kinglist')
- if target in legalst:
- p1.goto(board[target[0]][target[1]][0])
- board[target[0]][target[1]][1] = board[i][j][1]
- board[i][j][1] = 0
- p1 = 0
- counter += 1
- moved = True
- return(moved)
- def eat(tar):
- global wn,board,counter,p1,moving
- moved = False
- moving = True
- mbking = False
- i,j = getpos(p1)
- if 'white' in p1.shape():
- if tar == (i+2,j+2) and board[i+2][j+2][1]==0 and 'black' in board[i+1][j+1][1].shape():
- print('1')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i+1][j+1][1].hideturtle()
- board[i+1][j+1][1] = 0
- board[i+2][j+2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- mbking = tar
- moved = True
- if tar == (i-2,j+2) and board[i-2][j+2][1]==0 and 'black' in board[i-1][j+1][1].shape():
- print('2')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i-1][j+1][1].hideturtle()
- board[i-1][j+1][1] = 0
- board[i-2][j+2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- mbking = tar
- moved = True
- if tar == (i+2,j-2) and board[i+2][j-2][1]==0 and 'black' in board[i+1][j-1][1].shape():
- print('3')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i+1][j-1][1].hideturtle()
- board[i+1][j-1][1] = 0
- board[i+2][j-2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- moved = True
- if tar == (i-2,j-2) and board[i-2][j-2][1]==0 and 'black' in board[i-1][j-1][1].shape():
- print('5')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i-1][j-1][1].hideturtle()
- board[i-1][j-1][1] = 0
- board[i-2][j-2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- moved = True
- try:
- if mbking[1] == 7:
- print('KING')
- turnKing(mbking,'white')
- except Exception as e :
- #print(e)
- pass
- if moved == False and 'black' in p1.shape():
- if tar == (i+2,j-2) and board[i+2][j-2][1]==0 and 'white' in board[i+1][j-1][1].shape():
- print('3')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i+1][j-1][1].hideturtle()
- board[i+1][j-1][1] = 0
- board[i+2][j-2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- moved = True
- mbking = tar
- if tar == (i-2,j-2) and board[i-2][j-2][1]==0 and 'white' in board[i-1][j-1][1].shape():
- print('5')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i-1][j-1][1].hideturtle()
- board[i-1][j-1][1] = 0
- board[i-2][j-2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- moved = True
- mbking = tar
- if tar == (i+2,j+2) and board[i+2][j+2][1]==0 and 'white' in board[i+1][j+1][1].shape():
- print('1')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i+1][j+1][1].hideturtle()
- board[i+1][j+1][1] = 0
- board[i+2][j+2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- moved = True
- if tar == (i-2,j+2) and board[i-2][j+2][1]==0 and 'white' in board[i-1][j+1][1].shape():
- print('2')
- p1.goto(board[tar[0]][tar[1]][0])
- board[i-1][j+1][1].hideturtle()
- board[i-1][j+1][1] = 0
- board[i-2][j+2][1] = board[i][j][1]
- board[i][j][1] = 0
- temp1 = board[tar[0]][tar[1]][1]
- p1 = 0
- moved = True
- try:
- if mbking[1] == 0:
- print('KING')
- turnKing(mbking,'black')
- except Exception as e :
- #print(e)
- pass
- if moved == True and moving == True:
- p1 = temp1
- counter+=1
- return(True)
- if moved == False:
- return(False)
- if moved == True and moving == False:
- counter+=1
- if counter%2 == 0:
- print('white turn')
- else:
- print('black turn')
- return(False)
- '''
- if moved == True:
- print('move-true')
- p1 = temp1
- getpos1(p1.pos
- '''
- ###
- isempty(board)
- def isempty(board):
- white = 0
- black = 0
- p = 0
- for i in range(8):
- for j in range(8):
- try:
- p = board[i][j][1]
- if 'white' in p.shape():
- white += 1
- except: Exception
- try:
- p = board[i][j][1]
- if 'black' in p.shape():
- black += 1
- except: Exception
- if white == 0 and black != 0:
- print('black wins')
- if white != 0 and black == 0:
- print('white wins')
- def getpos(pawn):
- #print(pawn)
- i = int((pawn.pos()[0]+380)/90)
- j = int((pawn.pos()[1]+380)/90)
- return(i,j)
- def getpos1(x,y):
- global p1,board,counter,wn,moving
- moving = False
- i = int((x+380)/90)
- j = int((y+380)/90)
- if counter%2 == 0:
- try:
- temp1 = board[i][j][1]
- if 'white' in temp1.shape():
- p1 = temp1
- wn.onclick(settar,1)
- except Exception as e:
- print(e,'getpos11 error')
- pass
- if counter%2 == 1:
- try:
- temp1 = board[i][j][1]
- if 'black' in temp1.shape():
- p1 = temp1
- wn.onclick(settar,1)
- except Exception as e:
- print(e,'getpos22 error')
- pass
- print('I = ',i,'J = ',j)
- def settar(x,y):
- global tar
- i = int((x+380)/95)
- j = int((y+380)/95)
- tar = (i,j)
- #wn.onclick(settar,2)
- trytomove()
- print(i,j,'tar')
- '''
- try:
- trytomove(x,y)
- except Exception :
- pass
- try:
- trytoeat()
- except Exception :
- pass
- '''
- def trytomove():
- global tar,p1,counter
- print(p1)
- try:
- if eat(tar):
- moved = True
- while(moved == True):
- moved = eat(tar)
- #p1 = 0
- except Exception as e :
- print(e,'tomov2')
- pass
- moved = False
- print(p1)
- if moved == False:
- if 'king' in p1.shape():
- try:
- moved = moveking(tar)
- except Exception as e :
- print(e,'tomov0')
- pass
- if 'man' in p1.shape():
- try:
- moved = move(tar)
- except Exception as e :
- print(e,'tomov1')
- pass
- '''
- if white, check if i+2,j+2 is empty and i+1, j+1 is black
- " " " " i-2,j+2 is empty and i-1, j+1 is black
- if black, check if i-2,j-2 is black, i-1,j-
- delete
- '''
- def legal(pawn):
- global board
- i,j = getpos(pawn)
- lst=[]
- #print(board[1][3])
- if 'white' in pawn.shape():
- if i!=0 and i!=7 and j!=7:
- if board[i+1][j+1][1]==0:
- lst.append((i+1,j+1))
- if board[i-1][j+1][1]==0:
- lst.append((i-1,j+1))
- if i==0 and j!=7:
- if board[i+1][j+1][1]==0:
- lst.append((i+1,j+1))
- if j!=7:
- if board[i-1][j+1][1]==0:
- lst.append((i-1,j+1))
- if j+1 == 7:
- lst.append('white')
- if "black" in pawn.shape():
- if i!=0 and i!=7 and j!=0:
- if board[i+1][j-1][1]==0:
- lst.append((i+1,j-1))
- if board[i-1][j-1][1]==0:
- lst.append((i-1,j-1))
- if i==0 and j!=0:
- if board[i+1][j-1][1]==0:
- lst.append((i+1,j-1))
- if j!=0 :
- if board[i-1][j-1][1]==0:
- lst.append((i-1,j-1))
- if j-1 == 0:
- lst.append('black')
- return lst
- def kinglegal(king):
- global board
- print('kinglegal')
- i,j = getpos(king)
- lst=[]
- #print(board[1][3])
- if i!=0 and i!=7 and j!=7:
- if board[i+1][j+1][1]==0:
- lst.append((i+1,j+1))
- if board[i-1][j+1][1]==0:
- lst.append((i-1,j+1))
- if i==0 and j!=7:
- if board[i+1][j+1][1]==0:
- lst.append((i+1,j+1))
- if j!=7:
- if board[i-1][j+1][1]==0:
- lst.append((i-1,j+1))
- if i!=0 and i!=7 and j!=0:
- if board[i+1][j-1][1]==0:
- lst.append((i+1,j-1))
- if board[i-1][j-1][1]==0:
- lst.append((i-1,j-1))
- if i==0 and j!=0:
- if board[i+1][j-1][1]==0:
- lst.append((i+1,j-1))
- if j!=0 :
- if board[i-1][j-1][1]==0:
- lst.append((i-1,j-1))
- return lst
- #def onclick():
- wn = turtle.Screen()
- wn.setworldcoordinates(-380,-380,380,380)
- wn.bgcolor('lightyellow')
- white_man = r"D:\Coding\Python\damka\imgs\white_man.gif"
- white_king = r"D:\Coding\Python\damka\imgs\white_king.gif"
- black_man = r"D:\Coding\Python\damka\imgs\black_man.gif"
- black_king = r"D:\Coding\Python\damka\imgs\black_king.gif"
- wn.register_shape(white_man)
- wn.register_shape(white_king)
- wn.register_shape(black_man)
- wn.register_shape(black_king)
- wn.onclick(settar,2)
- wn.onclick(trytomove,3)
- turtle.resizemode("user")
- #wn.tracer(False)
- def start():
- global wn, white_man,black_man,board,p1,counter,black_king,white_king
- counter = 0
- board=BoardMaker()
- board=SetCoordinates(board)
- #tiles = setboard()
- DrawBoard()
- board=SetPawns1(board)
- wn.listen()
- turtle.listen()
- #print(board[1][3])
- start()
- turtle.done()
- def initiate():
- global wn, white_man,black_man,board,p1,tar
- start()
- try:
- move(p1,tar)
- except Exception:
- pass
- #wn.listen()
- #initiate()
- import sys
- import os
- def restart_program():
- """Restarts the current program.
- Note: this function does not return. Any cleanup action (like
- saving data) must be done before calling this function."""
- python = sys.executable
- os.execl(python, python, * sys.argv)
- if __name__ == "__main__":
- answer = raw_input("Do you want to restart this program ? ")
- if answer.lower().strip() in "y yes".split():
- restart_program()
- '''
- I = עמודות
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement