动态规划案例——Nim游戏

游戏开始有一堆硬币,共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》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值