src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 14:28:44 +0400
changeset 61 5587303ff52c
parent 52 eea6fcf60b73
child 62 3252fb664a39
permissions -rw-r--r--
Атака
akhmetovlz@52
     1
package ru.alphatech.icfpc.game;
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@52
    10
import ru.alphatech.icfpc.game.Batch.OperationType;
akhmetovlz@51
    11
akhmetovlz@51
    12
/**
akhmetovlz@51
    13
 * 
akhmetovlz@51
    14
 * TODO 
akhmetovlz@51
    15
 * 
akhmetovlz@51
    16
 * @author akhmetovlz
akhmetovlz@51
    17
 *
akhmetovlz@51
    18
 */
akhmetovlz@51
    19
public abstract class AbstractPolicy {
akhmetovlz@51
    20
akhmetovlz@51
    21
	protected ArrayList<Batch> batches = new ArrayList<Batch>();
akhmetovlz@51
    22
	
akhmetovlz@51
    23
	public AbstractPolicy(){
akhmetovlz@51
    24
	}	
akhmetovlz@51
    25
	
akhmetovlz@51
    26
	public void setRegister(int registerIndex, int num ){
akhmetovlz@51
    27
		if( registerIndex < 4) {
akhmetovlz@51
    28
			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
akhmetovlz@51
    29
			batches.addAll( numberIntoSlotField( registerSlot, num ) );
akhmetovlz@51
    30
		}
akhmetovlz@51
    31
	}
akhmetovlz@51
    32
	
akhmetovlz@51
    33
	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
akhmetovlz@51
    34
	{	
akhmetovlz@51
    35
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@51
    36
		
akhmetovlz@51
    37
		IValue targetValue = targetSlot.getValue();
akhmetovlz@51
    38
		
akhmetovlz@51
    39
		if(targetValue instanceof ICard){
akhmetovlz@51
    40
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@51
    41
		}
akhmetovlz@51
    42
		
akhmetovlz@51
    43
		Value numTargetValue = (Value) targetValue;
akhmetovlz@51
    44
		if( numTargetValue.getValue() == number ){
akhmetovlz@51
    45
			return retArray;
akhmetovlz@51
    46
		}else
akhmetovlz@51
    47
		if( numTargetValue.getValue() > number ){
akhmetovlz@51
    48
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@51
    49
			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
akhmetovlz@51
    50
			stupidAlg(0, number, targetSlot);
akhmetovlz@51
    51
		}else{
akhmetovlz@51
    52
			stupidAlg(numTargetValue.getValue(), number, targetSlot);
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
	
akhmetovlz@51
    72
	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
akhmetovlz@51
    73
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@51
    74
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@51
    75
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
akhmetovlz@51
    76
		while(end >= 2){
akhmetovlz@51
    77
			int d = end % 2;
akhmetovlz@51
    78
			if( d == 0 ) {
akhmetovlz@51
    79
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
akhmetovlz@51
    80
			} else {
akhmetovlz@51
    81
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
akhmetovlz@51
    82
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
akhmetovlz@51
    83
			}
akhmetovlz@51
    84
			end = end / 2; 
akhmetovlz@51
    85
		}
akhmetovlz@51
    86
		return retArray;
akhmetovlz@51
    87
	}
akhmetovlz@51
    88
	
akhmetovlz@51
    89
	public abstract ArrayList<Batch> getBatches();
akhmetovlz@51
    90
}