src/main/java/ru/alphatech/icfpc/policy/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 12:25:06 +0400
changeset 38 d727078107f4
parent 32 04ac70718172
child 41 c5d25e56aafc
permissions -rw-r--r--
AbstractPolicy
     1 package ru.alphatech.icfpc.policy;
     2 
     3 import java.util.ArrayList;
     4 
     5 import ru.alphatech.icfpc.cards.Dbl;
     6 import ru.alphatech.icfpc.cards.ICard;
     7 import ru.alphatech.icfpc.cards.Put;
     8 import ru.alphatech.icfpc.cards.Succ;
     9 import ru.alphatech.icfpc.cards.Zero;
    10 import ru.alphatech.icfpc.game.Batch;
    11 import ru.alphatech.icfpc.game.IValue;
    12 import ru.alphatech.icfpc.game.Player;
    13 import ru.alphatech.icfpc.game.Slot;
    14 import ru.alphatech.icfpc.game.Value;
    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 Player player = null;
    27 	
    28 	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    29 	
    30 	public AbstractPolicy(Player player){
    31 		this.player = player;
    32 	}	
    33 	
    34 	public void setRegister(int registerIndex, int num ){
    35 		if( registerIndex < 4) {
    36 			Slot registerSlot = player.getRegisterByIndex(registerIndex);
    37 			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    38 		}
    39 	}
    40 	
    41 	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    42 	{	
    43 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    44 		
    45 		IValue targetValue = targetSlot.getValue();
    46 		
    47 		if(targetValue instanceof ICard){
    48 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    49 		}
    50 		
    51 		Value numTargetValue = (Value) targetValue;
    52 		if( numTargetValue.getValue() == number ){
    53 			return retArray;
    54 		}else
    55 		if( numTargetValue.getValue() > number ){
    56 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    57 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    58 			stupidAlg(0, number, targetSlot);
    59 		}else{
    60 			stupidAlg(numTargetValue.getValue(), number, targetSlot);
    61 		}
    62 		
    63 		return retArray;
    64 	}
    65 	
    66 	
    67 	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    68 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    69 		while( begin*2 < end){
    70 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    71 			begin *= 2;
    72 		}
    73 		while( begin != end){
    74 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    75 			begin ++;
    76 		}
    77 		return retArray;
    78 	}
    79 	
    80 	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
    81 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    82 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    83 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    84 		while(end >= 2){
    85 			int d = end % 2;
    86 			if( d == 0 ) {
    87 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    88 			} else {
    89 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    90 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    91 			}
    92 			end = end / 2; 
    93 		}
    94 		return retArray;
    95 	}
    96 }