Write a program to create a tic-tac-toe game in python.
Requirements and Specifications
In this assignment students will demonstrate their understanding of how to create and manipulate NumPy Arrays.
For this assignment, refer to page 277 of your textbook. The instructions below list the assignment requirements.
- Create a Python code file named M5HW_TicTacToe_FirstLast.py
- Add a title comment block to the top of the new Python file using the following form
- Complete Exercise 7.10 on page 277 of your textbook. If you are up for another challenge, do the next level NumPy array MUST be used
- To create an empty array, use the .empty() method and set the dtype = str. This is referenced in the book
- To display the array as a grid(board), you will have to use a for loop to iterate over the array. Since it is 2 dim , you will have 2 for loops . One for the rows and another for the columns. I already have the code for this part below:
- Now , you can start asking the user what cell they want to mark. For example for the second cell in the first row, you want to identify the row index and the column index.
- So you are to ask the user to enter the row number and the column number. For instance if user wants first row second column the indexes will be row(0) and column(1). Why , because we know that index position start with 0
- To mark the cell the user defined, use slicing. So back to our example above, if the grid is called (ticTac), the code to populate that cell will be something like this
- Now , as the users play you want to evaluate if all the cells in a row or column have been filled with the same symbol , also if all boxed have been filled diagonally with the same symbol. If any of these conditions is true, you have a win and the game should end
- Complete Exercise 7.11 on page 277 of your textbook. In this step, you will be building on the program you did for level 1.
(replace "FirstLast" with your own name)
# A brief description of the project
# CSC121 M5HW – Tic Tac Toe
# Your Name
Level 1 = 50 points
There are several online resources that have this part solved but none use NumPy arrays. You can therefore use them as a reference but CAN NOT copy a paste code.
To help you get started , I have a few hints and code listed below:
With this code, the grid is going to look something like this.
To do this you will have to use both a for loop and a decision structure.
Level 2 = 40 points
Hint: Use the random library for the computer’s pick
Submit your finished code solution file(s) through the assignment link below:
Note: Write program Pseudocode (detail algorithm) and add it as a comment block to the submitted program. (10 points)
Note : ONLY USE PROGRAMMING METHODS LEARNED IN CLASS. If submission doesn't comply, you will get a grade of "1".
# A brief description of the project# Date# CSC121 M5HW – Tic Tac Toe# Your Nameimport numpy as npimport randomdef init():return np.empty((3,3), dtype=str)def display(grid):for row in grid:for col in row:if len(col) == 0:print("|_", end="|")else:print("|" + col, end="|")print()def make_move(grid, place, symbol):row = placecol = placeif row < 0 or row > 2 or col < 0 or col > 2:return Falseif grid[row][col] != '':return Falsegrid[row][col] = symbolprint("Move", (row, col), "is made by", symbol)return Truedef game_state(grid):has_empty = Falsefor i in range(3):for j in range(3):if grid[i][j] == '':has_empty = Truefor i in range(3):if grid[i] == '':continuecurr = grid[i]is_same = Truefor j in range(1, 3):if curr != grid[i][j]:is_same = Falseif is_same:return curr + " wins!"for i in range(3):if grid[i] == '':continuecurr = grid[i]is_same = Truefor j in range(1, 3):if curr != grid[j][i]:is_same = Falseif is_same:return curr + " wins!"if grid != '':curr = gridis_same = Truefor j in range(1, 3):if curr != grid[j][j]:is_same = Falseif is_same:return curr + " wins!"if grid != '':curr = gridis_same = Truefor j in range(1, 3):if curr != grid[2-j][j]:is_same = Falseif is_same:return curr + " wins!"if not has_empty:return "Draw"return Nonedef player_move(grid, symbol):while True:row = int(input("Please, enter row: "))col = int(input("Please, enter column: "))if make_move(grid, (row, col), symbol):returnprint("Invalid input. Try again")def computer_move(grid, symbol):while True:row = random.randrange(0, 3)col = random.randrange(0, 3)if make_move(grid, (row, col), symbol):returnprint("Invalid input. Try again")if __name__ == '__main__':tictactoe = init()symbols = ["X", "O"]players = [True, False]to_move = 0state = Nonewhile not state:display(tictactoe)if players[to_move]:print("Human player (" + str(symbols[to_move] + ") to move:"))player_move(tictactoe, symbols[to_move])else:print("Computer player (" + str(symbols[to_move] + ") to move:"))computer_move(tictactoe, symbols[to_move])state = game_state(tictactoe)to_move = (to_move + 1) % 2print()display(tictactoe)print(state)