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