Your challenge is to implement the game described here. Where the description refers to picking a number, it means the computer should pick a random number between 1 and 10.
The computer picks one number for the dealer, and picks two numbers for the player. All these numbers are displayed to the player.
The player can ask for the computer to pick more numbers for the player, one at a time, but if the sum of their numbers goes over 21 then they have lost the game.
Once the player is happy with their set of numbers, the computer will pick numbers for the dealer until the sum of its numbers is 16 or higher.
If the dealer's numbers sum to more than 21, the player wins the game. Otherwise, the highest-scoring set of numbers wins the game.
Basically, I don't know if I understand the question correctly. I think this problem is questionable.
As player cannot predict the future pick and cannot revert his last pick, what is the stop-pick time when the player is happy? he can just know the dealer's price is always over 16, so if the initial pick is below 16, he chooses to pick more numbers. After his numbers sum is over 16, how does he do? If aggressive, he will continue to pick. or if conservative, he will stop and be happy.
So the problem is questionable!!
updated: I'm sure I misunderstand the problem. I need to let the player himself determine if he is happy. So I thought my code should do that intelligently for him. That's impossible. So I get an input in my code now.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class PlayGame {
private Random random = new Random();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private int dealer_sum = 0;
private int player_sum = 0;
private static final String WON = "won";
private static final String LOST = "lost";
private static final String EVEN = "even";
public static void main(String[] args) throws Exception {
String result = new PlayGame().play();
System.out.println("-----final result------");
System.out.println(result);
}
public String play() throws Exception {
System.out.println("-----inital round------");
dealer_pick();
player_pick();
player_pick();
System.out.println("-----player round------");
while(true) {
System.out.println("Are you happy now? ");
if("n".equals(br.readLine())) {
player_pick();
if(player_sum > 21) return LOST;
continue;
}
break;
}
if(player_sum > 21) return LOST;
System.out.println("-----dealer round------");
while(dealer_sum < 16) {
dealer_pick();
}
if(dealer_sum > 21) return WON;
if(player_sum == dealer_sum) return EVEN;
return (player_sum > dealer_sum) ? WON : LOST;
}
private int player_pick() {
int num = pick();
player_sum += num;
System.out.println("player: " + num + " => " + player_sum);
return num;
}
private void dealer_pick() {
int num = pick();
dealer_sum += num;
System.out.println("dealer: " + num + " => " + dealer_sum);
}
private int pick() {
return random.nextInt(10) + 1;
}
}