diff --git a/Board.py b/Board.py index 84e2f63..44dd672 100644 --- a/Board.py +++ b/Board.py @@ -12,7 +12,13 @@ class Board: self.state[field] = token self.empty_fields.remove(field) else: - print("This field's taken.") + print("This field's already taken. Choose another one.") + while field not in self.empty_fields: + field = int(input()) + if field not in self.empty_fields: + print("This field's already taken. Choose another one.") + self.state[field] = token + self.empty_fields.remove(field) def is_win(self, token): if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or diff --git a/HumanPlayer.py b/HumanPlayer.py new file mode 100644 index 0000000..70c3e03 --- /dev/null +++ b/HumanPlayer.py @@ -0,0 +1,8 @@ +from Player import Player + +class HumanPlayer(Player): + def __init__(self, token, name): + Player.__init__(self, token, name) + + def choose_field(self, empty_fields): + return int(input()) \ No newline at end of file diff --git a/Player.py b/Player.py index cd915e4..5d7234d 100644 --- a/Player.py +++ b/Player.py @@ -3,7 +3,7 @@ import random class Player: def __init__(self, token, name): self.token = token - self.name = name + self.name = name - def choose_random_field(self, empty_fields): - return random.choice(empty_fields) + def choose_field(self, empty_fields): + return random.choice(empty_fields) diff --git a/RandomPlayer.py b/RandomPlayer.py new file mode 100644 index 0000000..9140458 --- /dev/null +++ b/RandomPlayer.py @@ -0,0 +1,4 @@ +from Player import Player + +class RandomPlayer(Player): + pass diff --git a/TicTacToe.py b/TicTacToe.py index 078df99..59aa874 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -1,26 +1,42 @@ from Player import Player +from RandomPlayer import RandomPlayer +from HumanPlayer import HumanPlayer from Board import Board class TicTacToe: - def __init__(self): + def __init__(self, player_x, player_o): self.players = [] - self.players.append( Player("X", "Player 1") ) - self.players.append( Player("O", "Player 2") ) + self.player_x = player_x + self.player_o = player_o + + if self.player_x == "random": + self.players.append(RandomPlayer("X", "Player X")) + else: + self.players.append(HumanPlayer("X", "Player X")) + + if self.player_o == "random": + self.players.append(RandomPlayer("O", "Player O")) + else: + self.players.append(HumanPlayer("O", "Player O")) def run(self): self.board = Board() player = 0 + if self.player_x == "human": + self.board.print() + while len(self.board.empty_fields) != 0: - self.board.make_move(self.players[player].token, self.players[player].choose_random_field(self.board.empty_fields)) + self.board.make_move(self.players[player].token, self.players[player].choose_field(self.board.empty_fields)) + self.board.print() + if self.board.is_win(self.players[player].token): - self.board.print() print(self.players[player].name + " has won.\n") break - if (player == 0): + + if player == 0: player = 1 else: player = 0 else: - self.board.print() - print("It's a draw.\n") + print("It's a draw.") \ No newline at end of file diff --git a/main.py b/main.py index c9009ad..0d5af3d 100644 --- a/main.py +++ b/main.py @@ -1,21 +1,48 @@ import sys, getopt from TicTacToe import TicTacToe -number_of_rounds = None +games = None +player_x = None +player_o = None + try: - opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="]) + opts, args = getopt.getopt(sys.argv[1:], "n:", ["games="]) except getopt.GetoptError: - print("main.py -n ") + print(''' + usage: main.py -n GAMES + player_x player_o + + positional arguments: + player_x Type of the X player [human|random] + player_o Type of the O player [human|random] + + optional arguments: + -n GAMES, --games GAMES + number of games to play + ''') sys.exit(2) -if(len(sys.argv) != 1): +if((len(sys.argv) != 1) and (sys.argv[-2] in ("random", "human")) and (sys.argv[-1] in ("random", "human"))): for opt, arg in opts: if opt in ("-n", "--numberofrounds"): - number_of_rounds = arg + games = arg + player_x = sys.argv[-2] + player_o = sys.argv[-1] else: - print("main.py -n ") + print(''' + usage: main.py -n GAMES + player_x player_o + + positional arguments: + player_x Type of the X player [human|random] + player_o Type of the O player [human|random] + + optional arguments: + -n GAMES, --games GAMES + number of games to play + ''') sys.exit() -game = TicTacToe() -for x in range(0, int(number_of_rounds)): +game = TicTacToe(player_x, player_o) +for x in range(0, int(games)): game.run() \ No newline at end of file