diff --git a/main.py b/main.py index 312dba3..ac5eb03 100644 --- a/main.py +++ b/main.py @@ -7,8 +7,6 @@ """ from random import randint -from random import choice - class TicTacToe: """ @@ -25,8 +23,15 @@ class TicTacToe: for x in range(1,10): board.make_move(player1.token, player1.make_random_move(board.state)) board.print() + if board.isWin(player1.token): + print("Player 1 has won.") + break + board.make_move(player2.token, player2.make_random_move(board.state)) board.print() + if board.isWin(player2.token): + print("Player 2 has won.") + break class Board: def __init__(self): @@ -43,6 +48,18 @@ class Board: else: print("This field's taken.") + def isWin(self, token): + 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)): + return True + return False + def get_state(): pass