<jupyter_start><jupyter_text>Lab 4 &ndash; DATA 3401 (Fall 2021) Lab Dates: 9/24 & 10/1 Due Date 10/8 (before the beginning of lab) Lab DescriptionThe purpose of this lab is for you to code your own tic-tac-toe game that takes input from the user for each player and visualizes the state of the board after each move- As in the previous labs, copy and paste the cells below into a jupyter notebook titled Lab 4- Solve the problems and push them to a new Lab 4 folder in your GitHub repo prior to the deadlineYou will build an n x n Tic Tac Toe game. As you do the exercises, make sure your solutions work for any size Tic Tac Toe game, not just the standard 3x3.   Exercise 1:Write a function that creates an n by n matrix (a list of lists) which will represent the state of a Tic Tac Toe game. Let 0, 1, and 2 represent empty, "X", or "O".<jupyter_code>def make_game(size=3):
    Tic_Tac_Toe=[[0]*size for i in range(size)]
    return Tic_Tac_Toe
game=make_game(3)
game<jupyter_output><empty_output><jupyter_text>Exercise 2:Write a function that takes a `n` by `n` matrix representing a tic-tac-toe game, and returns -1, 0, 1, or 2 indicating the game is incomplete, the game is a draw, player 1 has won, or player 2 has one, respectively. Here are some example inputs you can use to test your code:<jupyter_code>def make_game_board(num_rows,num_cols):
    board = [[empty]*num_cols for i in range(num_rows)]
    
    global row_names, row_map, column_names, column_map
    row_names = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    row_map = dict(zip(row_names,range(num_rows)))

    column_names = list(map(str,range(1,num_cols+1)))
    column_map = dict(zip(column_names,range(num_cols)))
    
    return board
def draw_board(board):
    num_rows = len(board)
    num_cols = len(board[0])
      
    print("  ",end=" ")
    for i in range(num_cols):
        print(column_names[i],end=" ")
        print(" ",end = " ")
    
    print()
    print("  ",end=" ")
    print("----"*num_cols)
    for i in range(num_rows):
        print(row_names[i],end=" ")
        print("|",end=" ")
        for j in range(num_cols):
            print(space_character[board[i][j]],end=" ")
            print("|",end=" ")
        print()  
        print("  ",end=" ")
        print("----"*num_cols)

board2 = make_game_board(4,5)
draw_board(board2)<jupyter_output><empty_output><jupyter_text>Exercise 3Write a function that takes 2 integers `n` and `m` as input and draws a `n` by `m` game board. For example the following is a 3x3 board:```   --- --- ---   |   |   |   |    --- --- ---    |   |   |   |    --- --- ---    |   |   |   |    --- --- ---    ```<jupyter_code>def draw_board(n,m):
for i in range(n):
    print("---"*m)
    print("|  "*(m+1))
print("---"*m)
draw_board(3,3)<jupyter_output><empty_output><jupyter_text>Exercise 4:Modify exercise 3, so that it takes a matrix of the form from exercise 2 and draws a tic-tac-tie board with "X"s and "O"s.<jupyter_code>def draw_tic_tac_toe(matrix):
l = len(matrix)
  
for i in range(l):
    if i!=0:
    print("\n----------")
    for j in range(l):
    var = matrix[i][j]
      if var == 1:
        var = "X"
    if var == 2:
        var = "O"
    if var == 0:
        var = " "
    if j!=0:
    print("| "+var+" ",end="")
      if j==0:
    print(" "var,end="")
draw_tic_tac_toe(no_winner)<jupyter_output><empty_output><jupyter_text>Exercise 5:Write a function that takes a game board, player number, and `(x,y)` coordinates and places "X" or "O" in the correct location of the game board. Make sure that you only allow filling previously empty locations. Return `True` or `False` to indicate successful placement of "X" or "O".<jupyter_code>def place_on_board(matrix,player_number,x_coordinate,y_coordinate):
player_1_piece = "X"
  player_2_piece = "O"
empty_space = " "
  if matrix[x_coordinate][y_coordinate]!=0:
    return False
  elif player_number == 1:
    matrix[x_coordinate][y_coordinate]=1
    return True
  elif player_number == 2:
    matrix[x_coordinate][y_coordinate]=2
    return True
board=make_game()
place_on_board(board,2,1,2)
draw_tic_tac_toe(board)<jupyter_output>|   |   
----------
  |   | O 
