1. The game is set up with two piles of rocks with the number of N and M rocks. In each turn, the players must pick at least one rock from one of the piles or pick from each pile. In the second case, the player must pick the same number of rocks from each pile. The player wins if picks the last rock from the piles. a. init method: 1 point
b. legal_steps method: 2 points
c. take_steps method: 2 points
d. play_game method 1 point
Implement the below-mentioned methods of the python script (sample_mid_term.py) using the following state space representation of the game:
1. Description: Let N = 32, M = 48
H 1= {0,…, N}, H2 = {0,…,M} - Number of rocks in piles
2. Set of states:
𝑆 ⊆𝐻1×𝐻2=> 𝑆=𝐻1×𝐻2
3. Initial state:
𝒔𝟎= <𝑵,𝑴>
4. Set of end states:
𝐺={<0,0>}
5. Set of steps:
𝑶={𝒐𝒊,𝒋 | 𝒊 ∈{𝟎,…,𝑵}∧𝒋 ∈{𝟎,…,𝑴}∧(𝒊 ≠𝟎∧𝒋≠𝟎)⊃𝑖=𝑗∧𝑖=0 ⊃𝑗 ≠0∧𝑗=0 ⊃𝑖 ≠0}
6. Domain of operations:
𝒅𝒐𝒎 (𝒐𝒊,𝒋)={<𝒔𝟏,𝒔𝟐>|𝒊 ≤𝒔𝟏∧𝒋 ≤𝒔𝟐}
7. Transition function:
𝒐𝒊,𝒋(<𝒔𝟏,𝒔𝟐>)= <𝒓𝟏,𝒓𝟐> 𝒓𝒌= {𝒔𝒌−𝒊,𝒊𝒇 𝒌=𝒊𝒔𝒌−𝒋,𝒊𝒇 𝒌=𝒋 𝒌 ∈{𝟏,𝟐}
import random
def init():
"""
Initialize the game state.
Returns:
A tuple of (N, M), where N and M are the number of rocks in the two piles.
"""
return (32, 48)
def legal_steps(state):
"""
Return a list of all legal steps from the given state.
Args:
state: A tuple of (N, M), where N and M are the number of rocks in the two piles.
Returns:
A list of tuples of (i, j), where i and j are the number of rocks to remove from the first and second piles, respectively.
"""
# Check if the state is an end state.
if state == (0, 0):
return []
# Get the possible steps.
steps = []
for i in range(state[0] + 1):
for j in range(state[1] + 1):
if i == 0 and j == 0:
continue
if i == j:
steps.append((i, j))
else:
steps.append((i, 0))
steps.append((0, j))
return steps
def take_steps(state, steps):
"""
Take the given steps from the given state.
Args:
state: A tuple of (N, M), where N and M are the number of rocks in the two piles.
steps: A list of tuples of (i, j), where i and j are the number of rocks to remove from the first and second piles, respectively.
Returns:
The new state after taking the steps.
"""
new_state = list(state)
for i, j in steps:
new_state[0] -= i
new_state[1] -= j
return tuple(new_state)
def play_game(player_1, player_2):
"""
Play a game of rock-paper-scissors between the two players.
Args:
player_1: A function that takes a state and returns a step.
player_2: A function that takes a state and returns a step.
Returns:
A tuple of (winner, steps), where winner is the player who won the game, and steps is a list of all the steps taken in the game.
"""
# Initialize the game state.
state = init()
# Keep track of the steps taken.
steps = []
# Play the game until one player wins.
while state != (0, 0):
# Get the steps from each player.
step_1 = player_1(state)
step_2 = player_2(state)
# Check if the steps are legal.
if step_1 not in legal_steps(state) or step_2 not in legal_steps(state):
raise ValueError("Invalid step")
# Take the steps.
new_state = take_steps(state, (step_1, step_2))
# Update the state and steps.
state = new_state
steps.append((step_1, step_2))
# Return the winner.
if state == (0, 0):
return None, steps
else:
return state[0] > 0, steps