From ea39495deeca073e7b0ad7b3601f7a7693d5825b Mon Sep 17 00:00:00 2001 From: Fei Dong Date: Sat, 26 Oct 2013 23:29:18 -0400 Subject: [PATCH 1/4] Made player object and most of board object. --- uttt.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 uttt.py diff --git a/uttt.py b/uttt.py new file mode 100644 index 0000000..a53b259 --- /dev/null +++ b/uttt.py @@ -0,0 +1,86 @@ +class Game(object): + """The Ultimate game class.""" + + def __init__(self): + # # List of 9 small boards + # self.boards = [Board() for i in range(10)] + # # The main board that is trying to be won + # self.main_board = Board() + self.p1 = Player('x') + self.p2 = Player('o') + self.current_player = self.p1 + + def has_winner(self): + pass + + def print_board(self): + pass + + def next_move(self): + pass + + def message(self, msg): + pass + + #def is_valid_move(self, move): + # a, b = move + # if a == self._next_board or self._next_board == -1: + # self.boards[a].is_valid_move(b) + # else: + # False + + def run(self): + pass + + def __repr__(self): + pass + + +class Board(object): + """Represents a standard 3x3 tic-tac-toe board.""" + def __init__(self): + self.spaces = [i for i in range(9)] + self.avail = set(self.spaces) + self.won = False # make property + + def check_if_won(self): + win_conds = {self.spaces[0] == self.spaces[1] == self.spaces[2], + self.spaces[3] == self.spaces[4] == self.spaces[5], + self.spaces[6] == self.spaces[7] == self.spaces[8], + self.spaces[0] == self.spaces[3] == self.spaces[6], + self.spaces[1] == self.spaces[4] == self.spaces[7], + self.spaces[2] == self.spaces[5] == self.spaces[8], + self.spaces[0] == self.spaces[4] == self.spaces[8], + self.spaces[2] == self.spaces[4] == self.spaces[6]} + + self.won = True in win_conds + + def make_move(self, index, player): + """Mark the space at the given index with the given player's symbol""" + if index in self.avail: + self.spaces[index] = player.symbol + self.avail.remove(index) + self.check_if_won(player) + else: + raise Exception("Invalid Move! >:(") + + def __repr__(self): + return self.spaces + + def __str__(self): + divider = "-------" + row1 = "{}|{}|{}".format(*self.spaces[:3]) # TODO: stop printing initialized numbers + + +class Player(object): + """The player object""" + def __init__(self, turn, symbol): + self.turn = turn # 1st or 2nd + self.symbol = symbol + + def __repr__(self): + return self.symbol + + +if __name__ == "__main__": + Game().run() From 546ccc8fee050baceb8ecbff8a3d7acfad634a43 Mon Sep 17 00:00:00 2001 From: Fei Dong Date: Sun, 27 Oct 2013 17:52:23 -0400 Subject: [PATCH 2/4] Completed __str__ method for Board class. --- uttt.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/uttt.py b/uttt.py index a53b259..b2f1697 100644 --- a/uttt.py +++ b/uttt.py @@ -2,18 +2,14 @@ class Game(object): """The Ultimate game class.""" def __init__(self): - # # List of 9 small boards - # self.boards = [Board() for i in range(10)] - # # The main board that is trying to be won - # self.main_board = Board() - self.p1 = Player('x') - self.p2 = Player('o') + self.p1 = Player(1, 'x') + self.p2 = Player(2, 'o') self.current_player = self.p1 def has_winner(self): pass - def print_board(self): + def get_board(self): pass def next_move(self): @@ -35,6 +31,8 @@ def run(self): def __repr__(self): pass + def __str__(self): + return self.get_board() class Board(object): """Represents a standard 3x3 tic-tac-toe board.""" From 9364888f115d1b28ecaa9e5c46c8bc04174b0bc4 Mon Sep 17 00:00:00 2001 From: Fei Dong Date: Sun, 3 Nov 2013 00:33:52 -0400 Subject: [PATCH 3/4] Initial commit. --- uttt.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/uttt.py b/uttt.py index b2f1697..f759bf2 100644 --- a/uttt.py +++ b/uttt.py @@ -18,13 +18,6 @@ def next_move(self): def message(self, msg): pass - #def is_valid_move(self, move): - # a, b = move - # if a == self._next_board or self._next_board == -1: - # self.boards[a].is_valid_move(b) - # else: - # False - def run(self): pass From e5af858cfaa9db6f4874e1febc9469fbfea33035 Mon Sep 17 00:00:00 2001 From: Brian Gapinski Date: Sun, 3 Nov 2013 01:35:31 +0000 Subject: [PATCH 4/4] Fixed board str and repr. Created player order in game --- uttt.py | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/uttt.py b/uttt.py index f759bf2..82b668e 100644 --- a/uttt.py +++ b/uttt.py @@ -2,9 +2,23 @@ class Game(object): """The Ultimate game class.""" def __init__(self): - self.p1 = Player(1, 'x') - self.p2 = Player(2, 'o') - self.current_player = self.p1 + self.p1 = Player('x') + self.p2 = Player('o') + self.current_player = None # self.decide_order() + self.subboards = [Board() for _ in range(9)] + self.mainboard = Board() + + def decide_order(self): + # TODO: Add random option + text = "Who would like to go first? (1 / 2): " + while True: + player = input(text) + if player == "1": + return self.p1 + elif player == "2": + return self.p2 + else: + print("Please select either 1 or 2") def has_winner(self): pass @@ -19,7 +33,10 @@ def message(self, msg): pass def run(self): - pass + self.current_player = self.decide_order() + # TODO: Implement the main game loop + while True: + print("Nyaa~") def __repr__(self): pass @@ -27,10 +44,11 @@ def __repr__(self): def __str__(self): return self.get_board() + class Board(object): """Represents a standard 3x3 tic-tac-toe board.""" def __init__(self): - self.spaces = [i for i in range(9)] + self.spaces = [" " for i in range(9)] self.avail = set(self.spaces) self.won = False # make property @@ -56,17 +74,22 @@ def make_move(self, index, player): raise Exception("Invalid Move! >:(") def __repr__(self): - return self.spaces + return str(self.spaces) def __str__(self): - divider = "-------" - row1 = "{}|{}|{}".format(*self.spaces[:3]) # TODO: stop printing initialized numbers + # TODO: List comprehensions yo + divider = "-----" + row1 = "{}|{}|{}".format(*self.spaces[:3]) + row2 = "{}|{}|{}".format(*self.spaces[3:6]) + row3 = "{}|{}|{}".format(*self.spaces[6:]) + return "\n".join([row1, divider, row2, divider, row3]) +# Is this class even necessary? +# Probably not. It should be removed. class Player(object): """The player object""" - def __init__(self, turn, symbol): - self.turn = turn # 1st or 2nd + def __init__(self, symbol): self.symbol = symbol def __repr__(self):