src/main/java/ru/alphatech/icfpc/game/Run.java
author indvdum
Mon, 20 Jun 2011 02:00:52 +0400
changeset 125 292347b71221
parent 122 845c89fa268e
child 126 6aa46f954627
permissions -rw-r--r--
* merge:
     1 /***********************************************
     2  * icfpc-2011
     3  * بِسْمِ اللهِ الرَّحْمنِ الرَّحِيمِِ
     4  * Time: 18.06.2011 11:37:46
     5  ***********************************************/
     6 package ru.alphatech.icfpc.game;
     7 
     8 import java.io.BufferedReader;
     9 import java.io.File;
    10 import java.io.IOException;
    11 import java.io.InputStreamReader;
    12 
    13 import ru.alphatech.icfpc.cards.Attack;
    14 import ru.alphatech.icfpc.cards.Copy;
    15 import ru.alphatech.icfpc.cards.Dbl;
    16 import ru.alphatech.icfpc.cards.Dec;
    17 import ru.alphatech.icfpc.cards.Get;
    18 import ru.alphatech.icfpc.cards.Help;
    19 import ru.alphatech.icfpc.cards.I;
    20 import ru.alphatech.icfpc.cards.ICard;
    21 import ru.alphatech.icfpc.cards.Inc;
    22 import ru.alphatech.icfpc.cards.K;
    23 import ru.alphatech.icfpc.cards.Put;
    24 import ru.alphatech.icfpc.cards.Revive;
    25 import ru.alphatech.icfpc.cards.S;
    26 import ru.alphatech.icfpc.cards.Succ;
    27 import ru.alphatech.icfpc.cards.Zero;
    28 import ru.alphatech.icfpc.cards.Zombie;
    29 import ru.alphatech.icfpc.game.Batch.OperationType;
    30 import ru.alphatech.icfpc.game.strategy.IStrategy;
    31 import ru.alphatech.icfpc.game.strategy.SimplyStrategy;
    32 import ru.alphatech.icfpc.game.strategy.YetAnotherStrategy;
    33 
    34 public class Run
    35 {
    36 	private static int callcount = 0;
    37 
    38 	private static boolean automodeapply = false;
    39 
    40 	private static boolean endgame = false;
    41 	
    42 	private static Player proponent = new Player();
    43 	
    44 	private static Player opponent = new Player();
    45 	
    46 	private static int registers[] = new int[4];
    47 	
    48 	static {
    49 		proponent.setMyOpponent(opponent);
    50 		opponent.setMyOpponent(proponent);
    51 		registers[0] = 0;
    52 		registers[1] = 1;
    53 		registers[2] = 2;
    54 		registers[3] = 3;
    55 	}
    56 	
    57 	private static IStrategy strategy = new YetAnotherStrategy();
    58 	
    59 	public static Slot getRegisterByIndex(int registerIndex){
    60 		return proponent.getSlot( registers[registerIndex] );
    61 	}
    62 	
    63 	public static int getRegisterSlotNum(int registerIndex ){
    64 		return registers[registerIndex];
    65 	}
    66 	
    67 	public static Player getOpponent(){
    68 		return opponent;
    69 	}
    70 	
    71 	public static Player getProponent(){
    72 		return proponent;
    73 	}
    74 	
    75 	private static void readOpponent() throws IOException
    76 	{
    77 		BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
    78 		String sTurnCase = breader.readLine();
    79 		String sCard = null;
    80 		String sSlot = null;
    81 		
    82 		OperationType operationType = null;
    83 		ICard card = null;
    84 		Slot slot = null;
    85 		
    86 		if("1".equals(sTurnCase))
    87 		{
    88 			sCard = breader.readLine();
    89 			sSlot = breader.readLine();
    90 			operationType = OperationType.CARD_TO_SLOT;
    91 		}
    92 		else if("2".equals(sTurnCase))
    93 		{
    94 			sSlot = breader.readLine();
    95 			sCard = breader.readLine();
    96 			operationType = OperationType.SLOT_TO_CARD;
    97 		}
    98 		else
    99 		{
   100 			// do nothing
   101 		}
   102 		
   103 		if(sTurnCase == null || sCard == null || sSlot == null)
   104 		{
   105 			endgame = true;
   106 			return;
   107 		}
   108 		
   109 		slot = opponent.getSlot(Integer.parseInt(sSlot));
   110 		for(ICard iCard: new ICard[]{
   111 				new Attack(),
   112 				new Copy(),
   113 				new Dbl(),
   114 				new Dec(),
   115 				new Get(),
   116 				new Help(),
   117 				new I(),
   118 				new Inc(),
   119 				new K(),
   120 				new Put(),
   121 				new Revive(),
   122 				new S(),
   123 				new Succ(),
   124 				new Zero(),
   125 				new Zombie(),
   126 				}){
   127 			if(sCard.equals(iCard.getName())){
   128 				card = iCard;
   129 				break;
   130 			}
   131 		}
   132 		Batch batch = new Batch(operationType, card, slot);
   133 		opponent.applyBatch(batch);
   134 	}
   135 	
   136 	private static void writeMove()
   137     {
   138 		Batch batch = strategy.move(proponent, opponent);
   139 		System.out.print(batch);
   140 		proponent.applyBatch(batch);
   141     }
   142 	
   143 	public static void resetCallCount()
   144 	{
   145 		callcount = 0;
   146 	}
   147 	
   148 	public static int incCallCount()
   149 	{
   150 		callcount++;
   151 		
   152 		return callcount;
   153 	}
   154 	
   155 	public static boolean getAutoMode()
   156 	{
   157 		return automodeapply;
   158 	}
   159 	
   160 	public static void setAutoMode(boolean b){
   161 		automodeapply = b;
   162 	}
   163 	
   164 	public static void main(String[] args) throws IOException
   165     {
   166 	    String playerNum = args[0];
   167 
   168 	    if("1".equals(playerNum))
   169 	    {
   170 	    	proponent.setPlayerNumber(1);
   171 	    	opponent.setPlayerNumber(0);
   172 	    	readOpponent();
   173 	    } else {
   174 	    	proponent.setPlayerNumber(0);
   175 	    	opponent.setPlayerNumber(1);
   176 	    }
   177 	    
   178 	    while(!endgame)
   179 	    {
   180 		    writeMove();
   181 		    readOpponent();
   182 	    }
   183     }
   184 }