<jupyter_start><jupyter_text>Milestone Project 1: Tic-Tac-Toe Game<jupyter_code>from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
def game_start():
    global p1_name
    global p2_name
    print("""Would you like to play a game?
    How about either Tic-Tac-Toe or Global Thermonuclear War?

    (just kidding, I don't know how to play Global Thermonuclear War, let's play tic-tac-toe.)
    """)
    p1_name = input('Player 1, what is your name? ')
    p2_name = input('Player 2, what is your name? ')

    print("""\n
    Thank you!
    Here are the 'structions: You will take turns choosing a space on the board by giving me the position wish to play (row first, then column).
    For example: 'top right' or 'bottom middle' or 'middle middle'

    \nYou may enter 'x' or 'exit' at any time to exit the game.  Good luck and may the best player win.\n""")
    print(p1_name + ", you are 'X's")
    print(p2_name + ", you are 'O's\n")
    print(p1_name + ', you go first.')
def reset_game():
    # Resetting Variables for New Game
    global board
    global XOs
    global turns
    global p1_list
    global p2_list
    global playerlist
    global playername
    global winning_combos
    dash = '-'
    board = [[dash, dash, dash],[dash, dash, dash],[dash, dash, dash]]
    XOs = (0,'X','O')
    turns = 0
    p1_list = []
    p2_list = []
    playerlist = [0, p1_list, p2_list]
    playername = [0, p1_name, p2_name]
    winning_combos = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7))

def show_board(board):
    # This function shows the board to the players at the start of each new move
    print("Here's the Board:")
    print('=' * 15)
    for row in board:
        print(' ' * 4, row[0], row[1], row[2])
    print('=' * 15)

def player_move(player):
    print("Okay " + playername[player] + ", you're up.  What's your move?")
    move = input().strip()
    if 'x' in move or 'exit' in move:
        return 'exit'
    else:
        return move

def test_valid(move):
    # This function tests whether the move entered was in the valid format, and returns False if not
    try:
        move = move.split(sep=' ')
    except:
        return False
    
    positions = {'top': 0, 'left':0, 'middle': 1, 'bottom': 2, 'right': 2}
    try:
        moverow, movecol = positions[move[0]], positions[move[1]]
    except:
        return False
    return (moverow, movecol)

def test_avail(moverow, movecol, board):
    # This function tests whether the spot on the board the player indicated is still open or not, and returns False if not
    return board[moverow][movecol] == '-'

def record_move(player, moverow, movecol):
    # This function records the move on the board, and adds the number of the position (1-9) to that player's 'playerlist'
    global board
    global playerlist
    board[moverow][movecol] = XOs[player]
    playerlist[player].append((moverow*3) + (movecol+1))
    
def test_win(playerlist):
    for combo in winning_combos:
        if set(combo).issubset(set(playerlist)):
            return True
    return False
    
def test_draw(board):
    for sublist in board:
        for item in sublist:
            if item == '-':
                return False
    return True
    
def assess_move(player, move, playerlist, board):
    # This function calls validating functions to be sure the move is a valid one, then records the move and tests
    # to see if the move was a winner, or if the move covered the last spot on the board without either player being 
    # a winner (a draw).  If neither of these, it instructs the game to move to the next player's move by returning a value
    # of 0.  
    # (Return Values: 
    #     -1 indicates the same player needs to try again (invalid move)
    #     0 indicates it's the next player's turn
    #     1 indicates that this move is a winner or resulted in a draw and this game is over.)
    
    validity = test_valid(move)
    if validity == False:
        print("That is not a valid entry. Please enter a move in the correct format (e.g.,'top right')")
        return -1
    moverow, movecol = validity
    if test_avail(moverow, movecol, board) == False:
        print('That spot has already been taken. Please enter another spot.')
        return -1
    else:
        record_move(player, moverow, movecol)
    
    if test_win(playerlist) == True:
        print()
        print('Tic-Tac-Toe!!!! The ' + XOs[player] + 's have it!!! Congratulations, ' + playername[player] + '!')
        return 1
    elif test_draw(board) == True:
        print("All spots are filled and no one has won. It's a Draw!")
        return 1
    else:
        return 0
        
def newgame():
    print()
    print()
    print('Starting a New Game:')
    print()
    reset_game()
    gameplay()
def gameplay():
    turns = 0
    while True:
        turns += 1
        if turns % 2 == 0:
            player = 2
        else:
            player = 1
        show_board(board)
        move = player_move(player)
        if move == 'exit':
            print()
            print('Okay ... now exiting game. Smell ya later!')
            print()
            break
        status = assess_move(player, move, playerlist[player], board)
        if status == -1:
            turns -=1
            continue
        if status == 0:
            continue
        if status == 1:
            turns = 0
            newgame()
        

game_start()
reset_game()
gameplay()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10859318346627991, 'fraction_numerical': 0.015409717186366932, 'mean_word_length': 2.4741813602015115, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '153844', 'license': 'MIT', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1704, 'n_words': 630, 'path': 'Milestone Project 1--Ken Script.ipynb', 'repo_name': 'kenmaready/pythonbootcamp', 'size': '5516'}