src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 14:29:07 +0400
changeset 62 3252fb664a39
parent 52 eea6fcf60b73
child 76 5955cb5e5ef3
child 87 0f4c762f56f7
permissions -rw-r--r--
Мего метод.
     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 	protected ArrayList<Batch> BcombineForFunctWith3ParamsInRegister(ICard function, Slot processingSlot){
    95 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    96 		
    97 		if(!(function instanceof Attack || function instanceof Help)){
    98 			throw new ICFPCException();
    99 		}
   100 		
   101 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
   102 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, function,  processingSlot));
   103 		
   104 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   105 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   106 		
   107 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
   108 
   109 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   110 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   111 		
   112 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   113 		
   114 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   115 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   116 		
   117 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
   118 
   119 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   120 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   121 		
   122 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   123 		
   124 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   125 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   126 		
   127 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   128 		
   129 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   130 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   131 		
   132 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
   133 
   134 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   135 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   136 		
   137 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   138 		
   139 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   140 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   141 		
   142 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   143 		
   144 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   145 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   146 		
   147 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   148 		
   149 		return retArray;
   150 	}
   151 	
   152 	public abstract ArrayList<Batch> getBatches();
   153 }