src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 17:22:00 +0400
changeset 86 22bad86100ac
parent 76 5955cb5e5ef3
child 88 9555608af847
permissions -rw-r--r--
Merge with 5940c7605d039b10b1bfeda7d971be29f03987e2
     1 package ru.alphatech.icfpc.game;
     2 
     3 import java.util.ArrayList;
     4 
     5 import ru.alphatech.icfpc.cards.Attack;
     6 import ru.alphatech.icfpc.cards.Dbl;
     7 import ru.alphatech.icfpc.cards.Get;
     8 import ru.alphatech.icfpc.cards.Help;
     9 import ru.alphatech.icfpc.cards.ICard;
    10 import ru.alphatech.icfpc.cards.K;
    11 import ru.alphatech.icfpc.cards.Put;
    12 import ru.alphatech.icfpc.cards.S;
    13 import ru.alphatech.icfpc.cards.Succ;
    14 import ru.alphatech.icfpc.cards.Zero;
    15 import ru.alphatech.icfpc.game.Batch.OperationType;
    16 
    17 /**
    18  * 
    19  * TODO 
    20  * 
    21  * @author akhmetovlz
    22  *
    23  */
    24 public abstract class AbstractPolicy {
    25 
    26 	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    27 	
    28 	public AbstractPolicy(){
    29 	}	
    30 	
    31 	public void setRegister(int registerIndex, int num ){
    32 		if( registerIndex < 4) {
    33 			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
    34 			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    35 		}
    36 	}
    37 	
    38 	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    39 	{	
    40 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    41 		
    42 		IValue targetValue = targetSlot.getValue();
    43 		
    44 		if(targetValue instanceof ICard){
    45 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    46 		}
    47 		
    48 		Value numTargetValue = (Value) targetValue;
    49 		if( numTargetValue.getValue() == number ){
    50 			return retArray;
    51 		}else
    52 		if( numTargetValue.getValue() > number ){
    53 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    54 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    55 			stupidAlg(0, number, targetSlot);
    56 		}else{
    57 			stupidAlg(numTargetValue.getValue(), number, targetSlot);
    58 		}
    59 		
    60 		return retArray;
    61 	}
    62 	
    63 	
    64 	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    65 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    66 		while( begin*2 < end){
    67 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    68 			begin *= 2;
    69 		}
    70 		while( begin != end){
    71 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    72 			begin ++;
    73 		}
    74 		return retArray;
    75 	}
    76 	
    77 	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
    78 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    79 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    80 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    81 		while(end >= 2){
    82 			int d = end % 2;
    83 			if( d == 0 ) {
    84 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    85 			} else {
    86 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    87 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    88 			}
    89 			end = end / 2; 
    90 		}
    91 		return retArray;
    92 	}
    93 	
    94 	public abstract ArrayList<Batch> getBatches();
    95 }