diff --git a/main.py b/main.py index 240246d..2f99aed 100644 --- a/main.py +++ b/main.py @@ -10,22 +10,20 @@ import random class TicTacToe: def __init__(self): - self.player1 = Player("X") - self.player2 = Player("O") + 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.make_random_move(self.board.emptyFields)) self.board.print() - if self.board.isWin(self.player1.token): - print("Player 1 has won.") + if self.board.isWin(self.player1.token, self.player1.name): break self.board.make_move(self.player2.token, self.player2.make_random_move(self.board.emptyFields)) self.board.print() - if self.board.isWin(self.player2.token): - print("Player 2 has won.") + if self.board.isWin(self.player2.token, self.player2.name): break class Board: @@ -44,11 +42,7 @@ class Board: else: print("This field's taken.") - """ - Ta funkcja byłaby dużo bardziej użyteczna, - gdyby informowała nas który z graczy jest zwycięzcą - """ - def isWin(self, token): + 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 @@ -57,6 +51,7 @@ class Board: (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 @@ -67,8 +62,9 @@ class Board: pass class Player: - def __init__(self, token): + def __init__(self, token, name): self.token = token + self.name = name def make_random_move(self, emptyFields): return random.choice(emptyFields)