Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use the python code below and add the AI and minimax: asad To implement the full version of tic-tac-toe, you need to create an Al

use the python code below and add the AI and minimax:

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedasadimage text in transcribed

To implement the full version of tic-tac-toe, you need to create an Al (Artificial Intelligence) Player that can play against the user. In this assignment, you have to create three different types of Al: a simple Al that chooses moves randomly, a sophisticated SmartAl that never looses a game because it is based on heuristic approaches, and a MiniMax that also never looses a game because it precalculates all possible moves. All three Al classes should be placed in the same module, the file player.py, where the class Player is written. Al should be a subclass of the class Player and should inherit all properties from its superclass Player. The init and choose methods should be overridden (modified). The simplest implementation of an Al player is to use a random choice for generating a valid move. For this strategy, you need to create all possible moves: in the beginning of the game, all moves are empty cells on the board, so you can create a list of all cells and then remove the occupied cells from the board as the game progresses. You can import choice from the random module to randomly choose a cell (a move) from the list of all possible cells (moves). The output of the program should be the same as before, the user plays as Alice and the Al plays as Bob. The only difference from the previous tic-tac-toe game is that the user does not have to play for Bob, the Al (your computer program) plays instead of the user. To do so, you need to modify tictac.py to create an Al object: you can achieve it by substituting player1 = Player("Bob", "X") to player1 = Al("Bob", "X", board) and the import statement from player import Player to from player import Player, Al. Minimax Algorithm To improve the performance of a random-choice Al, you can create another class, MiniMax that uses a minimax algorithm based on recursion. MiniMax should be a subclass of Al and inherit all methods from its superclass Al. The method choose should be overridden and should call the recursive minimax method. The pseudocode for minimax is shown below, "self" refers to the MiniMax player and "opponent" - to its opponent: Minimax: 1. Check the base case: if the gane is over, then return 1 if self lost, 0 if it is a tie, or 1 if self won. 2. Set the min score to infinity and max score to -infinity 3. Choose a cell (or nake a move) : a. Iterate through a11 avallable cells ( 9 cells) b. check if the cell is enpty c. mark the cell with x or 0 (if self then nark it with its sign, otherwise mark it with another sign) d. get score by calling the mininax recursively (you need to alternate between self and opponent) e. update score: if self then use the max score (compare it to the nax score), otherwise use the min score (compare it to the min score) f. update move: update the cell index g. unnark the cell (make it a space again " ") and reset all other variables that were affected by the gane play 4. If it is the start level (the last level to be executed completely and the last score to be returned) return the nove. Let see how it works. At the start, the program chooses the first cell (e.g., 'A1') and marks it with X, then on the next level it chooses the next cell (e.g., 'B1') and marks it with O, on the third level it chooses the next cell (e.g., ' C1 ') and marks it with X, and so on until there will be a winning condition or a tie. i.e., the game is over. After that, the program returns to the previous upper level and updates the min and max scores depending on whose turn is. The process continues until it reaches the beginning level. At this point the program should return the optimal move as a cell. You can read more about a minimax algorithm here: Minimax - Wikipedia _

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

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions