日期:2014-05-20 浏览次数:20723 次
import java.util.Scanner;
/**
* @author Android_iPhone@CSDN
*/
public class TakeApples {
/**
* @param args Command-Line Arguments
*/
public static void main(final String[] args) {
System.out.print("Please input total number of apples:");
leftNumberOfApples = DEFAULT_VALUE_0;
do {
try {
leftNumberOfApples = (new Scanner(System.in)).nextInt();
} catch (Exception e) {
leftNumberOfApples = DEFAULT_VALUE_0;
}
} while (leftNumberOfApples <= DEFAULT_VALUE_0);
gameStart(randomBoolean());
} // main
/**
* @param youTakeFirst [youTakeFirst(true)] or [computerTakeFirst(false)]
* @return [COMPUTER_WIN(true)] or [YOU_WIN(false)]
*/
public static boolean gameStart(final boolean youTakeFirst) {
boolean yourTurn = youTakeFirst;
do {
if (!gameOver()) {
if (yourTurn) { youTake(); }
} else {
System.out.println("You Win!");
return YOU_WIN;
}
if (!gameOver()) {
computerTake();
yourTurn = true;
} else {
System.out.println("Computer Win!");
return COMPUTER_WIN;
}
} while(true);
} // gameStart
/**
* @return [GameOver(true)] or [GameIsNotOver(false)]
*/
private static boolean gameOver() {
return (leftNumberOfApples <= DEFAULT_VALUE_0);
} // gameOver
/***/
private static void youTake() {
System.out.print("Please input number of apples you take:");
int takeNumberOfApples = DEFAULT_VALUE_0;
do {
try {
if (debugMode) {
takeNumberOfApples = randomInt(
RANDOM_LOWER_BOUND,
RANDOM_UPPER_BOUND);
} else {
takeNumberOfApples = (new Scanner(System.in)).nextInt();