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