Question
Change the game so that it is now 1 player against the computer. You must implement the computer logic so that it selects its next
Change the game so that it is now 1 player against the computer. You must implement the computer logic so that it selects its next moved using an Alpha-beta pruning algorithm.
import random
# Pre : accepts empty rockList, random pile as integers # Post: creates the board def create_board(rockList, randPile): # create board for i in range(0, randPile): randRock = random.randint(1, 8) rockList.append(randRock) print(\"-\" * 25)
# Pre : accepts empty rockList, random pile as integers # Post: prints the board def display_board(rockList, randPile): # display board print(\"-\" * 25) for i in range(0, randPile): print('Pile {}: {}'.format(i + 1, 'O' * rockList[i])) print(\"-\" * 25)
# Pre : accepts modified rockList, random integer for piles, current player as string # Post: returns a string when input is invalid, updates game board for following turns def get_valid_input(rockList, randPile, player): # Begin loop that tests for valid input - if valid, break loop - if not, keep asking while True: stones = input('{}, how many stones to remove? '.format(player)) piles = input('Pick a pile to remove from: ')
# If all condiitons for input are CORRECT, break out of loop if (stones and piles) and (stones.isdigit()) and (piles.isdigit()): if (int(stones) > 0) and (int(piles) 0): if (int(stones) => if (int(stones) != 0) and (int(piles) != 0): break=>
# If not, display this statement print(\"Hmmm.\",player,\", you entered an invalid value. Please try again.\")
# Update state rockList[int(piles) - 1] -= int(stones)
# Pre : accepts modified rockList, random integer for piles, names of players, current palyer as string # Post: prints winner of game, asks if players want to play game again, determine current player def play(rockList, randPile, name1, name2, player): # Begin loop to initiate player switching while rockList != [0] * len(rockList): get_valid_input(rockList, randPile, player)
# display the board display_board(rockList, randPile)
# switch players 2->1, 1->2 if player == name1: player = name2 else: player = name1
print(player,\" is the winner is :)\")
# Define an empty rocklist to append rocks to, define random integers, call functions rockList = [] # random number of rock piles randPile = random.randint(2, 5) # random number of rocks randRock = random.randint(1, 9)
#get the name of the players name1 = input(\"Enter player 1 name: \") name2 = input(\"Enter player 2 name: \")
# Set current player to 1 (for switching later) player = name1
create_board(rockList, randPile) display_board(rockList, randPile) # Set initial board
play(rockList, randPile, name1, name2, player)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started