src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author rushan
Sun, 19 Jun 2011 17:30:18 +0400
changeset 87 0f4c762f56f7
parent 62 3252fb664a39
child 88 9555608af847
permissions -rw-r--r--
догонка числа
     1 package ru.alphatech.icfpc.game;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.List;
     6 
     7 import ru.alphatech.icfpc.cards.Attack;
     8 import ru.alphatech.icfpc.cards.Dbl;
     9 import ru.alphatech.icfpc.cards.Get;
    10 import ru.alphatech.icfpc.cards.Help;
    11 import ru.alphatech.icfpc.cards.ICard;
    12 import ru.alphatech.icfpc.cards.K;
    13 import ru.alphatech.icfpc.cards.Put;
    14 import ru.alphatech.icfpc.cards.S;
    15 import ru.alphatech.icfpc.cards.Succ;
    16 import ru.alphatech.icfpc.cards.Zero;
    17 import ru.alphatech.icfpc.game.Batch.OperationType;
    18 
    19 /**
    20  * 
    21  * TODO 
    22  * 
    23  * @author akhmetovlz
    24  *
    25  */
    26 public abstract class AbstractPolicy {
    27 
    28 	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    29 	
    30 	public AbstractPolicy(){
    31 	}	
    32 	
    33 	public void setRegister(int registerIndex, int num ){
    34 		if( registerIndex < 4) {
    35 			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
    36 			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    37 		}
    38 	}
    39 	
    40 	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    41 	{	
    42 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    43 		
    44 		IValue targetValue = targetSlot.getValue();
    45 		
    46 		if(targetValue instanceof ICard){
    47 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    48 		}
    49 		
    50 		Value numTargetValue = (Value) targetValue;
    51 		if( numTargetValue.getValue() == number ){
    52 			return retArray;
    53 		}else
    54 		if( numTargetValue.getValue() > number ){
    55 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    56 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    57 			stupidAlg(0, number, targetSlot);
    58 		}else{
    59 			stupidAlg(numTargetValue.getValue(), number, targetSlot);
    60 		}
    61 		
    62 		return retArray;
    63 	}
    64 	
    65 	
    66 	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    67 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    68 		while( begin*2 < end){
    69 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    70 			begin *= 2;
    71 		}
    72 		while( begin != end){
    73 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    74 			begin ++;
    75 		}
    76 		return retArray;
    77 	}
    78 	
    79 	protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
    80 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    81 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    82 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    83 		
    84 		List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
    85 		List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
    86 		if (list1.size() <= list2.size())
    87 		{
    88 			Collections.reverse(list1);
    89 			retArray.addAll(list1);
    90 		}
    91 		else
    92 		{
    93 			Collections.reverse(list2);
    94 			retArray.addAll(list2);
    95 		}
    96 		return retArray;
    97 	}
    98 	
    99 	protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
   100 		List<Batch> list = new ArrayList<Batch>();
   101 		
   102 		while(end >= 2){
   103 			int d = end % 2;
   104 			if (begin > 0 && 2*begin > end)
   105 			{
   106 				for (int i = 0; i < end - begin; i++)
   107 				{
   108 					list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   109 				}
   110 				break;
   111 			}
   112 			if( d == 0 ) {
   113 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   114 				end = end / 2;
   115 			} else {
   116 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   117 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   118 				end = (end-1) / 2;
   119 			}
   120 		}
   121 		return list;
   122 	}
   123 	
   124 	protected ArrayList<Batch> BcombineForFunctWith3ParamsInRegister(ICard function, Slot processingSlot){
   125 		ArrayList<Batch> retArray = new ArrayList<Batch>();
   126 		
   127 		if(!(function instanceof Attack || function instanceof Help)){
   128 			throw new ICFPCException();
   129 		}
   130 		
   131 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
   132 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, function,  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 Get(), 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 Zero(), 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 Get(), processingSlot));
   148 
   149 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   150 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   151 		
   152 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   153 		
   154 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   155 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   156 		
   157 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   158 		
   159 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   160 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   161 		
   162 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
   163 
   164 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   165 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   166 		
   167 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   168 		
   169 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   170 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   171 		
   172 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   173 		
   174 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   175 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   176 		
   177 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   178 		
   179 		return retArray;
   180 	}
   181 	
   182 	public abstract ArrayList<Batch> getBatches();
   183 	
   184 	public static void main(String[] args)
   185 	{
   186 		Slot slot = new Slot();
   187 		List<Batch> list = smartAlg(5, 12, slot);
   188 		System.out.println(list.toArray().toString());
   189 	}
   190 }