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