Question

USING RECURSIONN!!! JAVA CODE Coin game: Alice and Bob are playing a game using a bunch...

USING RECURSIONN!!! JAVA CODE

Coin game: Alice and Bob are playing a game using a bunch of coins. The players
pick several coins out of the bunch in turn. Each time a player is allowed to pick 1, 2 or 4
coins, and the player that gets the last coin is the winner. Assume that both players are
very smart and he/she will try his/her best to work out a strategy to win the game. For
example, if there are 2 coins and Alice is the rst player to pick, she will denitely pick 2
coins and win. If there are 3 coins and Alice is still the rst player to pick, no matter she
picks 1 or 2 coins, Bob will get the last coin and win the game.
Given the number of coins and the order of players (which means the rst and the second
players to pick the coins), you are required to write a program pickcoin.java to calculate
the winner of the game, and calculate how many different strategies there are for he/she
to win the game. You should use recursion to solve the problem, and the parameters are
read from the command line. You can assume that there are no more than 35 coins.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

public class CoinGame
{
public static void Count(int n, String[] s) // n: no. of coins
//s:{"Alice","Bob"} (order)
{
if(n>0)
{
if(n==1 || n==2 || n==4) System.out.println( s[0] ); // if 1/2/4
//coins left, the one with first chance wins
else
{
Count(n-1,new String[]{s[1],s[0]});
Count(n-2,new String[]{s[1],s[0]});
Count(n-4,new String[]{s[1],s[0]});
}
}
}

public static void main(String[] args)
{
String[] order = new String[]{"Alice","Bob"};
Count(5,order);
}
}
Output:

Alice Alice

Add a comment
Know the answer?
Add Answer to:
USING RECURSIONN!!! JAVA CODE Coin game: Alice and Bob are playing a game using a bunch...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • Design an O(n)-time non-losing strategy for the first player, Alice, in the coins in-a-line game. Your...

    Design an O(n)-time non-losing strategy for the first player, Alice, in the coins in-a-line game. Your strategy does not have to be optimal, but it should be guaranteed to end in a tie or better for Alice. Coins in a Line 12.4.1 The first game we consider is reported to arise in a problem that is sometimes asked during job interviews at major software and Internet companics (probably because it is so tempting to apply a greedy strategy to this...

  • Develop a game using Matlab. Work in groups of two. A Game of Sticks The rules...

    Develop a game using Matlab. Work in groups of two. A Game of Sticks The rules are as follows: Players: 2 Sticks: 20 Players take turns sequentially to pick 1~3 sticks. Loser picks up the last stick! This project has the following deliverables: (1) Your project implements a 2-player version of the game of sticks in the command window. Required display in command window: ‘Welcome to the game!’ ‘Please enter player 1 name:’ ‘Please enter player 1 name:’ ‘Player 1...

  • python code( using functions please)! Rules of the game: - This game requires a dice. The...

    python code( using functions please)! Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 =...

  • Alice has two coins. The probability of Heads for the first coin is 1/4, and the...

    Alice has two coins. The probability of Heads for the first coin is 1/4, and the probability of Heads for the second is 3/4. Other than this difference, the coins are indistinguishable. Alice chooses one of the coins at random and sends it to Bob. The random selection used by Alice to pick the coin to send to Bob is such that the first coin has a probability p of being selected. Assume that 0<p<1. Bob tries to guess which...

  • Problem 1. (20 points) Consider a game with two players, Alice and Bob. Alice can choose...

    Problem 1. (20 points) Consider a game with two players, Alice and Bob. Alice can choose A or B. The game ends if she chooses A while it continues to Bob if she chooses B. Bob then can choose C or D. If he chooses C the game ends, and if he chooses D the game continues to Alice. Finally, Alice can choose E or F and the game ends after each of these choices. a. Present this game as...

  • Problem 2.(20 points) Consider the following game: In the first step, Alice has two $10 bills...

    Problem 2.(20 points) Consider the following game: In the first step, Alice has two $10 bills and can take one of the following two actions: (i) she can give S20 to Bob or (ii) she can give one of the S10 bills to Bob. All the money will be used to buy popcorns before the movie they will see. Each one dollar of popcorn gives one unit of payoff for the player who buys it. In the second step, they...

  • Problem 2.(20 points) Consider the following game: In the first step, Alice has two $10 bills...

    Problem 2.(20 points) Consider the following game: In the first step, Alice has two $10 bills and can take one of the following two actions: (i) she can give S20 to Bob or (ii) she can give one of the S10 bills to Bob. All the money will be used to buy popcorns before the movie they will see. Each one dollar of popcorn gives one unit of payoff for the player who buys it. In the second step, they...

  • I need help figuring out how to code this in C++ in Visual Studio. Problem Statement:...

    I need help figuring out how to code this in C++ in Visual Studio. Problem Statement: Open the rule document for the game LCR Rules Dice Game. Develop the code, use commenting to describe your code and practice debugging if you run into errors. Submit source code. Use the document to write rules for the user on how to play the game in a text file. Then, using the information from the resource articles, create a program that will read...

  • Suppose, we have 3 players and they are playing a card game. In the card game,...

    Suppose, we have 3 players and they are playing a card game. In the card game, the deck contains 10 cards where there are 3 kings, 3 aces, and 4 jacks. Each player gets a card from the deck randomly. The winner of the game is determined based on the strength of the card where a king has 10 strength, an ace has 15 strength and a jack has 20 strength. Your task is to write a function called playgame()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT