mt_sample2

该文章描述了一个基于Python实现的石子游戏,游戏有两个石子堆,玩家每回合必须从任一堆中拿走至少一个石子,或者从两堆中拿走相同数量的石子。游戏目标是拿走最后一颗石子。文章提供了初始化游戏状态、合法步骤、执行步骤和游戏玩法的方法实现,并给出了具体的游戏状态空间表示和规则。
摘要由CSDN通过智能技术生成

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值