Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.53 KB | None | 0 0
  1. import turtle
  2.  
  3.  
  4. """
  5. wn = turtle.Screen()
  6. wn.setworldcoordinates(-100,-100,100,100)
  7. wn.bgcolor('lightyellow')
  8.  
  9. wn.register_shape("Car.gif")
  10. Bug = turtle.Turtle()
  11. Spider = turtle.Turtle()
  12. Bug.penup()
  13. Bug.goto(0,5)
  14. Bug.pendown()
  15. Spider.goto(0,0)
  16. """
  17.  
  18.  
  19.  
  20. def BoardMaker():
  21.  
  22. # Creating the board
  23. board = []
  24. a = []
  25. for i in range(8):
  26. for j in range(8):
  27. a.append(['pos',0])
  28. board.append(a)
  29. a = []
  30.  
  31. return board
  32.  
  33. #print(BoardMaker())
  34.  
  35. def SetPawns(board):
  36. #Placing the pawns on the board
  37. global wn,white_man,black_man
  38. wn.tracer(False)
  39. turtle.showturtle()
  40. for i in range(8):
  41.  
  42. for j in range(0,8,2):
  43.  
  44. if i == 0 or i == 2:
  45. a = turtle.Turtle()
  46. a.shape(white_man)
  47. a.penup()
  48. a.goto(board[i][j][0])
  49. a.onclick(getpos1)
  50. board[i][j][1] = a
  51. if i == 1:
  52. a = turtle.Turtle()
  53. a.shape(white_man)
  54. a.penup()
  55. a.goto(board[i][j+1][0])
  56. a.onclick(getpos1)
  57. board[i][j+1][1] = a
  58. if i == 6:
  59. a = turtle.Turtle()
  60. a.shape(black_man)
  61. a.penup()
  62. a.goto(board[i][j][0])
  63. a.onclick(getpos1)
  64. board[i][j][1] = a
  65. if i == 5 or i == 7:
  66. a = turtle.Turtle()
  67. a.shape(black_man)
  68. a.penup()
  69. a.goto(board[i][j+1][0])
  70. a.onclick(getpos1)
  71. board[i][j+1][1] = a
  72. wn.tracer(True)
  73. turtle.showturtle()
  74. return board
  75. def SetPawns1(board):
  76. global wn,white_man,black_man
  77. wn.tracer(False)
  78. turtle.showturtle()
  79.  
  80. for j in range(8):
  81. for i in range(0,8,2):
  82.  
  83. if j == 0 or j == 2:
  84. a = turtle.Turtle()
  85. a.shape(white_man)
  86. a.turtlesize(90,90)
  87. a.penup()
  88. a.goto(board[i][j][0])
  89. a.onclick(getpos1)
  90. board[i][j][1] = a
  91.  
  92. if j == 1:
  93. a = turtle.Turtle()
  94. a.shape(white_man)
  95. a.penup()
  96. a.goto(board[i+1][j][0])
  97. a.onclick(getpos1)
  98. board[i+1][j][1] = a
  99.  
  100. if j == 5 or j == 7:
  101. a = turtle.Turtle()
  102. a.shape(black_man)
  103. a.penup()
  104. a.goto(board[i+1][j][0])
  105. a.onclick(getpos1)
  106. board[i+1][j][1] = a
  107.  
  108. if j == 6:
  109. a = turtle.Turtle()
  110. a.shape(black_man)
  111. a.penup()
  112. a.goto(board[i][j][0])
  113. a.onclick(getpos1)
  114. board[i][j][1] = a
  115. wn.tracer(True)
  116. turtle.showturtle()
  117. return board
  118.  
  119.  
  120. def SetCoordinates(board):
  121. x=-315
  122. y=-315
  123.  
  124. for i in range(8):
  125.  
  126.  
  127.  
  128.  
  129. for j in range(8):
  130.  
  131. board[i][j][0] = (x,y)
  132. y+=90
  133. x+=90
  134. y=-315
  135.  
  136.  
  137. return board
  138.  
  139.  
  140. def DrawBoard():
  141. wn.tracer(False)
  142. # Draw chess board borders
  143.  
  144. # turtle.tracer(False)
  145. turtle.pensize(3) # Set pen thickness to 3 pixels
  146. turtle.penup() # Pull the pen up
  147. turtle.goto(-360, -360)
  148. turtle.pendown() # Pull the pen down
  149. turtle.color("black")
  150.  
  151. for i in range(4):
  152. turtle.forward(720) # Draw a line
  153. turtle.left(90) # Turn left 90 degrees
  154.  
  155. # Draw chess board inside
  156. turtle.color("dark slate gray")
  157. #turtle.color("brown")
  158. for j in range(-360, 270, 180):
  159. for i in range(-360, 360, 180):
  160. turtle.penup()
  161. turtle.goto(i, j)
  162. turtle.pendown()
  163.  
  164. # Draw a small rectangle
  165. turtle.begin_fill()
  166. for k in range(4):
  167. turtle.forward(90) # Draw a line
  168. turtle.left(90) # Turn left 90 degrees
  169. turtle.end_fill()
  170.  
  171. for j in range(-270, 360, 180):
  172. for i in range(-270, 360, 180):
  173. turtle.penup()
  174. turtle.goto(i, j)
  175. turtle.pendown()
  176.  
  177. # Draw a small rectangle
  178. turtle.begin_fill()
  179. for k in range(4):
  180. turtle.forward(90) # Draw a line
  181. turtle.left(90) # Turn left 90 degrees
  182. turtle.end_fill()
  183. wn.tracer(True)
  184. #turtle.hideturtle()
  185.  
  186. #turtle.done()
  187.  
  188. def setboard():
  189. global board,wn
  190. wn.tracer(False)
  191. tiles = []
  192. for i in range(8):
  193. for j in range(8):
  194. a = turtle.Turtle()
  195. a.penup()
  196. #a.ht()
  197. a.shape('square')
  198. a.turtlesize(90,90)
  199. a.goto(board[i][j][0])
  200. a.onclick(settar)
  201. tiles.append(a)
  202. wn.tracer(True)
  203. return tiles
  204.  
  205. def turnKing(target,color):
  206.  
  207.  
  208. global board,white_king,black_king
  209.  
  210. if 'white' in color:
  211. board[target[0]][target[1]][1].shape(white_king)
  212.  
  213. if 'black' in color:
  214. board[target[0]][target[1]][1].shape(black_king)
  215. board[target[0]][target[1]][1].onclick(getpos1)
  216.  
  217.  
  218. def move(target):
  219.  
  220.  
  221. global board,p1,counter
  222. moved = False
  223. i,j = getpos(p1)
  224. legalst = legal(p1)
  225. if target in legalst:
  226.  
  227. p1.goto(board[target[0]][target[1]][0])
  228. board[target[0]][target[1]][1] = board[i][j][1]
  229. board[i][j][1] = 0
  230. p1 = 0
  231. counter += 1
  232. moved = True
  233.  
  234. if counter%2 == 0:
  235. print('white turn')
  236. else:
  237. print('black turn')
  238. if moved == True:
  239. if 'white' in legalst:
  240. print('KING')
  241. turnKing(target,'white')
  242. if 'black' in legalst:
  243. print('KING')
  244. turnKing(target,'black')
  245. return(moved)
  246.  
  247.  
  248. def moveking(target):
  249.  
  250.  
  251. global board,counter,p1
  252. moved = False
  253. i,j = getpos(p1)
  254. legalst = kinglegal(p1)
  255. print(legalst,'kinglist')
  256. if target in legalst:
  257. p1.goto(board[target[0]][target[1]][0])
  258. board[target[0]][target[1]][1] = board[i][j][1]
  259. board[i][j][1] = 0
  260. p1 = 0
  261. counter += 1
  262. moved = True
  263. return(moved)
  264.  
  265. def eat(tar):
  266.  
  267.  
  268. global wn,board,counter,p1,moving
  269. moved = False
  270. moving = True
  271. mbking = False
  272. i,j = getpos(p1)
  273.  
  274. if 'white' in p1.shape():
  275.  
  276. if tar == (i+2,j+2) and board[i+2][j+2][1]==0 and 'black' in board[i+1][j+1][1].shape():
  277. print('1')
  278. p1.goto(board[tar[0]][tar[1]][0])
  279. board[i+1][j+1][1].hideturtle()
  280. board[i+1][j+1][1] = 0
  281. board[i+2][j+2][1] = board[i][j][1]
  282. board[i][j][1] = 0
  283. temp1 = board[tar[0]][tar[1]][1]
  284. p1 = 0
  285. mbking = tar
  286. moved = True
  287.  
  288.  
  289. if tar == (i-2,j+2) and board[i-2][j+2][1]==0 and 'black' in board[i-1][j+1][1].shape():
  290. print('2')
  291. p1.goto(board[tar[0]][tar[1]][0])
  292. board[i-1][j+1][1].hideturtle()
  293. board[i-1][j+1][1] = 0
  294. board[i-2][j+2][1] = board[i][j][1]
  295. board[i][j][1] = 0
  296. temp1 = board[tar[0]][tar[1]][1]
  297. p1 = 0
  298. mbking = tar
  299. moved = True
  300.  
  301. if tar == (i+2,j-2) and board[i+2][j-2][1]==0 and 'black' in board[i+1][j-1][1].shape():
  302. print('3')
  303. p1.goto(board[tar[0]][tar[1]][0])
  304. board[i+1][j-1][1].hideturtle()
  305. board[i+1][j-1][1] = 0
  306. board[i+2][j-2][1] = board[i][j][1]
  307. board[i][j][1] = 0
  308. temp1 = board[tar[0]][tar[1]][1]
  309. p1 = 0
  310. moved = True
  311.  
  312.  
  313. if tar == (i-2,j-2) and board[i-2][j-2][1]==0 and 'black' in board[i-1][j-1][1].shape():
  314. print('5')
  315. p1.goto(board[tar[0]][tar[1]][0])
  316. board[i-1][j-1][1].hideturtle()
  317. board[i-1][j-1][1] = 0
  318. board[i-2][j-2][1] = board[i][j][1]
  319. board[i][j][1] = 0
  320. temp1 = board[tar[0]][tar[1]][1]
  321. p1 = 0
  322. moved = True
  323.  
  324. try:
  325. if mbking[1] == 7:
  326. print('KING')
  327. turnKing(mbking,'white')
  328. except Exception as e :
  329. #print(e)
  330. pass
  331.  
  332.  
  333. if moved == False and 'black' in p1.shape():
  334.  
  335. if tar == (i+2,j-2) and board[i+2][j-2][1]==0 and 'white' in board[i+1][j-1][1].shape():
  336. print('3')
  337. p1.goto(board[tar[0]][tar[1]][0])
  338. board[i+1][j-1][1].hideturtle()
  339. board[i+1][j-1][1] = 0
  340. board[i+2][j-2][1] = board[i][j][1]
  341. board[i][j][1] = 0
  342. temp1 = board[tar[0]][tar[1]][1]
  343. p1 = 0
  344. moved = True
  345. mbking = tar
  346.  
  347.  
  348. if tar == (i-2,j-2) and board[i-2][j-2][1]==0 and 'white' in board[i-1][j-1][1].shape():
  349. print('5')
  350. p1.goto(board[tar[0]][tar[1]][0])
  351. board[i-1][j-1][1].hideturtle()
  352. board[i-1][j-1][1] = 0
  353. board[i-2][j-2][1] = board[i][j][1]
  354. board[i][j][1] = 0
  355. temp1 = board[tar[0]][tar[1]][1]
  356. p1 = 0
  357. moved = True
  358. mbking = tar
  359.  
  360. if tar == (i+2,j+2) and board[i+2][j+2][1]==0 and 'white' in board[i+1][j+1][1].shape():
  361. print('1')
  362. p1.goto(board[tar[0]][tar[1]][0])
  363. board[i+1][j+1][1].hideturtle()
  364. board[i+1][j+1][1] = 0
  365. board[i+2][j+2][1] = board[i][j][1]
  366. board[i][j][1] = 0
  367. temp1 = board[tar[0]][tar[1]][1]
  368. p1 = 0
  369. moved = True
  370.  
  371.  
  372. if tar == (i-2,j+2) and board[i-2][j+2][1]==0 and 'white' in board[i-1][j+1][1].shape():
  373. print('2')
  374. p1.goto(board[tar[0]][tar[1]][0])
  375. board[i-1][j+1][1].hideturtle()
  376. board[i-1][j+1][1] = 0
  377. board[i-2][j+2][1] = board[i][j][1]
  378. board[i][j][1] = 0
  379. temp1 = board[tar[0]][tar[1]][1]
  380. p1 = 0
  381. moved = True
  382.  
  383. try:
  384. if mbking[1] == 0:
  385. print('KING')
  386. turnKing(mbking,'black')
  387. except Exception as e :
  388. #print(e)
  389. pass
  390. if moved == True and moving == True:
  391. p1 = temp1
  392. counter+=1
  393. return(True)
  394. if moved == False:
  395. return(False)
  396.  
  397.  
  398. if moved == True and moving == False:
  399.  
  400. counter+=1
  401. if counter%2 == 0:
  402. print('white turn')
  403. else:
  404. print('black turn')
  405. return(False)
  406.  
  407.  
  408. '''
  409. if moved == True:
  410. print('move-true')
  411. p1 = temp1
  412. getpos1(p1.pos
  413. '''
  414. ###
  415. isempty(board)
  416.  
  417.  
  418.  
  419.  
  420.  
  421. def isempty(board):
  422. white = 0
  423. black = 0
  424. p = 0
  425. for i in range(8):
  426. for j in range(8):
  427.  
  428. try:
  429. p = board[i][j][1]
  430. if 'white' in p.shape():
  431. white += 1
  432. except: Exception
  433. try:
  434. p = board[i][j][1]
  435. if 'black' in p.shape():
  436. black += 1
  437. except: Exception
  438. if white == 0 and black != 0:
  439. print('black wins')
  440. if white != 0 and black == 0:
  441. print('white wins')
  442.  
  443.  
  444. def getpos(pawn):
  445. #print(pawn)
  446. i = int((pawn.pos()[0]+380)/90)
  447. j = int((pawn.pos()[1]+380)/90)
  448. return(i,j)
  449.  
  450. def getpos1(x,y):
  451. global p1,board,counter,wn,moving
  452. moving = False
  453.  
  454. i = int((x+380)/90)
  455. j = int((y+380)/90)
  456.  
  457. if counter%2 == 0:
  458. try:
  459. temp1 = board[i][j][1]
  460. if 'white' in temp1.shape():
  461. p1 = temp1
  462. wn.onclick(settar,1)
  463.  
  464. except Exception as e:
  465. print(e,'getpos11 error')
  466. pass
  467. if counter%2 == 1:
  468. try:
  469. temp1 = board[i][j][1]
  470. if 'black' in temp1.shape():
  471. p1 = temp1
  472. wn.onclick(settar,1)
  473. except Exception as e:
  474. print(e,'getpos22 error')
  475. pass
  476.  
  477. print('I = ',i,'J = ',j)
  478.  
  479.  
  480.  
  481. def settar(x,y):
  482. global tar
  483. i = int((x+380)/95)
  484. j = int((y+380)/95)
  485. tar = (i,j)
  486. #wn.onclick(settar,2)
  487. trytomove()
  488.  
  489. print(i,j,'tar')
  490. '''
  491. try:
  492. trytomove(x,y)
  493. except Exception :
  494. pass
  495. try:
  496. trytoeat()
  497. except Exception :
  498. pass
  499.  
  500.  
  501. '''
  502. def trytomove():
  503.  
  504. global tar,p1,counter
  505. print(p1)
  506.  
  507. try:
  508. if eat(tar):
  509. moved = True
  510. while(moved == True):
  511. moved = eat(tar)
  512. #p1 = 0
  513.  
  514.  
  515. except Exception as e :
  516. print(e,'tomov2')
  517. pass
  518. moved = False
  519. print(p1)
  520. if moved == False:
  521. if 'king' in p1.shape():
  522. try:
  523. moved = moveking(tar)
  524. except Exception as e :
  525. print(e,'tomov0')
  526. pass
  527.  
  528. if 'man' in p1.shape():
  529. try:
  530. moved = move(tar)
  531. except Exception as e :
  532. print(e,'tomov1')
  533. pass
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545. '''
  546. if white, check if i+2,j+2 is empty and i+1, j+1 is black
  547. " " " " i-2,j+2 is empty and i-1, j+1 is black
  548. if black, check if i-2,j-2 is black, i-1,j-
  549. delete
  550. '''
  551.  
  552.  
  553.  
  554. def legal(pawn):
  555.  
  556. global board
  557. i,j = getpos(pawn)
  558.  
  559. lst=[]
  560. #print(board[1][3])
  561. if 'white' in pawn.shape():
  562. if i!=0 and i!=7 and j!=7:
  563.  
  564. if board[i+1][j+1][1]==0:
  565. lst.append((i+1,j+1))
  566.  
  567. if board[i-1][j+1][1]==0:
  568. lst.append((i-1,j+1))
  569.  
  570. if i==0 and j!=7:
  571.  
  572. if board[i+1][j+1][1]==0:
  573. lst.append((i+1,j+1))
  574. if j!=7:
  575. if board[i-1][j+1][1]==0:
  576. lst.append((i-1,j+1))
  577. if j+1 == 7:
  578. lst.append('white')
  579.  
  580.  
  581.  
  582. if "black" in pawn.shape():
  583. if i!=0 and i!=7 and j!=0:
  584.  
  585. if board[i+1][j-1][1]==0:
  586. lst.append((i+1,j-1))
  587.  
  588. if board[i-1][j-1][1]==0:
  589. lst.append((i-1,j-1))
  590.  
  591. if i==0 and j!=0:
  592.  
  593. if board[i+1][j-1][1]==0:
  594. lst.append((i+1,j-1))
  595. if j!=0 :
  596.  
  597. if board[i-1][j-1][1]==0:
  598. lst.append((i-1,j-1))
  599. if j-1 == 0:
  600. lst.append('black')
  601. return lst
  602.  
  603. def kinglegal(king):
  604.  
  605. global board
  606. print('kinglegal')
  607. i,j = getpos(king)
  608.  
  609. lst=[]
  610. #print(board[1][3])
  611.  
  612. if i!=0 and i!=7 and j!=7:
  613.  
  614. if board[i+1][j+1][1]==0:
  615. lst.append((i+1,j+1))
  616.  
  617. if board[i-1][j+1][1]==0:
  618. lst.append((i-1,j+1))
  619.  
  620.  
  621. if i==0 and j!=7:
  622.  
  623. if board[i+1][j+1][1]==0:
  624. lst.append((i+1,j+1))
  625.  
  626. if j!=7:
  627.  
  628. if board[i-1][j+1][1]==0:
  629. lst.append((i-1,j+1))
  630.  
  631. if i!=0 and i!=7 and j!=0:
  632.  
  633. if board[i+1][j-1][1]==0:
  634. lst.append((i+1,j-1))
  635.  
  636. if board[i-1][j-1][1]==0:
  637. lst.append((i-1,j-1))
  638.  
  639. if i==0 and j!=0:
  640.  
  641. if board[i+1][j-1][1]==0:
  642. lst.append((i+1,j-1))
  643.  
  644. if j!=0 :
  645.  
  646. if board[i-1][j-1][1]==0:
  647. lst.append((i-1,j-1))
  648.  
  649. return lst
  650. #def onclick():
  651.  
  652. wn = turtle.Screen()
  653. wn.setworldcoordinates(-380,-380,380,380)
  654. wn.bgcolor('lightyellow')
  655. white_man = r"D:\Coding\Python\damka\imgs\white_man.gif"
  656. white_king = r"D:\Coding\Python\damka\imgs\white_king.gif"
  657. black_man = r"D:\Coding\Python\damka\imgs\black_man.gif"
  658. black_king = r"D:\Coding\Python\damka\imgs\black_king.gif"
  659. wn.register_shape(white_man)
  660. wn.register_shape(white_king)
  661. wn.register_shape(black_man)
  662. wn.register_shape(black_king)
  663. wn.onclick(settar,2)
  664. wn.onclick(trytomove,3)
  665. turtle.resizemode("user")
  666.  
  667. #wn.tracer(False)
  668. def start():
  669. global wn, white_man,black_man,board,p1,counter,black_king,white_king
  670. counter = 0
  671.  
  672. board=BoardMaker()
  673. board=SetCoordinates(board)
  674. #tiles = setboard()
  675. DrawBoard()
  676. board=SetPawns1(board)
  677. wn.listen()
  678. turtle.listen()
  679. #print(board[1][3])
  680.  
  681.  
  682.  
  683. start()
  684. turtle.done()
  685.  
  686.  
  687.  
  688.  
  689. def initiate():
  690. global wn, white_man,black_man,board,p1,tar
  691. start()
  692. try:
  693. move(p1,tar)
  694. except Exception:
  695. pass
  696. #wn.listen()
  697. #initiate()
  698. import sys
  699. import os
  700. def restart_program():
  701. """Restarts the current program.
  702. Note: this function does not return. Any cleanup action (like
  703. saving data) must be done before calling this function."""
  704. python = sys.executable
  705. os.execl(python, python, * sys.argv)
  706. if __name__ == "__main__":
  707. answer = raw_input("Do you want to restart this program ? ")
  708. if answer.lower().strip() in "y yes".split():
  709. restart_program()
  710. '''
  711.  
  712.  
  713. I = עמודות
  714. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement