Question

In: Computer Science

Coin taking game This game is played between 2 players, player 1 and player 2. There...

Coin taking game

This game is played between 2 players, player 1 and player 2. There are two piles of coins. The values of a coin can be any integer. Both players know the values of all coins in both piles. Player 1 makes the first move, and play alternates between the players. A move consists of taking a coin from the top of either of the piles (either player can take from either pile). The game ends when both piles are empty. A player’s score is the sum of the values of all the coins they took during the game. The player with the higher score wins.

1 Winning the coin taking game

In this task you will write a function best_score(pile1, pile2) in python to determine optimal play for the coin taking game. It will determine the highest score which can be achieved by player 1, assuming that both players play optimally.

1.1 Input

Two lists, pile1 and pile2. These lists contain integers which represent the values of the coins in the game. The numbers in pile1 are the values of coins in the first pile, where pile1[I] is the value of the coin with i coins beneath it in the pile. pile2 represents the coins in the second pile in the same way. The piles may be empty.

1.2 Output

A tuple with two elements. The first is a single number which represents the highest score which player 1 can achieve. The second represents the choices made by both players during the game (assuming optimal play). The choices are represented by a list containing only the numbers 1 and 2, where a 1 indicates that the player took the top coin from pile1 on their turn, and 2 indicates that the player took the top coin from pile2 on their turn.

If multiple sequences of choices are optimal, any of these sequences is acceptable.

Solutions

Expert Solution

Screenshots of the code:

Output:

Code to copy:

def best_scoreHelper(pile1, pile2, turn=1):
if len(pile1) == 0 and len(pile2) == 0:
return (0, 0)
  
if len(pile1) == 0:
# when one pile is empty, we have no option but
# to take the card.
if turn == 1:
b = best_scoreHelper(pile1, pile2[1:], 2)
return (pile2[0] + b[0], b[1])
if turn == 2:
b = best_scoreHelper(pile1, pile2[1:], 1)
return (b[0], pile2[0] + b[1])
if len(pile2) == 0:
if turn == 1:
b = best_scoreHelper(pile1[1:], pile2, 2)
return (pile1[0] + b[0], b[1])
if turn == 2:
b = best_scoreHelper(pile1[1:], pile2, 1)
return (b[0], pile1[0] + b[1])
# else, We have both piles available now.
if turn == 1:
x1 = best_scoreHelper(pile1[1:], pile2, 2)
x1 = (x1[0] + pile1[0], x1[1])
x2 = best_scoreHelper(pile1, pile2[1:], 2)
x2 = (x2[0] + pile2[0], x2[1])
  
if x1[0] > x2[0]:
return x1
else:
return x2
else:
x1 = best_scoreHelper(pile1[1:], pile2, 1)
x1 = (x1[0], x1[1] + pile1[0])
x2 = best_scoreHelper(pile1, pile2[1:], 1)
x2 = (x2[0], x2[1] + pile2[0])
  
if x1[1] > x2[1]:
return x1
else:
return x2
def best_score(pile1, pile2):
return best_scoreHelper(pile1, pile2, 1)[0] # take score for first player
print(best_score([5, 8, 2, 4, 1, 10, 2], [6, 2, 4, 5, 6, 9, 8]))


Related Solutions

5. Consider the following games played between two players, A and B.   Game 1: A and...
5. Consider the following games played between two players, A and B.   Game 1: A and B have reached a verbal agreement: A would deliver a case of beer to B, and B would deliver a bag of beer nuts to A. Now, each player needs to take an action: keep the promise (to deliver the goods), break the promise. If both keep their promises, then each player gets a payoff of 5; if both break their promises, then each...
Describe what it means to say a strategic game played with 2 players is in a...
Describe what it means to say a strategic game played with 2 players is in a Nash equilibrium. Then, describe the standard trust game, and identify the Nash equilibrium. Explain why this equilibrium is a Nash Equilibrium. Finally, explain why people actually playing this game might not play as the Nash Equilibrium predicts.
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to be the one that starts with the jar. Actions: - Each round at the same time both players deposit between 1 to 4 pennies into the jar. - Then both players are able to count the pennies in the jar. - If there are 21 or more pennies, the person with the jar is the winner. - If there are 20 or less pennies,...
There are two players in the game. Each player can pick any integer number between 1...
There are two players in the game. Each player can pick any integer number between 1 and n. If two numbers are the same then player 1 pays 1 dollar to player 2. If two numbers are different than nothing happens. (a) Prove that there are no equilibria in pure strategies; (b) Prove that in the equilibrium each strategy should be played with a positive probability. (c) Find all NE of the game.
A sequential game with two-players 1 and 2, where player 1 has the first move advantage....
A sequential game with two-players 1 and 2, where player 1 has the first move advantage. Each player has two strategies, A and B. If both players choose A, each gets a payoff of 2. Both choose B, each gets a payoff of 3. For player 1 choose A and player 2 choose B, player 1 gets 4, and player 2 gets 1. If player 1 chooses B and player 2 choose A, player 1 gets a payoff of 1,...
A game is to be played by first flipping a fair coin, and then drawing balls...
A game is to be played by first flipping a fair coin, and then drawing balls without replacement from a bin. If you flip heads you get to draw one ball, and if you flip tails you get two draw two balls. The bin contains 7 red balls, and 4 white balls. Let W = 4x - 2 be your win amount, where X represents the number of white balls drawn. What is your expected win amount on any given...
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player...
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player simultaneously chooses either a rock or a paper or scissors (usually with an outstretched hand). The rule of the game is simple: rock crushes scissors, scissors cut paper, and paper wraps rock. If both the players choose the same object, then it ends in a tie. Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the...
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player. If it is a draw, no payment is made. The payoff to a player is the amount of money (s)he gets. Represent the situation in a matrix form. Find all the Nash equilibria.
1-If a static game where both players have dominant strategies was to be played sequentially, A....
1-If a static game where both players have dominant strategies was to be played sequentially, A. the outcome of the dynamically played game would be the same with the outcome of the simultaneously played game. B. the Nash equilibrium of the game will not be sub-game perfect. C. the dominant strategies will no longer exist. D. the outcome of the dynamically played game would be different than the outcome of the simultaneously played game. 2- A sub-game perfect Nash equilibrium...
This game is meant for two or more players. In the game, each player starts out...
This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes a turn rolling the dice; the amount generated by the dice is subtracted from the player’s points. The first player with exactly one point remaining wins. If a player’s remaining points minus the amount generated by the dice results in a value less than one, then the amount should be added to the player’s points. (As an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT