src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 13:35:27 +0400
changeset 51 d93ce04e0f7b
child 52 eea6fcf60b73
permissions -rw-r--r--
(no commit message)
akhmetovlz@51
     1
package ru.alphatech.icfpc.policy;
akhmetovlz@51
     2
akhmetovlz@51
     3
import java.util.ArrayList;
akhmetovlz@51
     4
akhmetovlz@51
     5
import ru.alphatech.icfpc.cards.Dbl;
akhmetovlz@51
     6
import ru.alphatech.icfpc.cards.ICard;
akhmetovlz@51
     7
import ru.alphatech.icfpc.cards.Put;
akhmetovlz@51
     8
import ru.alphatech.icfpc.cards.Succ;
akhmetovlz@51
     9
import ru.alphatech.icfpc.cards.Zero;
akhmetovlz@51
    10
akhmetovlz@51
    11
/**
akhmetovlz@51
    12
 * 
akhmetovlz@51
    13
 * TODO 
akhmetovlz@51
    14
 * 
akhmetovlz@51
    15
 * @author akhmetovlz
akhmetovlz@51
    16
 *
akhmetovlz@51
    17
 */
akhmetovlz@51
    18
public abstract class AbstractPolicy {
akhmetovlz@51
    19
akhmetovlz@51
    20
	protected ArrayList<Batch> batches = new ArrayList<Batch>();
akhmetovlz@51
    21
	
akhmetovlz@51
    22
	public AbstractPolicy(){
akhmetovlz@51
    23
	}	
akhmetovlz@51
    24
	
akhmetovlz@51
    25
	public void setRegister(int registerIndex, int num ){
akhmetovlz@51
    26
		if( registerIndex < 4) {
akhmetovlz@51
    27
			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
akhmetovlz@51
    28
			batches.addAll( numberIntoSlotField( registerSlot, num ) );
akhmetovlz@51
    29
		}
akhmetovlz@51
    30
	}
akhmetovlz@51
    31
	
akhmetovlz@51
    32
	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
akhmetovlz@51
    33
	{	
akhmetovlz@51
    34
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@51
    35
		
akhmetovlz@51
    36
		IValue targetValue = targetSlot.getValue();
akhmetovlz@51
    37
		
akhmetovlz@51
    38
		if(targetValue instanceof ICard){
akhmetovlz@51
    39
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@51
    40
		}
akhmetovlz@51
    41
		
akhmetovlz@51
    42
		Value numTargetValue = (Value) targetValue;
akhmetovlz@51
    43
		if( numTargetValue.getValue() == number ){
akhmetovlz@51
    44
			return retArray;
akhmetovlz@51
    45
		}else
akhmetovlz@51
    46
		if( numTargetValue.getValue() > number ){
akhmetovlz@51
    47
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@51
    48
			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
akhmetovlz@51
    49
			stupidAlg(0, number, targetSlot);
akhmetovlz@51
    50
		}else{
akhmetovlz@51
    51
			stupidAlg(numTargetValue.getValue(), number, targetSlot);
akhmetovlz@51
    52
		}
akhmetovlz@51
    53
		
akhmetovlz@51
    54
		return retArray;
akhmetovlz@51
    55
	}
akhmetovlz@51
    56
	
akhmetovlz@51
    57
	
akhmetovlz@51
    58
	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
akhmetovlz@51
    59
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@51
    60
		while( begin*2 < end){
akhmetovlz@51
    61
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
akhmetovlz@51
    62
			begin *= 2;
akhmetovlz@51
    63
		}
akhmetovlz@51
    64
		while( begin != end){
akhmetovlz@51
    65
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
akhmetovlz@51
    66
			begin ++;
akhmetovlz@51
    67
		}
akhmetovlz@51
    68
		return retArray;
akhmetovlz@51
    69
	}
akhmetovlz@51
    70
	
akhmetovlz@51
    71
	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
akhmetovlz@51
    72
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@51
    73
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@51
    74
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
akhmetovlz@51
    75
		while(end >= 2){
akhmetovlz@51
    76
			int d = end % 2;
akhmetovlz@51
    77
			if( d == 0 ) {
akhmetovlz@51
    78
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
akhmetovlz@51
    79
			} else {
akhmetovlz@51
    80
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
akhmetovlz@51
    81
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
akhmetovlz@51
    82
			}
akhmetovlz@51
    83
			end = end / 2; 
akhmetovlz@51
    84
		}
akhmetovlz@51
    85
		return retArray;
akhmetovlz@51
    86
	}
akhmetovlz@51
    87
	
akhmetovlz@51
    88
	public abstract ArrayList<Batch> getBatches();
akhmetovlz@51
    89
}