Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use python code ############################################################################## # Tic-tac-toe (or noughts and crosses) is a simple strategy game in which two players # take turns placing a

Please use python code

############################################################################## # Tic-tac-toe (or noughts and crosses) is a simple strategy game in which two players # take turns placing a mark on a 3x3 board, attempting to make a row, column, # or diagonal of three with their mark. In this homework, # we will use the tools we've covered in the past two weeks to create # a tic-tac-toe simulator and evaluate basic winning strategies. # Do not forget to add print calls to see what is going on with variables #############################################################################

# Exercise 1 # * For our tic-tac-toe board, we will use a numpy array with dimension 3 by 3. # Make a function create_board() that creates such a board, with values of integers 0. # * Call create_board(), and store this as board.

# Do not forget to import numpy, use numpy help to find proper array functions # Your code here

# Exercise 2 # * Players 1 and 2 will take turns changing values of this array from a 0 to a 1 or 2, # indicating the number of the player who places there. # Create a function place(board, player, position) with # player being the current player (an integer 1 or 2), # and position being a tuple of length 2 specifying a desired location (from 0 to 2) # to place their marker. Only allow the current player to place a piece on the board # (change the board position to their number) if that position is empty (zero). # * Use create_board() to store a board as board, and use place to have # Player 1 place a piece on spot (0, 0).

# write your code here!

# Exercise 3 # * Create a function possibilities(board) that returns a list of all positions (tuples) # on the board that are not occupied (0). # * board is already defined from previous exercises. Call possibilities(board) to see what it returns!

# write your code here!

# Exercise 4 # * Write a function random_place(board, player) that places a marker for # the current player at random among all the available positions (those currently set to 0). # - Find possible placements with possibilities(board). # - Select one possible placement at random using random.choice(selection). # * board is already defined from previous exercises. Call random_place(board, player) # to place a random marker for Player 2, and store this as board to update its value.

# write your code here!

# Exercise 5 # * board is already defined from previous exercises. Use random_place(board, player) # to place three pieces on board each for players 1 and 2. # * Print board to see your result.

board = create_board() for i in range(3): for player in [1, 2]: # add here!

# Exercise 6 # * Now that players may place their pieces, how will they know they've won? # Make a function row_win(board, player) that takes the player (integer), # and determines if any row consists of only their marker. # Have it return True of this condition is met, and False otherwise. # * board is already defined from previous exercises. # Call row_win to check if Player 1 has a complete row.

# write your code here!

# Exercise 7 # * Create a similar function col_win(board, player) that # takes the player (integer), and determines if any column # consists of only their marker. Have it return True # if this condition is met, and False otherwise. # * board is already defined from previous exercises. # Call col_win to check if Player 1 has a complete column.

# write your code here!

# Exercise 8 # * Finally, create a function diag_win(board, player) that # tests if either diagonal of the board consists of only their marker. # Have it return True if this condition is met, and False otherwise. # * board is already defined from previous exercises. # Call diag_win to check if Player 1 has a complete diagonal.

# write your code here!

# Exercise 9 # * Create a function evaluate(board) that uses row_win, col_win, # and diag_win functions for both players. If one of them has won, # return that player's number. If the board is full but no one has # won, return -1. Otherwise, return 0. # * board is already defined from previous exercises. # Call evaluate to see if either player has won the game yet.

def evaluate(board): winner = 0 for player in [1, 2]: # Check if `row_win`, `col_win`, or `diag_win` apply. if so, store `player` as `winner`. # part of your code here! if np.all(board != 0) and winner == 0: winner = -1 return winner

# add your code here.

# Exercise 10 # * create_board(), random_place(board, player), and evaluate(board) # have been created from previous exercises. # Create a function play_game() that creates a board, calls alternates between # two players (beginning with Player 1), and evaluates the board for a winner # after every placement. Play the game until one player wins # (returning 1 or 2 to reflect the winning player), or the game is a draw (returning -1). # * Call play_game once.

# write your code here!

# Exercise 11 # * Use the play_game() function to play 1,000 random games, where Player 1 always goes first. # * When doing this, import and use the time library to call the time function # both before and after playing all 1,000 games in order to evaluate # how long this takes per game. Print your answer. # * The library matplotlib.pyplot has already been stored as plt. Use plt.hist and plt.show to plot a # histogram of the results. See help of that functions if needed. # Does Player 1 win more than Player 2? # Does either player win more than each player draws?

# please do not forget to import time as tm, matplotlib.pyplot as plt, random # write your code here!

# Exercise 12 # * This result is expected --- when guessing at random, it's better to go first. # Let's see if Player 1 can improve their strategy. create_board(), # random_place(board, player), and evaluate(board) have been created from # previous exercises. # Create a function play_strategic_game(), where Player 1 always starts # with the middle square, and otherwise both players place their markers randomly. # * Call play_strategic_game once.

def play_strategic_game(): board, winner = create_board(), 0 board[1,1] = 1 while winner == 0: for player in [2,1]: # use `random_place` to play a game, and store as `board`. # use `evaluate(board)`, and store as `winner`. #add your code here if winner != 0: break return winner

#add your code here

# Exercise 13 # * Use the play_strategic_game() function to play 1,000 random games. # * Use the time libary to evaluate how long all these games takes. # * The library matplotlib.pyplot has already been stored as plt. # Use plt.hist and plt.show to plot your results. # Did Player 1's performance improve? Does either player win more than each player draws?

# write your code here!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago