Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CODE in Python please The Game of Nimsticks Nimsticks is a turn-taking zero-sum game for two players. The game state consists of one or more

CODE in Python please

The Game of Nimsticks Nimsticks is a turn-taking zero-sum game for two players. The game state consists of one or more piles of sticks. On their turn, a player may take 1, 2 or 3 sticks from any one pile. The player who takes the last stick is the loser of the game. You are recommended to work it out on paper first so you understand what the tree will look like!

Example game:

At the start of the game, we have a pile of 3 sticks and a pile of 2 sticks [3,2]. The players are Alice and Bob.

Alice takes 1 stick from the pile of 3. State is now [2, 2].

Bob takes 1 stick from a pile of 2. The state is now [2, 1].

Alice takes 2 sticks from the pile of 2. The state is now [1].

Bob takes the final stick. Alice wins!

Implementing the Minimax Algorithm minimax_value(state)

You need to implement the following function:

def minimax_value(state):

# return the minimax value of a state

The state will be a tuple containing two parts: the current state of the piles, and whos turn it is (1 = MAX, 2 = MIN). For example, if there is a pile of 3 sticks and 2 sticks, and it is Maxs turn to play, then the input should look like ([3, 2], 1)

minimax_value(([3,2],1))

It would make sense to divide your code into several functions, all called from minimax_value. It would make sense to have at the minimum separate max and min functions

There are 2 terminal states, ([], 1) a win for MAX, so the value is +1, and ([], 2) a win for MIN, so the value is 1. You should check for these states, as per the algorithm

For example, in the algorithm, identifying successors, is similar to defining next states

Your function should also display a game that is played according to the players optimal strategies. You may (or may not!) need to define a global variable within your minimax function, or return path AND value from min and max, depending on whether or not you are creating separate functions for min and max player.

The output should be a value, either -1 or +1, and your minimax function should print an example optimal game path. Your path does not have to be exactly the same as in the examples!

Sample Examples

Some Sample outputs, the path is printed within the minimax function, and the value is returned as a string.

A single pile of 4 sticks, max to go first, resulting in max winning (output 1).

print(minimax_value(([4],1)))

Example Play: [([4], 1), ([1], 2), ([], 1)] 1

Two piles of sticks, 2 and 3 in each, max to go first, resulting in max winning.

print(minimax_value(([2,3],1)))

Example Play: [([2, 3], 1)([2, 2], 2)([1, 2], 1)([1], 2)([], 1)] 1

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

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions