src/main/java/ru/alphatech/icfpc/policy/OneArgumentCardsPolicy.java
changeset 88 9555608af847
parent 85 7292b7aea7e3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/java/ru/alphatech/icfpc/policy/OneArgumentCardsPolicy.java	Sun Jun 19 17:32:15 2011 +0400
     1.3 @@ -0,0 +1,104 @@
     1.4 +package ru.alphatech.icfpc.policy;
     1.5 +
     1.6 +import java.util.ArrayList;
     1.7 +
     1.8 +import ru.alphatech.icfpc.cards.Copy;
     1.9 +import ru.alphatech.icfpc.cards.Dec;
    1.10 +import ru.alphatech.icfpc.cards.Get;
    1.11 +import ru.alphatech.icfpc.cards.ICard;
    1.12 +import ru.alphatech.icfpc.cards.Inc;
    1.13 +import ru.alphatech.icfpc.cards.K;
    1.14 +import ru.alphatech.icfpc.cards.Put;
    1.15 +import ru.alphatech.icfpc.cards.Revive;
    1.16 +import ru.alphatech.icfpc.cards.S;
    1.17 +import ru.alphatech.icfpc.cards.Zero;
    1.18 +import ru.alphatech.icfpc.game.AbstractPolicy;
    1.19 +import ru.alphatech.icfpc.game.Batch;
    1.20 +import ru.alphatech.icfpc.game.IValue;
    1.21 +import ru.alphatech.icfpc.game.Run;
    1.22 +import ru.alphatech.icfpc.game.Slot;
    1.23 +import ru.alphatech.icfpc.game.Value;
    1.24 +import ru.alphatech.icfpc.game.Batch.OperationType;
    1.25 +
    1.26 +public class OneArgumentCardsPolicy extends AbstractPolicy{
    1.27 +	
    1.28 +	public enum TYPE {
    1.29 +		DEC,
    1.30 +		INC,
    1.31 +		COPY,
    1.32 +		REVIVE
    1.33 +	}
    1.34 +	
    1.35 +	private TYPE type;
    1.36 +	private int processingSlotIndex = -1;
    1.37 +	private int registerIndex = -1;
    1.38 +	private int targetSlotIndex = -1;
    1.39 +	
    1.40 +	
    1.41 +	public OneArgumentCardsPolicy(TYPE type, int processingSlot, int target, int registerIndex) {
    1.42 +		this.type = type;
    1.43 +		this.processingSlotIndex = processingSlot;
    1.44 +		this.targetSlotIndex = target;
    1.45 +		this.registerIndex = registerIndex;
    1.46 +	}
    1.47 +	
    1.48 +	public ICard getCard(){
    1.49 +		switch (type) {
    1.50 +			case DEC:
    1.51 +			return new Dec();
    1.52 +			case INC:
    1.53 +			return new Inc();
    1.54 +			case COPY:
    1.55 +			return new Copy();
    1.56 +			case REVIVE:
    1.57 +			return new Revive();
    1.58 +		}
    1.59 +		return null;
    1.60 +	}
    1.61 +	
    1.62 +	protected boolean isEqualsCard(IValue value){
    1.63 +		if(value instanceof IValue)
    1.64 +			return false;
    1.65 +		if( type == TYPE.DEC && value instanceof Dec)
    1.66 +			return true;
    1.67 +		if( type == TYPE.INC && value instanceof Inc)
    1.68 +			return true;
    1.69 +		if( type == TYPE.COPY && value instanceof Copy)
    1.70 +			return true;
    1.71 +		if( type == TYPE.REVIVE && value instanceof Revive)
    1.72 +			return true;
    1.73 +		return false;
    1.74 +	}
    1.75 +
    1.76 +	@Override
    1.77 +	public ArrayList<Batch> getBatches() {
    1.78 +		ArrayList<Batch> retArray = new ArrayList<Batch>();
    1.79 +		
    1.80 +		retArray.addAll(numberIntoSlotField(Run.getRegisterByIndex(registerIndex), targetSlotIndex));
    1.81 +		
    1.82 +		Slot processingSlot = Run.getProponent().getSlot(processingSlotIndex);
    1.83 +		IValue slotValue = processingSlot.getValue();
    1.84 +		
    1.85 +		if(slotValue instanceof Value){
    1.86 +			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
    1.87 +			retArray.add(new Batch(OperationType.SLOT_TO_CARD, getCard(), processingSlot));
    1.88 +		}else{
    1.89 +			ICard currentCard = (ICard) slotValue;
    1.90 +			if(!(currentCard instanceof Dec && type == TYPE.DEC) || 
    1.91 +			   !(currentCard instanceof Inc && type == TYPE.INC))
    1.92 +			{
    1.93 +				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
    1.94 +				retArray.add(new Batch(OperationType.SLOT_TO_CARD, getCard(), processingSlot));
    1.95 +			}
    1.96 +		}
    1.97 +		
    1.98 +		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
    1.99 +		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   1.100 +		
   1.101 +		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
   1.102 +		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   1.103 +		
   1.104 +		return retArray;
   1.105 +	}
   1.106 +	
   1.107 +}