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