Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.20 KB | None | 0 0
  1. # utilsQuixo.py
  2. # IA library
  3. # Author: Soffie Quentin - Thibaut Maringer
  4. # Version: February 20, 2019
  5.  
  6. def startIA(listebtn):
  7. simplelist = convertToSimpleList(listebtn)
  8.  
  9. listWeight = getTheBestWeight(simplelist,True,True) # liste de liste de tuple = [[(valueWeight,horizontale,droitebas,cell),...]]
  10.  
  11.  
  12.  
  13. # mode anticipation du tour suivant : on check le poid qu 'on obtient dans le prochain coups
  14. listWeightTurnTwo = []
  15. if len(listWeight) > 0:
  16. for i in range(len(listWeight)):
  17. for y in range(len(listWeight[i])): # on parcourt toutes les possibilités pour obtenir le meilleur poids a notre tour
  18. if listWeight[i][y][0] == 1000000000:
  19. return listWeight[i][y]
  20. if listWeight[i][y][0] != -1000000000:
  21. myliste = getFutureBlow(listWeight[i][y][1],listWeight[i][y][2],listWeight[i][y][3],simplelist,True)
  22. ennemy = getTheBestWeight(myliste,False)
  23. mynewliste = getFutureBlow(ennemy[1],ennemy[2],ennemy[3],myliste,False)
  24. listWeightTurnTwo.append(getTheBestWeight(mynewliste,True))
  25. if len(listWeightTurnTwo) > 0:
  26. BestWeight2 = listWeightTurnTwo[0]
  27. for i in listWeightTurnTwo:
  28. if BestWeight2[0] < i[0]:
  29. BestWeight2 = i
  30.  
  31. return BestWeight2 # meilleur lancé possible
  32.  
  33.  
  34. def getTheBestWeight(simplelist,round,returnlist = False):
  35. cellReachable = getReachableCells(simplelist)
  36. listWeight = [] # liste de tuple = [(valueWeight,horizontale,droitebas,cell)]
  37. for i in cellReachable:
  38. listWeight.append(evaluateAllAction(i,simplelist,round,returnlist))
  39.  
  40. if returnlist == True:
  41. return listWeight
  42.  
  43. #mode 1 analyse du jeu
  44. if len(listWeight) > 0:
  45. BestWeight = listWeight[0]
  46. for i in listWeight:
  47. if BestWeight[0] < i[0]:
  48. BestWeight = i
  49.  
  50. return BestWeight # meilleur lancé possible
  51.  
  52.  
  53.  
  54.  
  55. def evaluateAllAction(cell,mylist,round,returnlist = False):
  56. droitebas = False
  57. horizontale = False
  58. listWeight = [] # list of tuple
  59. if int(cell) == 0:
  60. listWeight.append((generateWeightSpell(cell,getFutureBlow(True,True,cell,mylist,round),round),True,True,cell))
  61. listWeight.append((generateWeightSpell(cell,getFutureBlow(False,True,cell,mylist,round),round),False,True,cell))
  62. elif int(cell) == 20:
  63. listWeight.append((generateWeightSpell(cell,getFutureBlow(True,True,cell,mylist,round),round),True,True,cell))
  64. listWeight.append((generateWeightSpell(cell,getFutureBlow(False,False,cell,mylist,round),round),False,False,cell))
  65. elif int(cell) == 4:
  66. listWeight.append((generateWeightSpell(cell,getFutureBlow(True,False,cell,mylist,round),round),True,False,cell))
  67. listWeight.append((generateWeightSpell(cell,getFutureBlow(False,True,cell,mylist,round),round),False,True,cell))
  68. elif int(cell) == 24:
  69. listWeight.append((generateWeightSpell(cell,getFutureBlow(False,False,cell,mylist,round),round),False,False,cell))
  70. listWeight.append((generateWeightSpell(cell,getFutureBlow(True,False,cell,mylist,round),round),True,False,cell))
  71. else:
  72. listWeight.append((generateWeightSpell(cell,getFutureBlow(False,False,cell,mylist,round),round),False,False,cell))
  73. listWeight.append((generateWeightSpell(cell,getFutureBlow(True,False,cell,mylist,round),round),True,False,cell))
  74. listWeight.append((generateWeightSpell(cell,getFutureBlow(False,True,cell,mylist,round),round),False,True,cell))
  75. listWeight.append((generateWeightSpell(cell,getFutureBlow(True,True,cell,mylist,round),round),True,True,cell))
  76. if returnlist == True:
  77. return listWeight
  78.  
  79.  
  80. if len(listWeight) > 0:
  81. BestWeight = listWeight[0]
  82. for i in listWeight:
  83. if BestWeight[0] < i[0]:
  84. BestWeight = i
  85. return BestWeight
  86.  
  87.  
  88.  
  89.  
  90. def generateWeightSpell(cell,listebtn,round):
  91. agressif = 0
  92. defensif = 0
  93. if round == True:
  94. agressif = generateWeightMyself(listebtn)
  95. defensif = generateWeightEnnemy(listebtn) - 10
  96. else:
  97. agressif = generateWeightEnnemy(listebtn)
  98. defensif = generateWeightMyself(listebtn) - 10
  99.  
  100. if agressif == 1000000000:
  101. print("Winner")
  102. return agressif # si la possibilité de gagner, on gagne la partie
  103. if defensif == 1000000000:
  104. return -1000000000 # si le coups est perdant, on annule le coups
  105. ratio = agressif - defensif
  106. return ratio
  107.  
  108.  
  109.  
  110.  
  111.  
  112. def generateWeightMyself(mylist):
  113. #Calcul du nombre de rond aligné on possede
  114. weight = 0
  115.  
  116. indexes = getMyselfCells(mylist)
  117. ## Horizontalement
  118. for t in range(5):
  119. num = 0
  120. for i in range((t * 5) ,(5 * t) + 5):
  121. if i in indexes:
  122. num += 1
  123. for i in range(num):
  124. weight += (i + 1)**2
  125. if num == 5:
  126. return 1000000000
  127.  
  128. ## Verticalement
  129. for t in range(5):
  130. num = 0
  131. for i in range(t,t+21,5):
  132. if i in indexes:
  133. num += 1
  134. for i in range(num):
  135. weight += (i + 1)**2
  136. if num == 5:
  137. return 1000000000
  138.  
  139. ## Diagonale 0,0 -> 4,4
  140.  
  141. num = 0
  142. for i in range(5):
  143. if convertMatrix(i,i) in indexes:
  144. num += 1
  145. for i in range(num):
  146. weight += (i + 1)**2
  147. if num == 5:
  148. return 1000000000
  149.  
  150. ## Diagonale 4,0 -> 0,4
  151.  
  152. num = 0
  153. for i in range(5):
  154. if convertMatrix(4-i,i) in indexes:
  155. num += 1
  156. for i in range(num):
  157. weight += (i + 1)**2
  158. if num == 5:
  159. return 1000000000
  160. return weight
  161.  
  162. def generateWeightEnnemy(mylist):
  163. #Calcul du nombre de rond aligné on possede
  164. weight = 0
  165.  
  166. indexes = getEnemyCells(mylist)
  167. ## Horizontalement
  168. for t in range(5):
  169. num = 0
  170. for i in range((t * 5) ,(5 * t) + 5):
  171. if i in indexes:
  172. num += 1
  173. for i in range(num):
  174. weight += (i + 1)**2
  175. if num == 5:
  176. return 1000000000
  177.  
  178. ## Verticalement
  179. for t in range(5):
  180. num = 0
  181. for i in range(t,t+21,5):
  182. if i in indexes:
  183. num += 1
  184. for i in range(num):
  185. weight += (i + 1)**2
  186. if num == 5:
  187. return 1000000000
  188.  
  189. ## Diagonale 0,0 -> 4,4
  190.  
  191. num = 0
  192. for i in range(5):
  193. if convertMatrix(i,i) in indexes:
  194. num += 1
  195. for i in range(num):
  196. weight += (i + 1)**2
  197. if num == 5:
  198. return 1000000000
  199.  
  200. ## Diagonale 4,0 -> 0,4
  201.  
  202. num = 0
  203. for i in range(5):
  204. if convertMatrix(4-i,i) in indexes:
  205. num += 1
  206. for i in range(num):
  207. weight += (i + 1)**2
  208. if num == 5:
  209. return 1000000000
  210. return weight
  211.  
  212.  
  213.  
  214.  
  215. def convertToSimpleList(listbtn):
  216. mines = []
  217. for i in range(len(listbtn)):
  218. mines.append(listbtn[i].background_normal)
  219. return mines
  220.  
  221. def getFutureBlow(horizontale,droitebas,index,mylist,round):
  222. mines = [] # list
  223. mylist2 = [] # list saveCode
  224. for i in range(len(mylist)):
  225. mines.append(mylist[i])
  226. mylist2.append(mylist[i])
  227.  
  228.  
  229. #### Gestion du haut et du bas horizontalement
  230. if horizontale == True and droitebas == False:
  231. if int(index) > -1 and int(index) < 5:
  232. for i in range(int(index)):
  233. addpictureotherbtn(i+1,i,mylist2,mines)
  234. Addpicture(mines,0,round)
  235. elif int(index) > 19 and int(index) < 25:
  236. for i in range(20,int(index)):
  237. addpictureotherbtn(i+1,i,mylist2,mines)
  238. Addpicture(mines,20,round)
  239. elif horizontale == True and droitebas == True:
  240. if int(index) > -1 and int(index) < 5:
  241. for i in range(4,int(index),-1):
  242. addpictureotherbtn(i-1,i,mylist2,mines)
  243. Addpicture(mines,4,round)
  244. elif int(index) > 19 and int(index) < 25:
  245. for i in range(24,int(index),-1):
  246. addpictureotherbtn(i-1,i,mylist2,mines)
  247. Addpicture(mines,24,round)
  248. #### Gestion du haut et du bas verticalement
  249.  
  250. if horizontale == False:
  251. if int(index) > -1 and int(index) < 5:
  252. for i in range(int(index) + 20,int(index),-5):
  253. addpictureotherbtn(i-5,i,mylist2,mines)
  254. Addpicture(mines,int(index) + 20,round)
  255. elif int(index) > 19 and int(index) < 25:
  256. for i in range(int(index)-20 ,int(index) ,5):
  257. addpictureotherbtn(i+5,i,mylist2,mines)
  258. Addpicture(mines,int(index) - 20,round)
  259. ##### Gestion du droite et gauche horizontalement
  260. if horizontale == True:
  261. if int(index) == 5 or int(index) == 10 or int(index) == 15:
  262. for i in range(int(index) + 4,int(index),-1):
  263. addpictureotherbtn(i-1,i,mylist2,mines)
  264. Addpicture(mines,int(index)+4,round)
  265. elif int(index) == 9 or int(index) == 14 or int(index) == 19:
  266. for i in range(int(index)-4,int(index)):
  267. addpictureotherbtn(i+1,i,mylist2,mines)
  268. Addpicture(mines,int(index)-4,round)
  269. ##### Gestion du droite et gauche verticalement
  270.  
  271. if horizontale == False and droitebas == False:
  272. if int(index) == 5 or int(index) == 10 or int(index) == 15:
  273. for i in range(0 ,int(index) ,5):
  274. addpictureotherbtn(i+5,i,mylist2,mines)
  275. Addpicture(mines,0,round)
  276. elif int(index) == 9 or int(index) == 14 or int(index) == 19:
  277. for i in range(4 ,int(index) ,5):
  278. addpictureotherbtn(i+5,i,mylist2,mines)
  279. Addpicture(mines,4,round)
  280. elif horizontale == False and droitebas == True:
  281. if int(index) == 5 or int(index) == 10 or int(index) == 15:
  282. for i in range(20,int(index),-5):
  283. addpictureotherbtn(i-5,i,mylist2,mines)
  284. Addpicture(mines,20,round)
  285. elif int(index) == 9 or int(index) == 14 or int(index) == 19:
  286. for i in range(24,int(index),-5):
  287. addpictureotherbtn(i-5,i,mylist2,mines)
  288. Addpicture(mines,24,round)
  289. return mines
  290.  
  291. def Addpicture(instance,index,round):
  292. if round == True:
  293. instance[index] = ".\casevide2.png"
  294. else:
  295. instance[index] = ".\casevide.png"
  296.  
  297. def addpictureotherbtn(index1,index2,savecode,listref):
  298. listref[index1] = savecode[index2]
  299. listref[index1] = savecode[index2]
  300.  
  301. def getEnemyCells(listebtn):
  302. # return list of index Enemy Cells
  303. mylist = []
  304. for i in range(len(listebtn)):
  305. if checkIfValueX(listebtn[i]) == True:
  306. mylist.append(i)
  307. return mylist
  308.  
  309. def getMyselfCells(listebtn):
  310. # return list of index Myself Cells
  311. mylist = []
  312. for i in range(len(listebtn)):
  313. if checkIfValueO(listebtn[i]) == True:
  314. mylist.append(i)
  315. return mylist
  316.  
  317. def getReachableCells(listebtn):
  318. # return list of index Reachable Cells
  319. mylist = []
  320. for i in range(len(listebtn)):
  321. if canLaunchSpell(listebtn[i],i) == True:
  322. mylist.append(i)
  323. return mylist
  324.  
  325. def canLaunchSpell(btn,index):
  326. if checkIfValueNothing(btn) == True:
  327. for i in range(5):
  328. if convertMatrix(0,i) == index:
  329. return True
  330. for i in range(5):
  331. if convertMatrix(i,0) == index:
  332. return True
  333. for i in range(5):
  334. if convertMatrix(4,i) == index:
  335. return True
  336. for i in range(5):
  337. if convertMatrix(i,4) == index:
  338. return True
  339. return False
  340.  
  341. def convertMatrix(columns,rows):
  342. return columns + rows * 5
  343.  
  344.  
  345.  
  346. def checkIfValueX(btn):
  347. if btn == ".\casevide.png":
  348. return True
  349. return False
  350.  
  351. def checkIfValueO(btn):
  352. if btn == ".\casevide2.png":
  353. return True
  354. return False
  355.  
  356. def checkIfValueNothing(btn):
  357. if btn == ".\case2.png":
  358. return True
  359. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement