Имея проблемы с получением результатов для обновления в Mancala Code Player 2 Оценка остается на ноль.Python

Программы на Python
Anonymous
 Имея проблемы с получением результатов для обновления в Mancala Code Player 2 Оценка остается на ноль.

Сообщение Anonymous »

player1 = [4, 4, 4, 4, 4, 4, 0] # Player 1's pockets + Mancala
player2 = [4, 4, 4, 4, 4, 4, 0] # Player 2's pockets + Mancala

print("Welcome to Mancala!")

def explain_rules():
"""Explain the game rules to the players."""
print("""
GAME RULES:
- Players take turns choosing a pocket (1-6) on their side of the board.
- Stones are distributed counterclockwise, one by one, into each pocket.
- If your last stone lands in your Mancala, you get another turn.
- If your last stone lands in an empty pocket on your side, you capture stones from the opposite pocket.
- The game ends when one side's pockets are empty. The remaining stones go to the opponent's Mancala.
""")

def table():
"""Print the current state of the board with proper alignment."""
pocket_labels = " 6 5 4 3 2 1"
print(f"\n Pocket # : {pocket_labels}")
print(f'P2 --> [ {" ".join(f"{p:>2}" for p in reversed(player2[:6]))} ]', f" Score: {player2[6]:>2}")
print(f'P1 --> [ {" ".join(f"{p:>2}" for p in player1[:6])} ]', f" Score: {player1[6]:>2}")
print(f" Pocket # : {pocket_labels}\n")

def distribute_stones(player, opponent, player_num, pocket_index, stones):
"""Distribute stones around the board counterclockwise."""

original_stones = stones # Store the original number of stones
current_index = pocket_index + 1 # Start distributing from the next pocket

for _ in range(stones):
if player_num == 1:
if current_index > 13:
current_index = 0
if current_index == 6:
player[6] += 1
elif current_index < 6:
player[current_index] += 1
elif current_index > 6 and current_index < 13:
opponent[current_index - 7] += 1
current_index += 1

elif player_num == 2:
if current_index > 13:
current_index = 0
if current_index == 6:
opponent[6] += 1
elif current_index < 6:
player[current_index] += 1
elif current_index > 6 and current_index < 13:
opponent[current_index - 7] += 1
current_index += 1
return current_index - 1, original_stones # Return both last_index and original_stones

def capture_stones(player, opponent, player_num, index, original_stones):
"""Handle capturing stones when the last stone lands in an empty pocket."""
if index < 6 and player[index] == 1: # Only valid for Player's side (pockets 1 to 6)
if player_num == 1 and opponent[5 - index] > 0: # Player 1's capture logic
player[6] += opponent[5 - index] + original_stones # Capture stones in opponent's opposite pocket
player[index] = 0 # Empty the player's pocket
opponent[5 - index] = 0 # Empty the opponent's captured pocket
elif player_num == 2 and opponent[index] > 0: # Player 2's capture logic
opponent[6] += player[5-index] + original_stones # Capture stones in opponent's opposite pocket
opponent[index] = 0 # Empty the opponent's pocket
player[5 - index] = 0 # Empty the player's captured pocket

def move_player(player, opponent, player_num):
"""Handle a player's turn by distributing stones and applying game rules."""
while True:
try:
# Get the pocket choice and validate input
move = input(f"\nPlayer {player_num}, choose a pocket to move (1-6 or 'q' to quit): ")

if move.lower() == 'q': # Quit the game
print("Game quit. Final scores:")
return False

move = int(move) # Convert to integer
if move < 1 or move > 6: # Validate pocket number
print("Invalid input. Please choose a pocket between 1 and 6.")
continue
except ValueError: # Handle non-numeric input
print("Invalid input. Please enter a number between 1 and 6.")
continue

# Check if the chosen pocket is empty
stones = player[move - 1]
if stones == 0:
print("That pocket is empty. Choose a different pocket.")
continue

# Clear the chosen pocket and distribute stones
player[move - 1] = 0
last_index, original_stones = distribute_stones(player, opponent, player_num, move - 1, stones)

# Handle capture condition
capture_stones(player, opponent, player_num, last_index, original_stones)

table()

# Check if the player gets another turn (if last stone lands in their Mancala)
if (player_num == 1 and last_index == 6) or (player_num == 2 and last_index == 13):
print("You get another turn!")
continue
else:
break

return True # Continue game

def game():
"""Main game loop, alternating turns until the game ends."""
explain_rules()
table()

while True:
# Check if either side of the board is empty
p1_total = sum(player1[:6])
p2_total = sum(player2[:6])

if p1_total == 0 or p2_total == 0:
player1[6] += p1_total
player2[6] += p2_total
for i in range(6):
player1 = 0
player2 = 0
break

if not move_player(player1, player2, 1): # Player 1's turn
return
if sum(player2[:6]) != 0 and not move_player(player2, player1, 2): # Player 2's turn
return

# Game Over - Display results with "Score: #"
print('\nGAME OVER.')
print(f'Player 1 Score: {player1[6]}')
print(f'Player 2 Score: {player2[6]}')
if player1[6] > player2[6]:
print("Player 1 wins!")
elif player2[6] > player1[6]:
print("Player 2 wins!")
else:
print("It's a tie!")

# Start the game
game()


Подробнее здесь: https://stackoverflow.com/questions/793 ... -remains-a

Вернуться в «Python»