def run_two_player_mode(): """ Runs two player Connect three on variable length board """ MIN_ROWS = 3 MAX_ROWS = 7 MIN_COLS = 3 MAX_COLS = 7 rows = get_int( "Please select number of rows (Min: {} Max: {}): ".format( MIN_ROWS, MAX_ROWS), MIN_ROWS, MAX_ROWS) cols = get_int( "Please select number of columns (Min: {} Max: {}): ".format( MIN_COLS, MAX_COLS), MIN_COLS, MAX_COLS) game = Connect3Board(rows, cols) print(game) while game.get_winner() is None: move = get_int( "Player {}'s turn. Choose column (0 to {})".format( game.get_whose_turn(), cols - 1), 0, cols - 1) if game.can_add_token_to_column(move) is True: game.add_token(move) print(game) else: print("That column is not available. Please choose again.") if game.get_winner() == Connect3Board.DRAW: print("This game has ended in a draw!") else: print("Player {} wins!".format(game.get_winner()))
def board_testing(): a = [3, 10, 20, 40, 80, 100, 200, 400, 800, 1000] for i in a: start = timer() game = Connect3Board(i, i) end = timer() print(end - start)
def run_two_player_mode(): c = get_int("How many columns?") r = get_int("How many rows?") start = timer() game = Connect3Board(c, r) end = timer() print(game) print(end - start) while True: print("Turn " + str(game.get_turn_number())) print(str(game.get_whose_turn()) + "'s Turn") uinput = get_int("In which column do you want to put?") if game.can_add_token_to_column(uinput): game.add_token(uinput) if game.get_winner(): print(game) print(game.get_winner() + " Wins") break else: print(game) else: print("cannot lah") return 0
def main(): test = Connect3Board(3, 3) Tree = GameTree(test) print('Welcome to Connect 3 by Kevin Richards') mode = get_mode() while mode != 'Q': if mode == 'A': run_two_player_mode() elif mode == 'B': run_ai_mode(Tree) mode = get_mode()
def run_two_player_mode(): row = checkIntValue(0) column = checkIntValue(1) board = Connect3Board(row,column) print(board) for i in range(0,4): board.move() winner = board.get_winner() while winner is None: board.move() winner = board.get_winner() print("Player " + winner + " wins!") pass
def run_ai_mode(): """ Run Connect 3 against AI on a 3x3 board """ player = player_piece() rows = 3 cols = 3 game = Connect3Board(rows, cols) print(game) game_tree = GameTree(game) position = game_tree.get_root_position() while game.get_winner() is None: if game.get_whose_turn() == player: move = get_int( "Your turn. Choose column (0 to {}): ".format(cols - 1), 0, cols - 1) if game.can_add_token_to_column(move) is True: game.add_token(move) position = position.get_child(move) print(game) else: print("ERROR: Invalid move, please try again") else: children_scores = position.get_children_scores() child_index = None max_score = -2 min_score = 2 for i, child in enumerate(children_scores): if game.get_whose_turn() == game.TOKENS[0]: if child is not None and child > max_score: max_score = child child_index = i else: if child is not None and child < min_score: min_score = child child_index = i game.add_token(child_index) position = position.get_child(child_index) print("AI's turn") print(game) if game.get_winner() == Connect3Board.DRAW: print("This game has ended in a draw!") else: print("Player {} wins!".format(game.get_winner()))
def run_two_player_mode(): game_not_done = True board = Connect3Board(3, 3) while game_not_done: print(board) whose_turn = board.get_whose_turn() token_choice = int(input("Choose a row to place a {}: ".format(whose_turn))) board.add_token(token_choice) if board.get_winner() is not None: winner = board.get_winner() game_not_done = False print(board) print("Player {} won".format(winner)) # for you to complete... pass
def run_ai_mode(): board = Connect3Board(3, 3) player_token = select_player_token() game_tree = GameTree(board) print(game_tree.count) position_tree = game_tree.get_root_position() while board.get_winner() == None: if board.get_whose_turn() == player_token: print(board) column_choice = get_int( "Player {}'s turn. Choose column ({} to {}):".format( board.get_whose_turn(), 0, board.get_columns() - 1)) if board.can_add_token_to_column(column_choice): board.add_token(column_choice) # find the children from the tree based on the users selection position_tree = position_tree.get_child(column_choice) else: print("You cannot add a token at column {}".format( column_choice)) else: print("AI's turn") child_scores = position_tree.get_children_scores() # select the best child score from the children best_child = 0 for index, score in enumerate(child_scores): # pick the score based on player selecting O token if score is not None and player_token != GameTree.MIN_PLAYER and score < GameTree.MAX_WIN_SCORE: best_child = index # pick the best score based on player selecting # token elif score is not None and player_token != GameTree.MAX_PLAYER and score > GameTree.MIN_WIN_SCORE: best_child = index board.add_token(best_child) # navigate to the next child in the tree position_tree = position_tree.get_child(best_child) print(board) # Display the winner if its not a draw if board.get_winner() != board.DRAW: print("Player {} wins!".format(board.get_winner())) else: print(board.get_winner())
def run_two_player_mode(): rows = get_int("How many rows? (3 to 7) ") columns = get_int("How many columns? (3 to 7) ") board = Connect3Board(rows, columns) while board.get_winner() == None: print(board) column_choice = get_int( "Player {}'s turn. Choose column ({} to {}):".format( board.get_whose_turn(), 0, board.get_columns() - 1)) if board.can_add_token_to_column(column_choice): board.add_token(column_choice) else: print("You cannot add a token at column {}".format(column_choice)) print(board) # Display the winner if its not a draw if board.get_winner() != board.DRAW: print("Player {} wins!".format(board.get_winner())) else: print(board.get_winner())
def run_ai_mode(Tree): Tree = Tree._root char = input("Will you play as O or #? ") while char not in "#O": char = input("Will you play as O or #? ") board = Connect3Board(3,3) print(board) if char == 'O': winner = None while winner is None: mover = board.check_player_move(1) board.add_token(mover) print(board) winner = board.get_winner() if winner is not None: break print("AI's turn") Tree = Tree._children[mover] list = Tree._children scores = [] for child in list: if child is not None: scores.append(child._score) else: scores.append(1000) min_score = min(scores) column_no = 0 for child_score in scores: if child_score == min_score or child_score==-1: board.add_token(column_no) Tree = Tree._children[column_no] break column_no+=1 print(board) winner = board.get_winner() if winner is not None: break print("Player " + winner + " wins!") else: winner = None while winner is None: print("AI's turn") list = Tree._children scores = [] for child_score in list: if child_score is not None: scores.append(child_score._score) else: scores.append(-1000) max_score = max(scores) column_no = 0 for child_score in scores: if child_score == max_score or child_score==1: board.add_token(column_no) Tree = Tree._children[column_no] break column_no += 1 print(board) winner = board.get_winner() if winner is not None: break mover = board.check_player_move(1) board.add_token(mover) print(board) Tree = Tree._children[mover] winner = board.get_winner() if winner is not None: break print("Player " + winner + " wins!")
def run_ai_mode(): c = get_int("How many columns?") r = get_int("How many rows?") game = Connect3Board(c, r) print(game)