Guest User

tic tac toe

a guest
Apr 8th, 2022
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.79 KB | None | 0 0
  1. import os
  2. import sys, time
  3. write = sys.stdout.write
  4. from types import NoneType
  5.  
  6. set_1 = 10
  7. set_2 = 10
  8. set_3 = 10
  9. set_4 = 10
  10. set_5 = 10
  11. set_6 = 10
  12. set_7 = 10
  13. set_8 = 10
  14. set_9 = 10
  15. set_1_set = 10
  16. set_2_set = 10
  17. set_3_set = 10
  18. set_4_set = 10
  19. set_5_set = 10
  20. set_6_set = 10
  21. set_7_set = 10
  22. set_8_set = 10
  23. set_9_set = 10
  24. set_1_win = 10
  25. set_2_win = 10
  26. set_3_win = 10
  27. set_4_win = 10
  28. set_5_win = 10
  29. set_6_win = 10
  30. set_7_win = 10
  31. set_8_win = 10
  32. set_9_win = 10
  33. play_again = 10
  34. win_cond = False
  35.  
  36.  
  37. # https://www.delftstack.com/howto/python/python-clear-console/
  38. def clearConsole():
  39. command = 'clear'
  40. if os.name in ('nt', 'dos'): # If Machine is running on Windows, use cls
  41. command = 'cls'
  42. os.system(command)
  43.  
  44. clearConsole()
  45.  
  46. def board(xo,key_pressed,win_cond):
  47. picture = [
  48. [0,0,0,'|',0,0,0,'|',0,0,0],
  49. [0,7,0,'|',0,8,0,'|',0,9,0],
  50. [0,0,0,'|',0,0,0,'|',0,0,0],
  51. ['-','-','-',0,'-','-','-',0,'-','-','-'],
  52. [0,0,0,'|',0,0,0,'|',0,0,0],
  53. [0,4,0,'|',0,5,0,'|',0,6,0],
  54. [0,0,0,'|',0,0,0,'|',0,0,0],
  55. ['-','-','-',0,'-','-','-',0,'-','-','-'],
  56. [0,0,0,'|',0,0,0,'|',0,0,0],
  57. [0,1,0,'|',0,2,0,'|',0,3,0],
  58. [0,0,0,'|',0,0,0,'|',0,0,0]
  59. ]
  60.  
  61. global set_1
  62. global set_2
  63. global set_3
  64. global set_4
  65. global set_5
  66. global set_6
  67. global set_7
  68. global set_8
  69. global set_9
  70. global set_1_set
  71. global set_2_set
  72. global set_3_set
  73. global set_4_set
  74. global set_5_set
  75. global set_6_set
  76. global set_7_set
  77. global set_8_set
  78. global set_9_set
  79. global set_1_win
  80. global set_2_win
  81. global set_3_win
  82. global set_4_win
  83. global set_5_win
  84. global set_6_win
  85. global set_7_win
  86. global set_8_win
  87. global set_9_win
  88.  
  89. for image in picture:
  90. for k in image:
  91. if k == '|':
  92. print('|', end='')
  93. continue
  94. if k == '-':
  95. print('-', end='')
  96. continue
  97. if k == int(key_pressed):
  98. # print(xo, end='')
  99. if int(key_pressed) == 1:
  100. set_1 = xo
  101. if int(key_pressed) == 2:
  102. set_2 = xo
  103. if int(key_pressed) == 3:
  104. set_3 = xo
  105. if int(key_pressed) == 4:
  106. set_4 = xo
  107. if int(key_pressed) == 5:
  108. set_5 = xo
  109. if int(key_pressed) == 6:
  110. set_6 = xo
  111. if int(key_pressed) == 7:
  112. set_7 = xo
  113. if int(key_pressed) == 8:
  114. set_8 = xo
  115. if int(key_pressed) == 9:
  116. set_9 = xo
  117. if k == 1 and (set_1 == 'X' or set_1 == 'O'):
  118. print(set_1, end='')
  119. set_1_set = 1
  120. if win_cond == True and set_1_win == 1:
  121. write('\b')
  122. print('\x1b[6;30;42m' + set_1 + '\x1b[0m', end='')
  123. continue
  124. if k == 2 and (set_2 == 'X' or set_2 == 'O'):
  125. print(set_2, end='')
  126. set_2_set = 1
  127. if win_cond == True and set_2_win == 1:
  128. write('\b')
  129. print('\x1b[6;30;42m' + set_2 + '\x1b[0m', end='')
  130. continue
  131. if k == 3 and (set_3 == 'X' or set_3 == 'O'):
  132. print(set_3, end='')
  133. set_3_set = 1
  134. if win_cond == True and set_3_win == 1:
  135. write('\b')
  136. print('\x1b[6;30;42m' + set_3 + '\x1b[0m', end='')
  137. continue
  138. if k == 4 and (set_4 == 'X' or set_4 == 'O'):
  139. print(set_4, end='')
  140. set_4_set = 1
  141. if win_cond == True and set_4_win == 1:
  142. write('\b')
  143. print('\x1b[6;30;42m' + set_4 + '\x1b[0m', end='')
  144. continue
  145. if k == 5 and (set_5 == 'X' or set_5 == 'O'):
  146. print(set_5, end='')
  147. set_5_set = 1
  148. if win_cond == True and set_5_win == 1:
  149. write('\b')
  150. print('\x1b[6;30;42m' + set_5 + '\x1b[0m', end='')
  151. continue
  152. if k == 6 and (set_6 == 'X' or set_6 == 'O'):
  153. print(set_6, end='')
  154. set_6_set = 1
  155. if win_cond == True and set_6_win == 1:
  156. write('\b')
  157. print('\x1b[6;30;42m' + set_6 + '\x1b[0m', end='')
  158. continue
  159. if k == 7 and (set_7 == 'X' or set_7 == 'O'):
  160. print(set_7, end='')
  161. set_7_set = 1
  162. if win_cond == True and set_7_win == 1:
  163. write('\b')
  164. print('\x1b[6;30;42m' + set_7 + '\x1b[0m', end='')
  165. continue
  166. if k == 8 and (set_8 == 'X' or set_8 == 'O'):
  167. print(set_8, end='')
  168. set_8_set = 1
  169. if win_cond == True and set_8_win == 1:
  170. write('\b')
  171. print('\x1b[6;30;42m' + set_8 + '\x1b[0m', end='')
  172. continue
  173. if k == 9 and (set_9 == 'X' or set_9 == 'O'):
  174. print(set_9, end='')
  175. set_9_set = 1
  176. if win_cond == True and set_9_win == 1:
  177. write('\b')
  178. print('\x1b[6;30;42m' + set_9 + '\x1b[0m', end='')
  179. continue
  180. else:
  181. print(' ', end='')
  182. print('')
  183.  
  184. return(set_1_set, set_2_set, set_3_set, set_4_set, set_5_set, set_6_set, set_7_set, set_8_set, set_9_set, set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
  185.  
  186. def key_press():
  187. key_pressed = 10
  188. global set_1_set
  189. global set_2_set
  190. global set_3_set
  191. global set_4_set
  192. global set_5_set
  193. global set_6_set
  194. global set_7_set
  195. global set_8_set
  196. global set_9_set
  197.  
  198. while int(key_pressed) not in (0,1,2,3,4,5,6,7,8,9):
  199. key_pressed = input("\nPlease enter a number from 1 and 9 or enter 0 to quit program: ")
  200. if key_pressed.isdigit():
  201. pass
  202. else:
  203. key_pressed = 10
  204.  
  205. if int(key_pressed) == 0:
  206. return key_pressed
  207.  
  208. if int(key_pressed) == 1 and set_1_set == 10:
  209. return key_pressed
  210.  
  211. if int(key_pressed) == 2 and set_2_set == 10:
  212. return key_pressed
  213.  
  214. if int(key_pressed) == 3 and set_3_set == 10:
  215. return key_pressed
  216.  
  217. if int(key_pressed) == 4 and set_4_set == 10:
  218. return key_pressed
  219.  
  220. if int(key_pressed) == 5 and set_5_set == 10:
  221. return key_pressed
  222.  
  223. if int(key_pressed) == 6 and set_6_set == 10:
  224. return key_pressed
  225.  
  226. if int(key_pressed) == 7 and set_7_set == 10:
  227. return key_pressed
  228.  
  229. if int(key_pressed) == 8 and set_8_set == 10:
  230. return key_pressed
  231.  
  232. if int(key_pressed) == 9 and set_9_set == 10:
  233. return key_pressed
  234.  
  235. def close(key_pressed):
  236. if int(key_pressed) == 0:
  237. print('0 enterd. Program has terminated')
  238. exit()
  239.  
  240. def win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9):
  241. global win_cond
  242. global set_1_win
  243. global set_2_win
  244. global set_3_win
  245. global set_4_win
  246. global set_5_win
  247. global set_6_win
  248. global set_7_win
  249. global set_8_win
  250. global set_9_win
  251.  
  252. if set_7 == set_8 == set_9 == 'X':
  253. print("The game has been won by X")
  254. win_cond = True
  255. set_7_win = 1
  256. set_8_win = 1
  257. set_9_win = 1
  258. board(10,10,win_cond)
  259. play_again = input("\nWould you like to play again. 0 to exit: ")
  260. if play_again.isdigit():
  261. if int(play_again) == 0:
  262. print('0 enterd. Program has terminated')
  263. exit()
  264. if set_4 == set_5 == set_6 == 'X':
  265. print("The game has been won by X")
  266. win_cond = True
  267. set_4_win = 1
  268. set_5_win = 1
  269. set_6_win = 1
  270. board(10,10,win_cond)
  271. play_again = input("\nWould you like to play again. 0 to exit: ")
  272. if play_again.isdigit():
  273. if int(play_again) == 0:
  274. print('0 enterd. Program has terminated')
  275. exit()
  276. if set_1 == set_2 == set_3 == 'X':
  277. print("The game has been won by X")
  278. win_cond = True
  279. set_1_win = 1
  280. set_2_win = 1
  281. set_3_win = 1
  282. board(10,10,win_cond)
  283. play_again = input("\nWould you like to play again. 0 to exit: ")
  284. if play_again.isdigit():
  285. if int(play_again) == 0:
  286. print('0 enterd. Program has terminated')
  287. exit()
  288. if set_1 == set_4 == set_7 == 'X':
  289. print("The game has been won by X")
  290. win_cond = True
  291. set_1_win = 1
  292. set_4_win = 1
  293. set_7_win = 1
  294. board(10,10,win_cond)
  295. play_again = input("\nWould you like to play again. 0 to exit: ")
  296. if play_again.isdigit():
  297. if int(play_again) == 0:
  298. print('0 enterd. Program has terminated')
  299. exit()
  300. if set_2 == set_5 == set_8 == 'X':
  301. print("The game has been won by X")
  302. win_cond = True
  303. set_2_win = 1
  304. set_5_win = 1
  305. set_8_win = 1
  306. board(10,10,win_cond)
  307. play_again = input("\nWould you like to play again. 0 to exit: ")
  308. if play_again.isdigit():
  309. if int(play_again) == 0:
  310. print('0 enterd. Program has terminated')
  311. exit()
  312. if set_3 == set_6 == set_9 == 'X':
  313. print("The game has been won by X")
  314. win_cond = True
  315. set_3_win = 1
  316. set_6_win = 1
  317. set_9_win = 1
  318. board(10,10,win_cond)
  319. play_again = input("\nWould you like to play again. 0 to exit: ")
  320. if play_again.isdigit():
  321. if int(play_again) == 0:
  322. print('0 enterd. Program has terminated')
  323. exit()
  324. if set_1 == set_5 == set_9 == 'X':
  325. print("The game has been won by X")
  326. win_cond = True
  327. set_1_win = 1
  328. set_5_win = 1
  329. set_9_win = 1
  330. board(10,10,win_cond)
  331. play_again = input("\nWould you like to play again. 0 to exit: ")
  332. if play_again.isdigit():
  333. if int(play_again) == 0:
  334. print('0 enterd. Program has terminated')
  335. exit()
  336. if set_7 == set_5 == set_3 == 'X':
  337. print("The game has been won by X")
  338. win_cond = True
  339. set_7_win = 1
  340. set_5_win = 1
  341. set_3_win = 1
  342. board(10,10,win_cond)
  343. play_again = input("\nWould you like to play again. 0 to exit: ")
  344. if play_again.isdigit():
  345. if int(play_again) == 0:
  346. print('0 enterd. Program has terminated')
  347. exit()
  348. if set_7 == set_8 == set_9 == 'O':
  349. print("The game has been won by O")
  350. win_cond = True
  351. set_7_win = 1
  352. set_8_win = 1
  353. set_9_win = 1
  354. board(10,10,win_cond)
  355. play_again = input("\nWould you like to play again. 0 to exit: ")
  356. if play_again.isdigit():
  357. if int(play_again) == 0:
  358. print('0 enterd. Program has terminated')
  359. exit()
  360. if set_4 == set_5 == set_6 == 'O':
  361. print("The game has been won by O")
  362. win_cond = True
  363. set_4_win = 1
  364. set_5_win = 1
  365. set_6_win = 1
  366. board(10,10,win_cond)
  367. play_again = input("\nWould you like to play again. 0 to exit: ")
  368. if play_again.isdigit():
  369. if int(play_again) == 0:
  370. print('0 enterd. Program has terminated')
  371. exit()
  372. if set_1 == set_2 == set_3 == 'O':
  373. print("The game has been won by O")
  374. win_cond = True
  375. set_1_win = 1
  376. set_2_win = 1
  377. set_3_win = 1
  378. board(10,10,win_cond)
  379. play_again = input("\nWould you like to play again. 0 to exit: ")
  380. if play_again.isdigit():
  381. if int(play_again) == 0:
  382. print('0 enterd. Program has terminated')
  383. exit()
  384. if set_1 == set_4 == set_7 == 'O':
  385. print("The game has been won by O")
  386. win_cond = True
  387. set_1_win = 1
  388. set_4_win = 1
  389. set_7_win = 1
  390. board(10,10,win_cond)
  391. play_again = input("\nWould you like to play again. 0 to exit: ")
  392. if play_again.isdigit():
  393. if int(play_again) == 0:
  394. print('0 enterd. Program has terminated')
  395. exit()
  396. if set_2 == set_5 == set_8 == 'O':
  397. print("The game has been won by O")
  398. win_cond = True
  399. set_2_win = 1
  400. set_5_win = 1
  401. set_8_win = 1
  402. board(10,10,win_cond)
  403. play_again = input("\nWould you like to play again. 0 to exit: ")
  404. if play_again.isdigit():
  405. if int(play_again) == 0:
  406. print('0 enterd. Program has terminated')
  407. exit()
  408. if set_3 == set_6 == set_9 == 'O':
  409. print("The game has been won by O")
  410. win_cond = True
  411. set_3_win = 1
  412. set_6_win = 1
  413. set_9_win = 1
  414. board(10,10,win_cond)
  415. play_again = input("\nWould you like to play again. 0 to exit: ")
  416. if play_again.isdigit():
  417. if int(play_again) == 0:
  418. print('0 enterd. Program has terminated')
  419. exit()
  420. if set_1 == set_5 == set_9 == 'O':
  421. print("The game has been won by O")
  422. win_cond = True
  423. set_1_win = 1
  424. set_5_win = 1
  425. set_9_win = 1
  426. board(10,10,win_cond)
  427. play_again = input("\nWould you like to play again. 0 to exit: ")
  428. if play_again.isdigit():
  429. if int(play_again) == 0:
  430. print('0 enterd. Program has terminated')
  431. exit()
  432. if set_7 == set_5 == set_3 == 'O':
  433. print("The game has been won by O")
  434. win_cond = True
  435. set_7_win = 1
  436. set_5_win = 1
  437. set_3_win = 1
  438. board(10,10,win_cond)
  439. play_again = input("\nWould you like to play again. 0 to exit: ")
  440. if play_again.isdigit():
  441. if int(play_again) == 0:
  442. print('0 enterd. Program has terminated')
  443. exit()
  444. return
  445.  
  446.  
  447. while play_again != 0:
  448.  
  449. clearConsole()
  450.  
  451. set_1 = 10
  452. set_2 = 10
  453. set_3 = 10
  454. set_4 = 10
  455. set_5 = 10
  456. set_6 = 10
  457. set_7 = 10
  458. set_8 = 10
  459. set_9 = 10
  460. set_1_set = 10
  461. set_2_set = 10
  462. set_3_set = 10
  463. set_4_set = 10
  464. set_5_set = 10
  465. set_6_set = 10
  466. set_7_set = 10
  467. set_8_set = 10
  468. set_9_set = 10
  469. set_1_win = 10
  470. set_2_win = 10
  471. set_3_win = 10
  472. set_4_win = 10
  473. set_5_win = 10
  474. set_6_win = 10
  475. set_7_win = 10
  476. set_8_win = 10
  477. set_9_win = 10
  478. play_again = 10
  479. win_cond = False
  480.  
  481. board(10,10,win_cond)
  482.  
  483. key_pressed = key_press()
  484. close(key_pressed)
  485. clearConsole()
  486. board('X', key_pressed, win_cond)
  487.  
  488. key_pressed = key_press()
  489. while type(key_pressed) == NoneType:
  490. key_pressed = key_press()
  491. close(key_pressed)
  492. clearConsole()
  493. board('O', key_pressed, win_cond)
  494.  
  495. key_pressed = key_press()
  496. while type(key_pressed) == NoneType:
  497. key_pressed = key_press()
  498. close(key_pressed)
  499. clearConsole()
  500. board('X', key_pressed, win_cond)
  501.  
  502. key_pressed = key_press()
  503. while type(key_pressed) == NoneType:
  504. key_pressed = key_press()
  505. close(key_pressed)
  506. clearConsole()
  507. board('O', key_pressed, win_cond)
  508.  
  509. key_pressed = key_press()
  510. while type(key_pressed) == NoneType:
  511. key_pressed = key_press()
  512. close(key_pressed)
  513. clearConsole()
  514. board('X', key_pressed, win_cond)
  515. win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
  516. if win_cond == True:
  517. board('X', key_pressed, win_cond)
  518.  
  519. if win_cond != True:
  520. key_pressed = key_press()
  521. while type(key_pressed) == NoneType:
  522. key_pressed = key_press()
  523. close(key_pressed)
  524. clearConsole()
  525. board('O', key_pressed, win_cond)
  526. win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
  527. if win_cond == True:
  528. board('O', key_pressed, win_cond)
  529.  
  530.  
  531. if win_cond != True:
  532. key_pressed = key_press()
  533. while type(key_pressed) == NoneType:
  534. key_pressed = key_press()
  535. close(key_pressed)
  536. clearConsole()
  537. board('X', key_pressed, win_cond)
  538. win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
  539. if win_cond == True:
  540. board('X', key_pressed, win_cond)
  541.  
  542. if win_cond != True:
  543. key_pressed = key_press()
  544. while type(key_pressed) == NoneType:
  545. key_pressed = key_press()
  546. close(key_pressed)
  547. clearConsole()
  548. board('O', key_pressed, win_cond)
  549. win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
  550. if win_cond == True:
  551. board('X', key_pressed, win_cond)
  552.  
  553. if win_cond != True:
  554. key_pressed = key_press()
  555. while type(key_pressed) == NoneType:
  556. key_pressed = key_press()
  557. close(key_pressed)
  558. clearConsole()
  559. board('X', key_pressed, win_cond)
  560. win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
  561. if win_cond == True:
  562. board('X', key_pressed, win_cond)
  563.  
  564. if win_cond == False:
  565. print('There is no winner.')
  566. play_again = input("\nWould you like to play again. 0 to exit: ")
  567. if play_again.isdigit():
  568. if int(play_again) == 0:
  569. print('\n0 enterd. Program has terminated')
  570. exit()
  571. else:
  572. pass
  573.  
  574.  
  575.  
  576.  
  577.  
Advertisement
Add Comment
Please, Sign In to add comment