标签:
书名《游戏人工智能编程》作者mat bucklandpublic abstract class State {
	
	
	abstract public void exec(Enity body);
	
}
file2(onminejava)
import java.util.Random;
public class OnMine extends State {
	
	private static OnMine OnlyOne=null;
	Random gener=new Random();
	private OnMine()
	{
		
	}
	
	public static  OnMine getInstance()
	{
		if(OnlyOne==null)
		{
			OnlyOne=new OnMine();
		}
		return OnlyOne;
	}
	
	@Override
	public void exec(Enity body) {
		// TODO Auto-generated method stub
		
		if(body instanceof Man)
		{
			Man toOperate=(Man) body;
			if(toOperate.getState()!=OnMine.getInstance())
			{
				System.out.println("The man walk to mine");
				toOperate.changeState(OnMine.getInstance());
				
			}
			if(toOperate.getCurrentPower()==0)
			{
				System.out.println("man's pow is too low,he should sleep");
				toOperate.changeState(Tired.getInstance());
				return;
			}
			//work to get gold has 1/3 property to get the gold
			if(gener.nextInt(3)==2)
			{
				int getGold=gener.nextInt(toOperate.getMaxCapacity());
				if(toOperate.getCurrentCapacity()+getGold>toOperate.getMaxCapacity())
				{
					System.out.println("Gread have find gold but too much("+getGold+"), man should go to store");
					toOperate.addToCurrentCapacity(toOperate.getMaxCapacity()-toOperate.getCurrentCapacity());
					
				}
				else
				{
					
					toOperate.addToCurrentCapacity(getGold);
					System.out.println("Man get the gold "+getGold+"there captity is "+toOperate.getCurrentCapacity());
				}
				
				
			}
			else
			{
				System.out.println("man don't have good luck,nothing get");
			}
			toOperate.decreasePower();
			
			System.out.println("man have power:"+toOperate.getCurrentPower());
			if(toOperate.getCurrentPower()==0)
			{
				System.out.println("man's pow is too low,he should sleep");
				toOperate.changeState(Tired.getInstance());
				return;
			}
			if(toOperate.getCurrentCapacity()==toOperate.getMaxCapacity())
			{
				System.out.println("man's bag if full and he should go to change it");
				toOperate.changeState(BagFull.getInstance());
			}
			
		}
		
	}
}
file3(bagfull.java)
import java.util.Scanner;
public class BagFull extends State {
	private static BagFull OnlyOne=null;
	
	private BagFull()
	{
		
	}
	
	public static BagFull getInstance()
	{
		if(OnlyOne==null)
		{
			OnlyOne=new BagFull();
		}
		return OnlyOne;
	}
	@Override
	public void exec(Enity body) {
		// TODO Auto-generated method stub
		if(body instanceof Man)
		{
			Man toOperate=(Man) body;
			if(toOperate.getCurrentCapacity()!=toOperate.getMaxCapacity())
			{
				System.out.println("error in bagfull beacause bag is not full");
				new Scanner(System.in).nextLine();//system("pause")
			}
			
			System.out.println("man go to bank put the gold for money and he is very happy");	
			toOperate.addMoney(toOperate.getCurrentCapacity()*10);
			System.out.println("man have money"+toOperate.getMoney());
			toOperate.setCurrentCapacity(0);
			
			toOperate.decreasePower();
			
			
			if(toOperate.getState()!=OnMine.getInstance())
			{
				System.out.println("man should move to the mine");
				toOperate.changeState(OnMine.getInstance());
			}
		}
		
	}
	
}
import java.util.Scanner;
public class Tired extends State {
	
	private static Tired OnlyOne=null;
	private Tired()
	{
		
	}
	
	public static Tired getInstance()
	{
		if(OnlyOne==null)
		{
			OnlyOne=new Tired();
		}
		return OnlyOne;
	}
	
	@Override
	public void exec(Enity body) {
		// TODO Auto-generated method stub
		if(body instanceof Man)
		{
			Man toOperate=(Man) body;
			if(toOperate.getCurrentPower()!=0)
			{
				System.out.println("there is some problem in Tired currentpow is not 0");
				new Scanner(System.in).nextLine();//system("pause")		
			}
			System.out.println("man is tired and should sleep a while");
			toOperate.recorverPower();
			if(toOperate.getCurrentCapacity()==toOperate.getMaxCapacity())
			{
				System.out.println("man find his bag is full after sleep,he should go to store it ");
				toOperate.changeState(BagFull.getInstance());
			}
			else
			{
				System.out.println("man is ready .and go to mine");
				toOperate.changeState(OnMine.getInstance());
			}
		}
	}
	
}
public abstract class Enity {
}
public class Man extends Enity {
	private int maxCapacity;
	private int currentCapacity;
	private int maxPower;
	private int currentPower;
	private int money;
	private State preState;
	private State state;
	
	
	public Man()
	{
		maxCapacity=100;
		currentCapacity=0;
		maxPower=10;
		currentPower=maxPower;
		money=0;
		preState=null;
		state=OnMine.getInstance();
	}
	public void update()
	{
		state.exec(this);
		
	}
	public void changeState(State toState)
	{
		preState=state;
		state=toState;
	}
	
	public State getState()
	{
		return state;
	}
	
	public int getCurrentCapacity()
	{
		return currentCapacity;
	}
	public int getMaxCapacity()
	{
		return maxCapacity;
	}
	
	public void addToCurrentCapacity(int src)
	{
		if(src<0||currentCapacity+src>maxCapacity)
		{
			System.out.println("addtoCurrentcapacity error src negative or capacity overflow");
		}
		this.currentCapacity+=src;
	}
	
	public void setCurrentCapacity(int src)
	{
		currentCapacity=src;
	}
	
	public void decreasePower()
	{
		this.currentPower--;
		if(currentPower==0)
		{
			changeState(Tired.getInstance());
		}
		
	}
	
	public int getMoney()
	{
		return money;
	}
	
	public void addMoney(int src)
	{
		money+=src;
	}
	
	public void setMoney(int src)
	{
		money=src;
	}
	
	public int getCurrentPower()
	{
		return currentPower;
	}
	
	
	public void recorverPower()
	{
		currentPower=maxPower;
	}
}
在编程的过程中,我发现一个问题就是这样的编程虽然便于状态的扩展,但是每个状态的逻辑非常复杂,比如在每个状态减掉体力值后,一定要首先判断体力值是不是0,然后才能继续判断其他的事情。我想到的改进方法就是把所有状态改变都放到一个控制类里面,由控制类完成状态的改变。这样可以避免多次判断体力值的问题。我不知道作者在之后会不会改进这个类。为了方便大家,我把这个游戏的代码也放到了gitoschina上。地址在这里点击打开链接
标签:
原文地址:http://blog.csdn.net/bleuesprit/article/details/45010921