fork(1) download
  1. from PyQt5.QtCore import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtWidgets import *
  4. from random import randint, choice
  5.  
  6. class Toe(QWidget):
  7.  
  8. def __init__ (self):
  9.  
  10. self.turn = 0
  11. self.first_cross = ''
  12. self.grid = [0,1,2,3,4,5,6,7,8]
  13. self.status = ['e','e','e','e','e','e','e','e','e']
  14. self.lines = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[6,4,2]]
  15. self.finish = 0
  16. self.buttons = []
  17. QWidget. __init__ (self)
  18. self.resize(360, 435)
  19. self.setMaximumSize(360,435)
  20. self.setMinimumSize(360,435)
  21. self.one = QPushButton(self)
  22. self.one.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.one, 0))
  23. self.one.setGeometry(QRect(40,70,70,60))
  24. self.one.setText('...')
  25. self.buttons.append(self.one)
  26. self.two = QPushButton(self)
  27. self.two.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.two, 1))
  28. self.two.setGeometry(QRect(140,70,70,60))
  29. self.two.setText('...')
  30. self.buttons.append(self.two)
  31. self.three = QPushButton(self)
  32. self.three.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.three, 2))
  33. self.three.setGeometry(QRect(240,70,70,60))
  34. self.three.setText('...')
  35. self.buttons.append(self.three)
  36. self.four = QPushButton(self)
  37. self.four.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.four, 3))
  38. self.four.setGeometry(QRect(40,155,70,60))
  39. self.four.setText('...')
  40. self.buttons.append(self.four)
  41. self.five = QPushButton(self)
  42. self.five.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.five, 4))
  43. self.five.setGeometry(QRect(140,155,70,60))
  44. self.five.setText('...')
  45. self.buttons.append(self.five)
  46. self.six = QPushButton(self)
  47. self.six.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.six, 5))
  48. self.six.setGeometry(QRect(240,155,70,60))
  49. self.six.setText('...')
  50. self.buttons.append(self.six)
  51. self.seven = QPushButton(self)
  52. self.seven.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.seven, 6))
  53. self.seven.setGeometry(QRect(40,240,70,60))
  54. self.seven.setText('...')
  55. self.buttons.append(self.seven)
  56. self.eight = QPushButton(self)
  57. self.eight.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.eight, 7))
  58. self.eight.setGeometry(QRect(140,240,70,60))
  59. self.eight.setText('...')
  60. self.buttons.append(self.eight)
  61. self.nine = QPushButton(self)
  62. self.nine.clicked.connect(lambda event= '<Button-1>' : self.set_it(self.nine, 8))
  63. self.nine.setGeometry(QRect(240,240,70,60))
  64. self.nine.setText('...')
  65. self.buttons.append(self.nine)
  66. self.result = QLabel('', self)
  67. self.result.setGeometry(QRect(125,360,111,18))
  68. self.crossornull = QCheckBox(self)
  69. self.crossornull.setGeometry(QRect(335, 25, 16, 16))
  70. self.startgame = QPushButton(self)
  71. self.startgame.setGeometry(QRect(175,20,80,30))
  72. self.startgame.setText("Старт!")
  73. self.startgame.clicked.connect(self.start)
  74. self.fornull = QLabel("За 'нолик':", self)
  75. self.fornull.setGeometry(QRect(270,25, 55,12))
  76. self.setWindowTitle("Tic-tac-toe")
  77. self.mylevel = QComboBox(self)
  78. self.mylevel.addItems(['Easy', 'Normal', 'Dead end'])
  79. self.mylevel.setGeometry(QRect(88,20,70,30))
  80. self.leveltext = QLabel('Сложность:', self)
  81. self.leveltext.setGeometry(10,30,58,15)
  82.  
  83. self.show()
  84.  
  85. def set_it(self, button, n):
  86. if self.mylevel.currentText() == 'Easy':
  87. if not self.crossornull.isChecked():
  88. button.setText('X')
  89. button.setDisabled(True)
  90. self.grid.remove(n)
  91. self.status[n] = 'X'
  92. if not self.check():
  93. self.easy_ai('O')
  94. else:
  95. button.setText('O')
  96. button.setDisabled(True)
  97. self.grid.remove(n)
  98. self.status[n] = 'O'
  99. if not self.check():
  100. self.easy_ai('X')
  101. elif self.mylevel.currentText() == 'Normal':
  102. if not self.crossornull.isChecked():
  103. button.setText('X')
  104. button.setDisabled(True)
  105. self.grid.remove(n)
  106. self.status[n] = 'X'
  107. if not self.check():
  108. self.normal_ai('O')
  109. else:
  110. button.setText('O')
  111. button.setDisabled(True)
  112. self.grid.remove(n)
  113. self.status[n] = 'O'
  114. if not self.check():
  115. self.normal_ai('X')
  116. elif self.mylevel.currentText() == 'Dead end':
  117. if not self.crossornull.isChecked():
  118. button.setText('X')
  119. button.setDisabled(True)
  120. self.grid.remove(n)
  121. self.status[n] = 'X'
  122. if not self.check():
  123. self.deadend_ai('O')
  124. else:
  125. button.setText('O')
  126. button.setDisabled(True)
  127. self.grid.remove(n)
  128. self.status[n] = 'O'
  129. if not self.check():
  130. self.deadend_ai('X')
  131.  
  132. def start(self):
  133. self.turn = 0
  134. self.finish = 0
  135. self.grid = [0,1,2,3,4,5,6,7,8]
  136. self.status = ['e','e','e','e','e','e','e','e','e']
  137. for elem in self.buttons:
  138. elem.setText('...')
  139. elem.setDisabled(False)
  140. self.result.setText('')
  141. if self.crossornull.isChecked():
  142. if self.mylevel.currentText() == 'Easy':
  143. self.easy_ai('X')
  144. elif self.mylevel.currentText() == 'Normal':
  145. self.normal_ai('X')
  146. elif self.mylevel.currentText() == 'Dead end':
  147. self.deadend_ai('X')
  148.  
  149. def easy_ai (self, sign, choose=''):
  150. if len(self.grid) > 0:
  151. if choose == '':
  152. choose = choice(self.grid)
  153. self.grid.remove(choose)
  154. if choose == 0:
  155. self.one.setText(sign)
  156. self.one.setDisabled(True)
  157. elif choose == 1:
  158. self.two.setText(sign)
  159. self.two.setDisabled(True)
  160. elif choose == 2:
  161. self.three.setText(sign)
  162. self.three.setDisabled(True)
  163. elif choose == 3:
  164. self.four.setText(sign)
  165. self.four.setDisabled(True)
  166. elif choose == 4:
  167. self.five.setText(sign)
  168. self.five.setDisabled(True)
  169. elif choose == 5:
  170. self.six.setText(sign)
  171. self.six.setDisabled(True)
  172. elif choose == 6:
  173. self.seven.setText(sign)
  174. self.seven.setDisabled(True)
  175. elif choose == 7:
  176. self.eight.setText(sign)
  177. self.eight.setDisabled(True)
  178. else:
  179. self.nine.setText(sign)
  180. self.nine.setDisabled(True)
  181. self.status[choose] = sign
  182. self.check()
  183.  
  184. def normal_ai(self, sign, hlp=0):
  185. test = 0
  186. p = 0 #~!!!!
  187. if sign == 'O':
  188. enemy = 'X'
  189. else: enemy = 'O'
  190. if len(self.grid) >= 8:
  191. self.easy_ai(sign)
  192. else:
  193. for elem in self.lines:
  194. for i in elem:
  195. if self.status[i] == sign:
  196. test+=1
  197. if test > 1 and any(self.status[k] == 'e' for k in elem):
  198. self.easy_ai(sign, int(list(filter(lambda j : self.status[j] == 'e', elem))[0]))
  199. p+=1
  200. break
  201. if test > 1 and p > 0:
  202. break
  203. test = 0
  204.  
  205. if p == 0:
  206. test = 0
  207. for elem in self.lines:
  208. for i in elem:
  209. if self.status[i] == enemy:
  210. test+=1
  211. if test > 1 and any(self.status[k] == 'e' for k in elem):
  212. self.easy_ai(sign, int(list(filter(lambda j : self.status[j] == 'e', elem))[0]))
  213. p+=1
  214. break
  215. if test > 1 and p > 0:
  216. break
  217. test = 0
  218. if test <= 1 and p == 0:
  219. if hlp == 0:
  220. self.easy_ai(sign)
  221. else:
  222. self.deadend_ai(sign, iam=0)
  223.  
  224. def deadend_ai(self, sign, iam=1):
  225. arr = [0,2,6,8]
  226. lin = [1,5,7,3]
  227. if sign == 'X':
  228. if self.turn == 0:
  229. self.easy_ai(sign, 4)
  230. self.turn+=1
  231. elif self.turn == 1:
  232. self.turn+=1
  233. if self.status[0] == 'O':
  234. self.easy_ai(sign, 8)
  235. elif self.status[1] == 'O':
  236. self.easy_ai(sign, 6)
  237. elif self.status[2] == 'O':
  238. self.easy_ai(sign, 6)
  239. elif self.status[3] == 'O':
  240. self.easy_ai(sign, 2)
  241. elif self.status[5] == 'O':
  242. self.easy_ai(sign, 6)
  243. elif self.status[6] == 'O':
  244. self.easy_ai(sign, 2)
  245. elif self.status[7] == 'O':
  246. self.easy_ai(sign, 0)
  247. else:
  248. self.easy_ai(sign, 0)
  249. elif self.turn > 1:
  250. self.normal_ai(sign)
  251. elif sign == 'O':
  252. if self.turn == 0:
  253. self.turn+=1
  254. if self.status[4] == 'X':
  255. n = choice(arr)
  256. self.easy_ai(sign, n)
  257. self.first_cross = 'c'
  258. elif self.status[4] != 'X' and (self.status[0] == 'X' or self.status[2] == 'X' or self.status[6] == 'X' or self.status[8] == 'X'):
  259. self.easy_ai(sign, 4)
  260. self.first_cross = 'co'
  261. else:
  262. self.first_cross = 'line'
  263. self.easy_ai(sign, 4)
  264.  
  265. elif self.turn > 0 and self.first_cross == 'c':
  266. if iam==1:
  267. self.normal_ai(sign, hlp=1)
  268. else:
  269. try:
  270. n = choice([i for i in arr if i in self.grid])
  271. self.easy_ai(sign, n)
  272. except:
  273. self.easy_ai(sign)
  274. elif self.turn > 0 and self.first_cross == 'co':
  275. if self.turn == 1:
  276. if iam == 1:
  277. self.normal_ai(sign, hlp=1)
  278. else:
  279. self.turn+=1
  280. try:
  281. if self.status[0] == 'X':
  282. self.easy_ai(sign, 8)
  283. elif self.status[2] == 'X':
  284. self.easy_ai(sign, 6)
  285. elif self.status[6] == 'X':
  286. self.easy_ai(sign, 2)
  287. elif self.status[8] == 'X':
  288. self.easy_ai(sign, 0)
  289. except:
  290. n = choice([i for i in lin if i in self.grid])
  291. self.easy_ai(sign, n)
  292. else:
  293. self.normal_ai(sign)
  294.  
  295. elif self.turn > 0 and self.first_cross == 'line':
  296. if self.turn == 1:
  297. if iam == 1:
  298. self.normal_ai(sign, hlp=1)
  299.  
  300. else:
  301. if self.status[0] == 'X' or self.status[2] == 'X' or self.status[6] == 'X' or self.status[8] == 'X':
  302. print('!222@')
  303. try:
  304. if self.status[0] == 'X':
  305. self.easy_ai(sign, 8)
  306. elif self.status[2] == 'X':
  307. self.easy_ai(sign, 6)
  308. elif self.status[6] == 'X':
  309. self.easy_ai(sign, 2)
  310. elif self.status[8] == 'X':
  311. self.easy_ai(sign, 0)
  312. self.turn+=1
  313. except:
  314. self.normal_ai(sign)
  315. elif (self.status[1] == 'X' and self.status[7] == 'X') or (self.status[3] == 'X' and self.status[5] == 'X'):
  316. n = choice(arr)
  317. self.easy_ai(sign, n)
  318. self.turn+=1
  319. elif ((self.status[3] == 'X' and self.status[1] == 'X') or (self.status[1] == 'X' and self.status[5] == 'X') or
  320. (self.status[5] == 'X' and self.status[7] == 'X') or (self.status[7] == 'X' and self.status[3] == 'X')):
  321. if (self.status[3] == 'X' and self.status[1] == 'X'):
  322. self.easy_ai(sign, 0)
  323. elif (self.status[1] == 'X' and self.status[5] == 'X'):
  324. self.easy_ai(sign, 2)
  325. elif (self.status[5] == 'X' and self.status[7] == 'X'):
  326. self.easy_ai(sign, 8)
  327. elif (self.status[7] == 'X' and self.status[3] == 'X'):
  328. self.easy_ai(sign, 6)
  329. self.turn+=1
  330. elif self.turn > 1:
  331. self.normal_ai(sign)
  332.  
  333. def check(self):
  334. for elem in self.lines:
  335. if all(self.status[i] == 'X' for i in elem) and not self.crossornull.isChecked():
  336. self.result.setText("<html><head/><body><p><span style=\" font-size:12pt; color:green;\">Вы выиграли!</span></p></body></html>")
  337. self.finish += 1
  338. if all(self.status[i] == 'X' for i in elem) and self.crossornull.isChecked():
  339. self.result.setText("<html><head/><body><p><span style=\" font-size:12pt; color:red;\">Вы проиграли.</span></p></body></html>")
  340. self.finish+=1
  341. if all(self.status[i] == 'O' for i in elem) and self.crossornull.isChecked():
  342. self.result.setText("<html><head/><body><p><span style=\" font-size:12pt; color:green;\">Вы выиграли!</span></p></body></html>")
  343. self.finish+=1
  344. if all(self.status[i] == 'O' for i in elem) and not self.crossornull.isChecked():
  345. self.result.setText("<html><head/><body><p><span style=\" font-size:12pt; color:red;\">Вы проиграли.</span></p></body></html>")
  346. self.finish+=1
  347. if len(self.grid) < 1 and self.finish == 0:
  348. self.result.setText("<html><head/><body><p><span style=\" font-size:12pt; color:grey;\"><center>Ничья.</center></span></p></body></html>")
  349. if self.finish > 0:
  350. return True
  351.  
  352. if __name__ == "__main__":
  353. import sys
  354. app = QApplication(sys.argv)
  355. window = Toe()
  356. sys.exit(app.exec_())
Runtime error #stdin #stdout #stderr 0.02s 8696KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ImportError: No module named 'PyQt5'