src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author rushan
Sun, 19 Jun 2011 17:30:18 +0400
changeset 87 0f4c762f56f7
parent 62 3252fb664a39
child 88 9555608af847
permissions -rw-r--r--
догонка числа
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);
rushan@87
    95
		}
rushan@87
    96
		return retArray;
rushan@87
    97
	}
rushan@87
    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@51
   102
		while(end >= 2){
akhmetovlz@51
   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@51
   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@51
   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@51
   119
			}
akhmetovlz@51
   120
		}
rushan@87
   121
		return list;
akhmetovlz@51
   122
	}
akhmetovlz@51
   123
	
akhmetovlz@62
   124
	protected ArrayList<Batch> BcombineForFunctWith3ParamsInRegister(ICard function, Slot processingSlot){
akhmetovlz@62
   125
		ArrayList<Batch> retArray = new ArrayList<Batch>();
akhmetovlz@62
   126
		
akhmetovlz@62
   127
		if(!(function instanceof Attack || function instanceof Help)){
akhmetovlz@62
   128
			throw new ICFPCException();
akhmetovlz@62
   129
		}
akhmetovlz@62
   130
		
akhmetovlz@62
   131
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
akhmetovlz@62
   132
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, function,  processingSlot));
akhmetovlz@62
   133
		
akhmetovlz@62
   134
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   135
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   136
		
akhmetovlz@62
   137
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
akhmetovlz@62
   138
akhmetovlz@62
   139
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   140
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   141
		
akhmetovlz@62
   142
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
akhmetovlz@62
   143
		
akhmetovlz@62
   144
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   145
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   146
		
akhmetovlz@62
   147
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
akhmetovlz@62
   148
akhmetovlz@62
   149
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   150
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   151
		
akhmetovlz@62
   152
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
akhmetovlz@62
   153
		
akhmetovlz@62
   154
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   155
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   156
		
akhmetovlz@62
   157
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
akhmetovlz@62
   158
		
akhmetovlz@62
   159
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   160
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   161
		
akhmetovlz@62
   162
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
akhmetovlz@62
   163
akhmetovlz@62
   164
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   165
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   166
		
akhmetovlz@62
   167
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
akhmetovlz@62
   168
		
akhmetovlz@62
   169
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   170
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   171
		
akhmetovlz@62
   172
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
akhmetovlz@62
   173
		
akhmetovlz@62
   174
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
akhmetovlz@62
   175
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
akhmetovlz@62
   176
		
akhmetovlz@62
   177
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
akhmetovlz@62
   178
		
akhmetovlz@62
   179
		return retArray;
akhmetovlz@62
   180
	}
akhmetovlz@62
   181
	
akhmetovlz@51
   182
	public abstract ArrayList<Batch> getBatches();
rushan@87
   183
	
rushan@87
   184
	public static void main(String[] args)
rushan@87
   185
	{
rushan@87
   186
		Slot slot = new Slot();
rushan@87
   187
		List<Batch> list = smartAlg(5, 12, slot);
rushan@87
   188
		System.out.println(list.toArray().toString());
rushan@87
   189
	}
akhmetovlz@51
   190
}