From 71d34178f03183ccf9e962b7a90e79f6b19e4588 Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Wed, 23 Dec 2020 21:00:38 +0100 Subject: [PATCH 1/9] DZIALA --- main.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index ef6040d..312dba3 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,16 @@ class TicTacToe: """ def run(self): + player1 = Player("X") + player2 = Player("O") + + board = Board() + + for x in range(1,10): + board.make_move(player1.token, player1.make_random_move(board.state)) + board.print() + board.make_move(player2.token, player2.make_random_move(board.state)) + board.print() class Board: def __init__(self): @@ -42,24 +52,20 @@ class Board: class Player: def __init__(self, token): self.token = token + def make_random_move(self, state): emptyField = True while emptyField: - field = state[randint(0,8)] + randomField = randint(0,8) + field = state[randomField] if field == " ": - return + return randomField -player1 = Player("X") -player2 = Player("O") game = TicTacToe() game.run() -for x in range(1,10): - player.make_random_move(game.state) - game.print() - player1.make_random_move(game.state) - game.print() + -- 2.45.2 From b7b599e295272fc00b08b3b4bc356468359cb318 Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Wed, 23 Dec 2020 21:46:09 +0100 Subject: [PATCH 2/9] :3 --- main.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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 -- 2.45.2 From a38a3a3d3dd8c2bb24cd3ba60eef8373c3d9a7e3 Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Wed, 23 Dec 2020 23:37:04 +0100 Subject: [PATCH 3/9] dsds --- main.py | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index ac5eb03..240246d 100644 --- a/main.py +++ b/main.py @@ -6,37 +6,32 @@ | | """ -from random import randint +import random class TicTacToe: - """ - uruchamia program, gracze wykonuja na zmiane ruchy, - az do osiagniecia wygranej przez jednego z nich - """ + def __init__(self): + self.player1 = Player("X") + self.player2 = Player("O") + self.board = Board() def run(self): - player1 = Player("X") - player2 = Player("O") - - board = Board() - - for x in range(1,10): - board.make_move(player1.token, player1.make_random_move(board.state)) - board.print() - if board.isWin(player1.token): + 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.") break - board.make_move(player2.token, player2.make_random_move(board.state)) - board.print() - if board.isWin(player2.token): + 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.") 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" @@ -45,9 +40,14 @@ class 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.") + """ + Ta funkcja byłaby dużo bardziej użyteczna, + gdyby informowała nas który z graczy jest zwycięzcą + """ 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 @@ -69,16 +69,9 @@ class Board: class Player: def __init__(self, token): self.token = token - - - def make_random_move(self, state): - emptyField = True - while emptyField: - randomField = randint(0,8) - field = state[randomField] - if field == " ": - return randomField + def make_random_move(self, emptyFields): + return random.choice(emptyFields) -- 2.45.2 From d6ce3d2579f2418bb8151feb613b9f5bd20816cc Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Thu, 24 Dec 2020 00:33:54 +0100 Subject: [PATCH 4/9] dupa --- main.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) 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) -- 2.45.2 From 24601be13c4797482be623a5f162d9e424e1c180 Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Thu, 24 Dec 2020 00:39:08 +0100 Subject: [PATCH 5/9] ffss --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 2f99aed..f9bca0e 100644 --- a/main.py +++ b/main.py @@ -16,12 +16,12 @@ class TicTacToe: def run(self): while True: - self.board.make_move(self.player1.token, self.player1.make_random_move(self.board.emptyFields)) + 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.make_random_move(self.board.emptyFields)) + 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 @@ -66,7 +66,7 @@ class Player: self.token = token self.name = name - def make_random_move(self, emptyFields): + def choose_random_field(self, emptyFields): return random.choice(emptyFields) -- 2.45.2 From a3ad4bf7acc608da27d70c2be26dd305dd8bd22a Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Thu, 24 Dec 2020 06:10:53 +0100 Subject: [PATCH 6/9] sdsd --- main.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index f9bca0e..a5c41d8 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ """ import random +import sys, getopt class TicTacToe: def __init__(self): @@ -16,20 +17,20 @@ class TicTacToe: def run(self): while True: - self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.emptyFields)) + self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields)) self.board.print() - if self.board.isWin(self.player1.token, self.player1.name): + if self.board.is_win(self.player1.token, self.player1.name): break - self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.emptyFields)) + self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields)) self.board.print() - if self.board.isWin(self.player2.token, self.player2.name): + if self.board.is_win(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] + self.empty_fields = [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" @@ -38,11 +39,11 @@ class Board: def make_move(self, token, field): if self.state[field] == " ": self.state[field] = token - self.emptyFields.remove(field) + self.empty_fields.remove(field) else: print("This field's taken.") - def isWin(self, token, name): + def is_win(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 @@ -66,12 +67,16 @@ class Player: self.token = token self.name = name - def choose_random_field(self, emptyFields): - return random.choice(emptyFields) + def choose_random_field(self, empty_fields): + return random.choice(empty_fields) +opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="]) +number_of_rounds = None +for opt, arg in opts: + if opt in ("-n", "--numberofrounds"): + number_of_rounds = arg game = TicTacToe() -game.run() - - +for x in range(0, int(number_of_rounds)): + game.run() -- 2.45.2 From 24213bb7133916120e2bd69d19d17541c72b12ab Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Thu, 24 Dec 2020 06:27:20 +0100 Subject: [PATCH 7/9] dsds --- main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index a5c41d8..5b27ec5 100644 --- a/main.py +++ b/main.py @@ -13,9 +13,9 @@ class TicTacToe: def __init__(self): self.player1 = Player("X", "Player 1") self.player2 = Player("O", "Player 2") - self.board = Board() def run(self): + self.board = Board() while True: self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields)) self.board.print() @@ -54,6 +54,9 @@ class Board: (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.") + return True return False def get_state(): -- 2.45.2 From b8dcadbeb32f2ce6714759007e7225f888df2f43 Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Fri, 25 Dec 2020 04:20:54 +0100 Subject: [PATCH 8/9] 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 -- 2.45.2 From 17ebde7fab4df1454818bf69c08c76ea3c34a619 Mon Sep 17 00:00:00 2001 From: Paulina Nowak Date: Fri, 25 Dec 2020 04:45:14 +0100 Subject: [PATCH 9/9] asaaaaas --- main.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 55b86cd..251dca3 100644 --- a/main.py +++ b/main.py @@ -21,18 +21,18 @@ class TicTacToe: if self.board.is_win(self.player1.token): self.board.print() if len(self.board.empty_fields) == 0: - print("It's a draw.") + print("It's a draw.\n") else: - print(self.player1.name + " has won.") + print(self.player1.name + " has won.\n") break self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields)) if self.board.is_win(self.player2.token): self.board.print() if len(self.board.empty_fields) == 0: - print("It's a draw.") + print("It's a draw.\n") else: - print(self.player2.name + " has won.") + print(self.player2.name + " has won.\n") break class Board: @@ -83,11 +83,16 @@ number_of_rounds = None try: opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="]) except getopt.GetoptError: + print("main.py -n ") + sys.exit(2) + +if(len(sys.argv) != 1): + for opt, arg in opts: + if opt in ("-n", "--numberofrounds"): + number_of_rounds = arg +else: print("main.py -n ") sys.exit() -for opt, arg in opts: - if opt in ("-n", "--numberofrounds"): - number_of_rounds = arg game = TicTacToe() for x in range(0, int(number_of_rounds)): -- 2.45.2