Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need the code of the Tic-Tac-Toe grid 3x3 game with added Minimax with AlphaBeta pruning. The minimax algorithm should work with any of the

I need the code of the Tic-Tac-Toe grid 3x3 game with added Minimax with AlphaBeta pruning. The minimax algorithm should work with any of the available grid sizes, both for Xs and Os.

Here is the code where the minimax should be added:

import random

def show_board(game_board): print(" ") print(game_board[0] + "|" + game_board[1] + "|" + game_board[2] + " ") print(game_board[3] + "|" + game_board[4] + "|" + game_board[5] + " ") print(game_board[6] + "|" + game_board[7] + "|" + game_board[8] + " ")

def check_winner(game_board): if game_board[0] == game_board[1] == game_board[2] and game_board[0] != ' ': winner = game_board[0] elif game_board[3] == game_board[4] == game_board[5] and game_board[3] != ' ': winner = game_board[3] elif game_board[6] == game_board[7] == game_board[8] and game_board[6] != ' ': winner = game_board[6] elif game_board[0] == game_board[3] == game_board[6] and game_board[0] != ' ': winner = game_board[0] elif game_board[1] == game_board[4] == game_board[7] and game_board[1] != ' ': winner = game_board[1] elif game_board[2] == game_board[5] == game_board[8] and game_board[2] != ' ': winner = game_board[2] elif game_board[0] == game_board[4] == game_board[8] and game_board[0] != ' ': winner = game_board[0] elif game_board[2] == game_board[4] == game_board[6] and game_board[2] != ' ': winner = game_board[2] else: winner = None return winner

def is_draw(game_board): x = 0 for y in game_board: if y == " ": x += 1

if check_winner(game_board) == None and x == 0: return True return False

def make_optimal_move(game_board, player): if player == "O": opponent = "X" else: opponent = "O" if game_board[4] == " ": game_board[4] = opponent elif game_board[0] == " ": game_board[0] = opponent elif game_board[2] == " ": game_board[2] = opponent elif game_board[6] == " ": game_board[6] = opponent elif game_board[8] == " ": game_board[8] = opponent else: move_made = False while not move_made: rand_move = random.randint(0, 8) if game_board[rand_move] == " ": game_board[rand_move] = opponent move_made = True

def main(): game_board = [" ", " ", " ", " ", " ", " ", " ", " ", " "] show_board(game_board) player = input("choose X or O? ") while player != "X" and player != "O": player = input("enter a valid input (X or O). ") player_turn = True while True: if player_turn: player_move = int(input("enter the number of the cell you want to play in ")) while player_move < 0 or player_move > 8 or game_board[player_move] != " ": player_move = int(input("this cell is already full. enter the number of an empty cell ")) game_board[player_move] = player else: make_optimal_move(game_board, player) show_board(game_board) winner = check_winner(game_board) if winner is not None: print(winner + " wins") break if is_draw(game_board) == True: print("draw") break player_turn = not player_turn

main()

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

Distinguish the three types of damages available in tort cases.

Answered: 1 week ago

Question

3. Explain the forces that influence how people handle conflict

Answered: 1 week ago