src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 19:13:42 +0400
changeset 101 4bb458c3548e
parent 89 b6543dcda026
child 115 21f9fa7770cc
permissions -rw-r--r--
(no commit message)
     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.Dbl;
     8 import ru.alphatech.icfpc.cards.ICard;
     9 import ru.alphatech.icfpc.cards.Put;
    10 import ru.alphatech.icfpc.cards.Succ;
    11 import ru.alphatech.icfpc.cards.Zero;
    12 import ru.alphatech.icfpc.game.Batch.OperationType;
    13 
    14 /**
    15  * 
    16  * TODO 
    17  * 
    18  * @author akhmetovlz
    19  *
    20  */
    21 public abstract class AbstractPolicy {
    22 
    23 	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    24 	
    25 	public AbstractPolicy(){
    26 	}	
    27 	
    28 	public void setRegister(int registerIndex, int num ){
    29 		if( registerIndex < 4) {
    30 			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
    31 			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    32 		}
    33 	}
    34 	
    35 	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    36 	{	
    37 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    38 		
    39 		IValue targetValue = targetSlot.getValue();
    40 		
    41 		if(targetValue instanceof ICard){
    42 			retArray.addAll(smartAlg(0, number, targetSlot));
    43 		}else{
    44 			Value numTargetValue = (Value) targetValue;
    45 			if( numTargetValue.getValue() == number ){
    46 				return retArray;
    47 			}else
    48 			if( numTargetValue.getValue() > number ){
    49 				retArray.addAll( smartAlg(0, number, targetSlot) );
    50 			}else{
    51 				retArray.addAll( smartAlg(numTargetValue.getValue(), number, targetSlot) );
    52 			}
    53 		}
    54 		
    55 		return retArray;
    56 	}
    57 	
    58 	
    59 	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    60 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    61 		while( begin*2 < end){
    62 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    63 			begin *= 2;
    64 		}
    65 		while( begin != end){
    66 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    67 			begin ++;
    68 		}
    69 		return retArray;
    70 	}
    71 	
    72 	protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
    73 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    74 		
    75 		if(begin == 0){
    76 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    77 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    78 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    79 		}
    80 		
    81 		List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
    82 		List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
    83 		if (list1.size() <= list2.size())
    84 		{
    85 			Collections.reverse(list1);
    86 			retArray.addAll(list1);
    87 		}
    88 		else
    89 		{
    90 			Collections.reverse(list2);
    91 			retArray.addAll(list2);
    92 		}
    93 		return retArray;
    94 	}
    95 	
    96 	protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
    97 		List<Batch> list = new ArrayList<Batch>();
    98 		
    99 		while(end >= 2){
   100 			int d = end % 2;
   101 			if (begin > 0 && 2*begin > end)
   102 			{
   103 				for (int i = 0; i < end - begin; i++)
   104 				{
   105 					list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   106 				}
   107 				break;
   108 			}
   109 			if( d == 0 ) {
   110 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   111 				end = end / 2;
   112 			} else {
   113 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   114 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   115 				end = (end-1) / 2;
   116 			}
   117 		}
   118 		return list;
   119 	}
   120 	
   121 	public abstract ArrayList<Batch> getBatches();
   122 	
   123 	public static void main(String[] args)
   124 	{
   125 		Slot slot = new Slot(1);
   126 		List<Batch> list = smartAlg(0, 8, slot);
   127 		System.out.println(list.toArray().toString());
   128 	}
   129 }