src/main/java/ru/alphatech/icfpc/policy/AbstractPolicy.java
author indvdum
Sun, 19 Jun 2011 13:15:51 +0400
changeset 46 c592dd7b0595
parent 42 ee46d43195c1
child 48 7e49c679fc70
permissions -rw-r--r--
* moving file
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@32
    13
import ru.alphatech.icfpc.game.Slot;
akhmetovlz@32
    14
import ru.alphatech.icfpc.game.Value;
akhmetovlz@32
    15
import ru.alphatech.icfpc.game.Batch.OperationType;
akhmetovlz@19
    16
akhmetovlz@38
    17
/**
akhmetovlz@38
    18
 * 
akhmetovlz@42
    19
 * TODO 
akhmetovlz@38
    20
 * 
akhmetovlz@38
    21
 * @author akhmetovlz
akhmetovlz@38
    22
 *
akhmetovlz@38
    23
 */
akhmetovlz@19
    24
public abstract class AbstractPolicy {
akhmetovlz@19
    25
akhmetovlz@19
    26
	protected Player player = null;
akhmetovlz@19
    27
	
akhmetovlz@32
    28
	protected ArrayList<Batch> batches = new ArrayList<Batch>();
akhmetovlz@19
    29
	
akhmetovlz@32
    30
	public AbstractPolicy(Player player){
akhmetovlz@19
    31
		this.player = player;
akhmetovlz@32
    32
	}	
akhmetovlz@32
    33
	
akhmetovlz@38
    34
	public void setRegister(int registerIndex, int num ){
akhmetovlz@38
    35
		if( registerIndex < 4) {
akhmetovlz@38
    36
			Slot registerSlot = player.getRegisterByIndex(registerIndex);
akhmetovlz@38
    37
			batches.addAll( numberIntoSlotField( registerSlot, num ) );
akhmetovlz@38
    38
		}
akhmetovlz@19
    39
	}
akhmetovlz@19
    40
	
akhmetovlz@38
    41
	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
akhmetovlz@32
    42
	{	
akhmetovlz@32
    43
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@32
    44
		
akhmetovlz@32
    45
		IValue targetValue = targetSlot.getValue();
akhmetovlz@32
    46
		
akhmetovlz@32
    47
		if(targetValue instanceof ICard){
akhmetovlz@32
    48
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@19
    49
		}
akhmetovlz@32
    50
		
akhmetovlz@32
    51
		Value numTargetValue = (Value) targetValue;
akhmetovlz@32
    52
		if( numTargetValue.getValue() == number ){
akhmetovlz@32
    53
			return retArray;
akhmetovlz@32
    54
		}else
akhmetovlz@38
    55
		if( numTargetValue.getValue() > number ){
akhmetovlz@32
    56
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@32
    57
			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
akhmetovlz@38
    58
			stupidAlg(0, number, targetSlot);
akhmetovlz@38
    59
		}else{
akhmetovlz@38
    60
			stupidAlg(numTargetValue.getValue(), number, targetSlot);
akhmetovlz@32
    61
		}
akhmetovlz@32
    62
		
akhmetovlz@32
    63
		return retArray;
akhmetovlz@19
    64
	}
akhmetovlz@19
    65
	
akhmetovlz@38
    66
	
akhmetovlz@38
    67
	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
akhmetovlz@38
    68
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@38
    69
		while( begin*2 < end){
akhmetovlz@38
    70
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
akhmetovlz@38
    71
			begin *= 2;
akhmetovlz@38
    72
		}
akhmetovlz@38
    73
		while( begin != end){
akhmetovlz@38
    74
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
akhmetovlz@38
    75
			begin ++;
akhmetovlz@38
    76
		}
akhmetovlz@38
    77
		return retArray;
akhmetovlz@38
    78
	}
akhmetovlz@38
    79
	
akhmetovlz@38
    80
	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
akhmetovlz@38
    81
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@38
    82
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
akhmetovlz@38
    83
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
akhmetovlz@38
    84
		while(end >= 2){
akhmetovlz@38
    85
			int d = end % 2;
akhmetovlz@38
    86
			if( d == 0 ) {
akhmetovlz@38
    87
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
akhmetovlz@38
    88
			} else {
akhmetovlz@38
    89
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
akhmetovlz@38
    90
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
akhmetovlz@38
    91
			}
akhmetovlz@38
    92
			end = end / 2; 
akhmetovlz@38
    93
		}
akhmetovlz@38
    94
		return retArray;
akhmetovlz@38
    95
	}
akhmetovlz@41
    96
	
akhmetovlz@41
    97
	public abstract ArrayList<Batch> getBatches();
akhmetovlz@19
    98
}