src/main/java/ru/alphatech/icfpc/policy/OneArgumentCardsPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 17:21:54 +0400
changeset 85 7292b7aea7e3
parent 84 f247a56bde48
permissions -rw-r--r--
(no commit message)
     1 package ru.alphatech.icfpc.policy;
     2 
     3 import java.util.ArrayList;
     4 
     5 import ru.alphatech.icfpc.cards.Copy;
     6 import ru.alphatech.icfpc.cards.Dec;
     7 import ru.alphatech.icfpc.cards.Get;
     8 import ru.alphatech.icfpc.cards.ICard;
     9 import ru.alphatech.icfpc.cards.Inc;
    10 import ru.alphatech.icfpc.cards.K;
    11 import ru.alphatech.icfpc.cards.Put;
    12 import ru.alphatech.icfpc.cards.Revive;
    13 import ru.alphatech.icfpc.cards.S;
    14 import ru.alphatech.icfpc.cards.Zero;
    15 import ru.alphatech.icfpc.game.AbstractPolicy;
    16 import ru.alphatech.icfpc.game.Batch;
    17 import ru.alphatech.icfpc.game.IValue;
    18 import ru.alphatech.icfpc.game.Run;
    19 import ru.alphatech.icfpc.game.Slot;
    20 import ru.alphatech.icfpc.game.Value;
    21 import ru.alphatech.icfpc.game.Batch.OperationType;
    22 
    23 public class OneArgumentCardsPolicy extends AbstractPolicy{
    24 	
    25 	public enum TYPE {
    26 		DEC,
    27 		INC,
    28 		COPY,
    29 		REVIVE
    30 	}
    31 	
    32 	private TYPE type;
    33 	private int processingSlotIndex = -1;
    34 	private int registerIndex = -1;
    35 	private int targetSlotIndex = -1;
    36 	
    37 	
    38 	public OneArgumentCardsPolicy(TYPE type, int processingSlot, int target, int registerIndex) {
    39 		this.type = type;
    40 		this.processingSlotIndex = processingSlot;
    41 		this.targetSlotIndex = target;
    42 		this.registerIndex = registerIndex;
    43 	}
    44 	
    45 	public ICard getCard(){
    46 		switch (type) {
    47 			case DEC:
    48 			return new Dec();
    49 			case INC:
    50 			return new Inc();
    51 			case COPY:
    52 			return new Copy();
    53 			case REVIVE:
    54 			return new Revive();
    55 		}
    56 		return null;
    57 	}
    58 	
    59 	protected boolean isEqualsCard(IValue value){
    60 		if(value instanceof IValue)
    61 			return false;
    62 		if( type == TYPE.DEC && value instanceof Dec)
    63 			return true;
    64 		if( type == TYPE.INC && value instanceof Inc)
    65 			return true;
    66 		if( type == TYPE.COPY && value instanceof Copy)
    67 			return true;
    68 		if( type == TYPE.REVIVE && value instanceof Revive)
    69 			return true;
    70 		return false;
    71 	}
    72 
    73 	@Override
    74 	public ArrayList<Batch> getBatches() {
    75 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    76 		
    77 		retArray.addAll(numberIntoSlotField(Run.getRegisterByIndex(registerIndex), targetSlotIndex));
    78 		
    79 		Slot processingSlot = Run.getProponent().getSlot(processingSlotIndex);
    80 		IValue slotValue = processingSlot.getValue();
    81 		
    82 		if(slotValue instanceof Value){
    83 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
    84 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, getCard(), processingSlot));
    85 		}else{
    86 			ICard currentCard = (ICard) slotValue;
    87 			if(!(currentCard instanceof Dec && type == TYPE.DEC) || 
    88 			   !(currentCard instanceof Inc && type == TYPE.INC))
    89 			{
    90 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
    91 				retArray.add(new Batch(OperationType.SLOT_TO_CARD, getCard(), processingSlot));
    92 			}
    93 		}
    94 		
    95 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
    96 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
    97 		
    98 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
    99 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   100 		
   101 		return retArray;
   102 	}
   103 	
   104 }