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