Instructions
Requirements and Specifications
- 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
# Date
# 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.
ticTac[0][1]= “X”
To do this you will have to use both a for loop and a decision structure.
Level 2 = 40 points
# 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 = place[0] col = place[1] if row < 0 or row > 2 or col < 0 or col > 2: return False if grid[row][col] != '': return False grid[row][col] = symbol print("Move", (row, col), "is made by", symbol) return Truedef game_state(grid): has_empty = False for i in range(3): for j in range(3): if grid[i][j] == '': has_empty = True for i in range(3): if grid[i][0] == '': continue curr = grid[i][0] is_same = True for j in range(1, 3): if curr != grid[i][j]: is_same = False if is_same: return curr + " wins!" for i in range(3): if grid[0][i] == '': continue curr = grid[0][i] is_same = True for j in range(1, 3): if curr != grid[j][i]: is_same = False if is_same: return curr + " wins!" if grid[0][0] != '': curr = grid[0][0] is_same = True for j in range(1, 3): if curr != grid[j][j]: is_same = False if is_same: return curr + " wins!" if grid[2][0] != '': curr = grid[2][0] is_same = True for j in range(1, 3): if curr != grid[2-j][j]: is_same = False if 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): return print("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): return print("Invalid input. Try again")if __name__ == '__main__': tictactoe = init() symbols = ["X", "O"] players = [True, False] to_move = 0 state = None while 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) % 2 print() display(tictactoe) print(state)
Related Samples
Discover our Python Assignment samples, offering solutions across fundamental to advanced concepts. Access clear, annotated code for algorithms, data structures, web scraping, GUI development, and more. Strengthen your Python skills with practical examples and step-by-step explanations, tailored to empower students in mastering versatile programming applications.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python