""" | | -|-|- | | -|-|- | | """ import random class TicTacToe: def __init__(self): self.player1 = Player("X", "Player 1") self.player2 = Player("O", "Player 2") self.board = Board() def run(self): while True: self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.emptyFields)) self.board.print() if self.board.isWin(self.player1.token, self.player1.name): break self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.emptyFields)) self.board.print() if self.board.isWin(self.player2.token, self.player2.name): break class Board: def __init__(self): self.state = list(" "*9) self.emptyFields = [0, 1, 2, 3, 4, 5, 6, 7, 8] def print(self): board = ""+ self.state[0] +"|"+self.state[1] +"|"+self.state[2] +" \n-|-|-\n"+self.state[3] +"|"+self.state[4] +"|"+self.state[5] +"\n-|-|-\n"+self.state[6] +"|"+self.state[7] +"|"+self.state[8] +"\n" print(board) def make_move(self, token, field): if self.state[field] == " ": self.state[field] = token self.emptyFields.remove(field) else: print("This field's taken.") def isWin(self, token, name): if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or (self.state[0] == token and self.state[4] == token and self.state[8] == token) or (self.state[0] == token and self.state[3] == token and self.state[6] == token) or (self.state[1] == token and self.state[4] == token and self.state[7] == token) or (self.state[2] == token and self.state[5] == token and self.state[8] == token) or (self.state[2] == token and self.state[4] == token and self.state[6] == token) or (self.state[3] == token and self.state[4] == token and self.state[5] == token) or (self.state[6] == token and self.state[7] == token and self.state[8] == token)): print(name + " has won.") return True return False def get_state(): pass def get_action(): pass class Player: def __init__(self, token, name): self.token = token self.name = name def choose_random_field(self, emptyFields): return random.choice(emptyFields) game = TicTacToe() game.run()