----------
  |   |<jupyter_text>Exercise 6:Modify Exercise 5 to show column and row labels so that players can specify location using "A2" or "C1".<jupyter_code># Write you solution here
def draw_tic_tac_toe_updated(matrix):
l = len(matrix)
  print("  1   2   3")
for i in range(l):
    if i!=0:
    print("\n  ----------")
    for j in range(l):
    var = matrix[i][j]
      if var == 1:
        var = "X"
    if var == 2:
        var = "O"
    if var == 0:
        var = " "
    if i==0 and j==0:
        print("A ",end="")
    if i==1 and j==0:
        print("B ",end="")
    if i==2 and j==0:
        print("C ",end="")
    if j!=0:
    print("| "+var+" ",end="")
      if j==0:
        print(" "+var,end="")
draw_tic_tac_toe_updated(no_winner)<jupyter_output><empty_output><jupyter_text>Exercise 7:Write a function that takes a board, player number, and location specified as in exercise 6 and then calls exercise 5 to correctly modify the board.<jupyter_code># Write you solution here
def nice_make_move(board,player,location):
    loc = parse_location(location)
    
    if loc:
        return make_move(board,player,loc)
    else:
        print_message("Invalid move.")
        return False
# Test your solution here
def parse_location(location_string):
    if not isinstance(location_string,str):
        print_message("Bad input. Location must be a string.")
        return False
    
    if len(location_string) != 2:
        print_message("Bad input. Location must be 2 characters.")
        return False
    
    row = location_string[0].upper()
    col = location_string[1].upper()
    
    if not row in row_names:
        print_message("This is not an existing row.")
        return False<jupyter_output><empty_output><jupyter_text>Exercise 8:Write a function is called with a board and player number, takes input from the player using python's `input`, and modifies the board using your function from exercise 7. Note that you should keep asking for input until you have gotten a valid input that results in a valid move.<jupyter_code># Write you solution here
def make_move(board,player,location,verbose=True):
    x,y = location
    # Check if space is empty
    if board[x][y] != empty:
        print_message("Invalid move n/: space is not empty")
        return False
    else:
        board[x][y] = player
        return True
# Test your solution here<jupyter_output><empty_output><jupyter_text>Exercise 9: Use all of the previous exercises to implement a full tic-tac-toe game, where an appropriate board is drawn, 2 players are repeatedly asked for a location coordinates of where they wish to place a mark, and the game status is checked until a player wins or a draw occurs.<jupyter_code>def col_winner():
        for i in range(num_cols):
            if is_winner(col_list[i]) == player_1:
                return player_1
            if is_winner(col_list[i]) == player_2:
                return player_2
        return False<jupyter_output><empty_output><jupyter_text>Exercise 10:Test that your game works for 5x5 Tic Tac Toe.<jupyter_code>def diag_winner():
        for i in range(2):
            if is_winner(diag_list[i]) == player_1:
                return player_1
            if is_winner(diag_list[i]) == player_2:
                return player_2
        return False
    
    def is_full():
        n = 0
        for i in range(num_rows):
            for j in range(num_cols):
                if board[i][j] != empty:
                    n+=1
        if n==(num_rows*num_cols):
            return True
        else:
            return False
        
    def winner():
        row_state = row_winner()
        col_state = col_winner()
        diag_state = diag_winner()
        if row_state:
            return row_state
        elif col_state:
            return col_state
        elif diag_winner():
            return diag_state
        else:
            return False
    
    if winner():
        return winner()
    elif is_full():
        return 0
    else:
        return -<jupyter_output><empty_output><jupyter_text>Exercise 11:(Extra Credit)Develop a version of the game where one player is the computer. Note that you don't need to do an extensive seach for the best move. You can have the computer simply protect against loosing and otherwise try to win with straight or diagonal patterns.<jupyter_code># Write you solution here
def tictactoe_game():
    print("Welcome to Tic-Tac-Toe!")
    print("--------------------")
    
    board_0 = make_game_board(3,3)
    
    player = player_1
    
    this_game_state = -1
    
    while this_game_state==-1:
        draw_board(board_0)
        
        print("Player",player,"move:")
        new_move = take_move(board_0,player)
        
        if new_move == "end":
            print("Player has exited the game")
            break
        this_game_state = game_state(board_0)
        
        if player==player_1:
            player = player_2
        else:
            player = player_1
         
    if not new_move == "end":
        draw_board(board_0)
        if this_game_state == 0:
            print("The game is a draw")
        else:
            print("Winner is player",this_game_state)
# Test your solution here<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup