src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author rushan
Sun, 19 Jun 2011 17:32:15 +0400
changeset 88 9555608af847
parent 87 0f4c762f56f7
parent 76 5955cb5e5ef3
child 89 b6543dcda026
permissions -rw-r--r--
Merge with 22bad86100ac86a8164a6c6925086cfda905d765
     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 	public abstract ArrayList<Batch> getBatches();
   125 	
   126 	public static void main(String[] args)
   127 	{
   128 		Slot slot = new Slot();
   129 		List<Batch> list = smartAlg(5, 12, slot);
   130 		System.out.println(list.toArray().toString());
   131 	}
   132 }