From b8dcadbeb32f2ce6714759007e7225f888df2f43 Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Fri, 25 Dec 2020 04:20:54 +0100 Subject: [PATCH] asasa --- main.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 5b27ec5..55b86cd 100644 --- a/main.py +++ b/main.py @@ -18,19 +18,27 @@ class TicTacToe: self.board = Board() while True: self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields)) - self.board.print() - if self.board.is_win(self.player1.token, self.player1.name): + if self.board.is_win(self.player1.token): + self.board.print() + if len(self.board.empty_fields) == 0: + print("It's a draw.") + else: + print(self.player1.name + " has won.") break self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields)) - self.board.print() - if self.board.is_win(self.player2.token, self.player2.name): + if self.board.is_win(self.player2.token): + self.board.print() + if len(self.board.empty_fields) == 0: + print("It's a draw.") + else: + print(self.player2.name + " has won.") break class Board: def __init__(self): self.state = list(" "*9) - self.empty_fields = [0, 1, 2, 3, 4, 5, 6, 7, 8] + self.empty_fields = [x for x in range(9)] 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" @@ -43,7 +51,7 @@ class Board: else: print("This field's taken.") - def is_win(self, token, name): + def is_win(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 @@ -51,11 +59,8 @@ class Board: (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 - if len(self.empty_fields) == 0: - print("It's a draw.") + (self.state[6] == token and self.state[7] == token and self.state[8] == token) or + len(self.empty_fields) == 0): return True return False @@ -71,11 +76,15 @@ class Player: self.name = name def choose_random_field(self, empty_fields): - return random.choice(empty_fields) + return random.choice(empty_fields) -opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="]) number_of_rounds = None +try: + opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="]) +except getopt.GetoptError: + print("main.py -n ") + sys.exit() for opt, arg in opts: if opt in ("-n", "--numberofrounds"): number_of_rounds = arg