# HG changeset patch # User Azamat S. Kalimoulline # Date 1308476573 -14400 # Node ID 9307831f7658f2b01e792dc46b1dc628c05dcbae # Parent 43e507966634ef44652660a3a720e86ab052c4b5# Parent 1ff9e4ee2b028103fbcd24600c37cf6b8cba1df7 Merge with 1ff9e4ee2b028103fbcd24600c37cf6b8cba1df7 diff -r 43e507966634 -r 9307831f7658 src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java Sun Jun 19 13:42:53 2011 +0400 @@ -0,0 +1,90 @@ +package ru.alphatech.icfpc.game; + +import java.util.ArrayList; + +import ru.alphatech.icfpc.cards.Dbl; +import ru.alphatech.icfpc.cards.ICard; +import ru.alphatech.icfpc.cards.Put; +import ru.alphatech.icfpc.cards.Succ; +import ru.alphatech.icfpc.cards.Zero; +import ru.alphatech.icfpc.game.Batch.OperationType; + +/** + * + * TODO + * + * @author akhmetovlz + * + */ +public abstract class AbstractPolicy { + + protected ArrayList batches = new ArrayList(); + + public AbstractPolicy(){ + } + + public void setRegister(int registerIndex, int num ){ + if( registerIndex < 4) { + Slot registerSlot = Run.getRegisterByIndex(registerIndex); + batches.addAll( numberIntoSlotField( registerSlot, num ) ); + } + } + + protected ArrayList numberIntoSlotField(Slot targetSlot, int number) + { + ArrayList retArray = new ArrayList(); + + IValue targetValue = targetSlot.getValue(); + + if(targetValue instanceof ICard){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); + } + + Value numTargetValue = (Value) targetValue; + if( numTargetValue.getValue() == number ){ + return retArray; + }else + if( numTargetValue.getValue() > number ){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); + retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot)); + stupidAlg(0, number, targetSlot); + }else{ + stupidAlg(numTargetValue.getValue(), number, targetSlot); + } + + return retArray; + } + + + protected ArrayList stupidAlg(int begin, int end, Slot targetSlot){ + ArrayList retArray = new ArrayList(); + while( begin*2 < end){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot)); + begin *= 2; + } + while( begin != end){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); + begin ++; + } + return retArray; + } + + protected ArrayList smartAlg(int begin, int end, Slot targetSlot){ + ArrayList retArray = new ArrayList(); + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); + retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot)); + while(end >= 2){ + int d = end % 2; + if( d == 0 ) { + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); + } else { + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); + } + end = end / 2; + } + return retArray; + } + + public abstract ArrayList getBatches(); +} diff -r 43e507966634 -r 9307831f7658 src/main/java/ru/alphatech/icfpc/game/Run.java --- a/src/main/java/ru/alphatech/icfpc/game/Run.java Sun Jun 19 13:42:36 2011 +0400 +++ b/src/main/java/ru/alphatech/icfpc/game/Run.java Sun Jun 19 13:42:53 2011 +0400 @@ -11,6 +11,9 @@ import java.util.LinkedList; import java.util.List; +import ru.alphatech.icfpc.game.strategy.IStrategy; +import ru.alphatech.icfpc.game.strategy.SimplyStrategy; + public class Run { private static boolean endgame = false; @@ -30,6 +33,8 @@ registers[3] = 3; } + private static IStrategy strategy = new SimplyStrategy(); + public static Slot getRegisterByIndex(int registerIdex){ return proponent.getSlot( registers[registerIdex] ); } @@ -80,9 +85,7 @@ private static void writeMove() { - System.out.print(1 + "\n"); - System.out.print("I" + "\n"); - System.out.print(0 + "\n"); + strategy.moving(proponent, opponent, System.out); } public static void main(String[] args) throws IOException diff -r 43e507966634 -r 9307831f7658 src/main/java/ru/alphatech/icfpc/game/strategy/IStrategy.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/alphatech/icfpc/game/strategy/IStrategy.java Sun Jun 19 13:42:53 2011 +0400 @@ -0,0 +1,14 @@ +package ru.alphatech.icfpc.game.strategy; + +import java.io.PrintStream; + +import ru.alphatech.icfpc.game.Player; + +/** + * @author indvdum + * 19.06.2011 13:36:17 + * + */ +public interface IStrategy { + public void moving(Player proponent, Player opponent, PrintStream out); +} diff -r 43e507966634 -r 9307831f7658 src/main/java/ru/alphatech/icfpc/game/strategy/SimplyStrategy.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/alphatech/icfpc/game/strategy/SimplyStrategy.java Sun Jun 19 13:42:53 2011 +0400 @@ -0,0 +1,21 @@ +package ru.alphatech.icfpc.game.strategy; + +import java.io.PrintStream; + +import ru.alphatech.icfpc.game.Player; + +/** + * @author indvdum + * 19.06.2011 13:36:26 + * + */ +public class SimplyStrategy implements IStrategy { + + @Override + public void moving(Player proponent, Player opponent, PrintStream out) { + out.print(1 + "\n"); + out.print("I" + "\n"); + out.print(0 + "\n"); + } + +} diff -r 43e507966634 -r 9307831f7658 src/main/java/ru/alphatech/icfpc/policy/AbstractPolicy.java --- a/src/main/java/ru/alphatech/icfpc/policy/AbstractPolicy.java Sun Jun 19 13:42:36 2011 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,96 +0,0 @@ -package ru.alphatech.icfpc.policy; - -import java.util.ArrayList; - -import ru.alphatech.icfpc.cards.Dbl; -import ru.alphatech.icfpc.cards.ICard; -import ru.alphatech.icfpc.cards.Put; -import ru.alphatech.icfpc.cards.Succ; -import ru.alphatech.icfpc.cards.Zero; -import ru.alphatech.icfpc.game.Batch; -import ru.alphatech.icfpc.game.IValue; -import ru.alphatech.icfpc.game.Player; -import ru.alphatech.icfpc.game.Run; -import ru.alphatech.icfpc.game.Slot; -import ru.alphatech.icfpc.game.Value; -import ru.alphatech.icfpc.game.Batch.OperationType; - -/** - * - * TODO - * - * @author akhmetovlz - * - */ -public abstract class AbstractPolicy { - - protected ArrayList batches = new ArrayList(); - - public AbstractPolicy(){ - } - - public void setRegister(int registerIndex, int num ){ - if( registerIndex < 4) { - Slot registerSlot = Run.getRegisterByIndex(registerIndex); - batches.addAll( numberIntoSlotField( registerSlot, num ) ); - } - } - - protected ArrayList numberIntoSlotField(Slot targetSlot, int number) - { - ArrayList retArray = new ArrayList(); - - IValue targetValue = targetSlot.getValue(); - - if(targetValue instanceof ICard){ - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); - } - - Value numTargetValue = (Value) targetValue; - if( numTargetValue.getValue() == number ){ - return retArray; - }else - if( numTargetValue.getValue() > number ){ - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); - retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot)); - stupidAlg(0, number, targetSlot); - }else{ - stupidAlg(numTargetValue.getValue(), number, targetSlot); - } - - return retArray; - } - - - protected ArrayList stupidAlg(int begin, int end, Slot targetSlot){ - ArrayList retArray = new ArrayList(); - while( begin*2 < end){ - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot)); - begin *= 2; - } - while( begin != end){ - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); - begin ++; - } - return retArray; - } - - protected ArrayList smartAlg(int begin, int end, Slot targetSlot){ - ArrayList retArray = new ArrayList(); - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); - retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot)); - while(end >= 2){ - int d = end % 2; - if( d == 0 ) { - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); - } else { - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); - retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); - } - end = end / 2; - } - return retArray; - } - - public abstract ArrayList getBatches(); -} diff -r 43e507966634 -r 9307831f7658 src/main/java/ru/alphatech/icfpc/policy/VitalityBoostPolicy.java --- a/src/main/java/ru/alphatech/icfpc/policy/VitalityBoostPolicy.java Sun Jun 19 13:42:36 2011 +0400 +++ b/src/main/java/ru/alphatech/icfpc/policy/VitalityBoostPolicy.java Sun Jun 19 13:42:53 2011 +0400 @@ -2,6 +2,7 @@ import java.util.ArrayList; +import ru.alphatech.icfpc.game.AbstractPolicy; import ru.alphatech.icfpc.game.Batch; public class VitalityBoostPolicy extends AbstractPolicy {