Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coded by: salism3
- # 26 - 02 - 2020
- class Tictactoe:
- def __init__(self):
- self.col = list(range(1,10))
- self.p1 = None
- self.p2 = None
- self.__turn = 0
- def show(self):
- for_format = str(self.col).replace("[", "").replace("]", "")
- data = box="""\x1b[1;33m ╔═══╦═══╦═══╗
- ║ {} \x1b[1;33m║ {} \x1b[1;33m║ {} \x1b[1;33m║
- ╠═══╣═══╣═══╣
- ║ {} \x1b[1;33m║ {} \x1b[1;33m║ {} \x1b[1;33m║
- ╠═══╣═══╣═══╣
- ║ {} \x1b[1;33m║ {} \x1b[1;33m║ {} \x1b[1;33m║
- ╚═══╩═══╩═══╝\x1b[1;39m"""
- data = eval("data.format({})".format(for_format))
- print(data)
- def p1_select(self, x):
- if self.col[x - 1] != "X" and self.col[x - 1] != "O" and x > 0 and x < 10:
- self.__turn += 1
- self.col[x - 1] = "O"
- def p2_select(self, x):
- if self.col[x - 1] != "O" and self.col[x - 1] != "X" and x > 0 and x < 10:
- self.__turn += 1
- self.col[x - 1] = "X"
- def who_win(self):
- if self.__turn == 9:
- return "Draw"
- col = self.col
- win = [("X", "X", "X"), ("O", "O", "O")]
- rules = [(0,1,2), (3,4,5), (6,7,8)]
- rules += [(0,3,6), (1,4,7), (2,5,8)]
- rules += [(0,4,8), (2,4,6)]
- for x in rules:
- w = tuple([col[i] for i in x])
- if w in win:
- return "X" if "X" in w else "O"
- def main():
- gas = Tictactoe()
- p1 = True
- while True:
- if p1:
- p1 = False
- gas.p1_select(int(input("p1: ")))
- else:
- p1 = True
- gas.p2_select(int(input("p2: ")))
- gas.show()
- if gas.who_win() in ["X", "O"]:
- print("Pemenang: " + gas.who_win())
- break
- elif gas.who_win() == "Draw":
- print("Draw")
- break
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement