游戏开始有一堆硬币,共13枚。在每一轮,每个玩家要从这堆硬币中拿走1、2或3枚,将它们放到一边。游戏的目标是避免拿到最后一枚硬币。
人机对弈程序,机器后拿。祝你好运
import java.util.Scanner;
public class Nim {
public void run() {
printInstructions();
nCoins = STARTING_COINS;
currentPlayer = STARTING_PLAYER;
while (nCoins > 1) {
System.out.println("There are " + nCoins + " coins in the pile.");
if (currentPlayer == Player.HUMAN) {
nCoins -= getUserMove();
} else {
int nTaken = getComputerMove();
System.out.println("I'll take " + nTaken + ".");
nCoins -= nTaken;
}
switchPlayer();
}
announceResult();
}
private int getUserMove() {
int limit = (nCoins < MAX_MOVE) ? nCoins : MAX_MOVE;
while (true) {
System.out.print("How many would you like? ");
Scanner input = new Scanner(System.in);
int nTaken = input.nextInt();
if (nTaken > 0 && nTaken <= limit) return nTaken;
System.out.println("That's cheating! Please choose " +
"between 1 and " + limit + ".");
System.out.println("There are " + nCoins + " coins int the pile.");
}
}
private int getComputerMove() {
int nTaken = findGoodMove(nCoins);
return (nTaken == NO_GOOD_MOVE) ? 1 : nTaken;
}
private int findGoodMove(int nCoins) {
int limit = (nCoins < MAX_MOVE) ? nCoins : MAX_MOVE;
for (int nTaken = 1; nTaken <= limit; nTaken++) {
if (isBadPosition(nCoins - nTaken)) return nTaken;
}
return NO_GOOD_MOVE;
}
private boolean isBadPosition(int nCoins) {
if (nCoins == 1) return true;
return findGoodMove(nCoins) == NO_GOOD_MOVE;
}
private void switchPlayer() {
currentPlayer = (currentPlayer == Player.HUMAN) ? Player.COMPUTER : Player.HUMAN;
}
private void printInstructions() {
System.out.println("Welcome to the game of Nim!");
System.out.println("In this game, we will start with a pile of " +
STARTING_COINS + " coins on the table.");
System.out.println("On each turn, you and I will alternately take " +
"between 1 and " + MAX_MOVE + " coins");
System.out.println("from the table. The player who takes the " +
"last coin loses.\n");
}
private void announceResult() {
if (nCoins == 0) {
System.out.println("You took the last coin. You lose.");
} else {
System.out.println("There is only one coin left.");
if (currentPlayer == Player.HUMAN) {
System.out.println("I win.");
} else {
System.out.println("I lose.");
}
}
}
private static final int MAX_MOVE = 3;
private static final int NO_GOOD_MOVE = -1;
private static final int STARTING_COINS = 13;
private static final Player STARTING_PLAYER = Player.HUMAN;
private int nCoins;
private Player currentPlayer;
public static void main(String[] args) {
new Nim().run();
}
}
参考《Programming Abstractions in Java》