Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. # coded by: salism3
  2. # 26 - 02 - 2020
  3.  
  4. class Tictactoe:
  5. def __init__(self):
  6. self.col = list(range(1,10))
  7. self.p1 = None
  8. self.p2 = None
  9. self.__turn = 0
  10.  
  11. def show(self):
  12. for_format = str(self.col).replace("[", "").replace("]", "")
  13. data = box="""\x1b[1;33m ╔═══╦═══╦═══╗
  14. ║ {} \x1b[1;33m║ {} \x1b[1;33m║ {} \x1b[1;33m║
  15. ╠═══╣═══╣═══╣
  16. ║ {} \x1b[1;33m║ {} \x1b[1;33m║ {} \x1b[1;33m║
  17. ╠═══╣═══╣═══╣
  18. ║ {} \x1b[1;33m║ {} \x1b[1;33m║ {} \x1b[1;33m║
  19. ╚═══╩═══╩═══╝\x1b[1;39m"""
  20. data = eval("data.format({})".format(for_format))
  21. print(data)
  22.  
  23. def p1_select(self, x):
  24. if self.col[x - 1] != "X" and self.col[x - 1] != "O" and x > 0 and x < 10:
  25. self.__turn += 1
  26. self.col[x - 1] = "O"
  27.  
  28. def p2_select(self, x):
  29. if self.col[x - 1] != "O" and self.col[x - 1] != "X" and x > 0 and x < 10:
  30. self.__turn += 1
  31. self.col[x - 1] = "X"
  32.  
  33. def who_win(self):
  34. if self.__turn == 9:
  35. return "Draw"
  36. col = self.col
  37. win = [("X", "X", "X"), ("O", "O", "O")]
  38. rules = [(0,1,2), (3,4,5), (6,7,8)]
  39. rules += [(0,3,6), (1,4,7), (2,5,8)]
  40. rules += [(0,4,8), (2,4,6)]
  41. for x in rules:
  42. w = tuple([col[i] for i in x])
  43. if w in win:
  44. return "X" if "X" in w else "O"
  45.  
  46. def main():
  47. gas = Tictactoe()
  48. p1 = True
  49. while True:
  50. if p1:
  51. p1 = False
  52. gas.p1_select(int(input("p1: ")))
  53. else:
  54. p1 = True
  55. gas.p2_select(int(input("p2: ")))
  56. gas.show()
  57. if gas.who_win() in ["X", "O"]:
  58. print("Pemenang: " + gas.who_win())
  59. break
  60. elif gas.who_win() == "Draw":
  61. print("Draw")
  62. break
  63.  
  64. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement