src/main/java/ru/alphatech/icfpc/game/Run.java
author Azamat S. Kalimoulline <turtle@bazon.ru>
Mon, 20 Jun 2011 02:44:58 +0400
changeset 129 e8d3dceb731a
parent 127 57ee53a3cf56
child 134 c686931ff47e
permissions -rw-r--r--
Redefined toString
     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 String[] doDebugRead()
    76 	{
    77 		return new String[] {"1", "I", "0"};
    78 	}
    79 	
    80 	private static String[] doRealRead() throws IOException
    81 	{
    82 		BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
    83 
    84 		return new String[] {breader.readLine(), breader.readLine(), breader.readLine()};
    85 	}
    86 	
    87 	private static void readOpponent() throws IOException
    88 	{
    89 		//String[] readData = doRealRead();
    90 		String[] readData = doDebugRead();
    91 		String sTurnCase = readData[0];
    92 		String sCard = null;
    93 		String sSlot = null;
    94 		
    95 		OperationType operationType = null;
    96 		ICard card = null;
    97 		Slot slot = null;
    98 		
    99 		if("1".equals(sTurnCase))
   100 		{
   101 			sCard = readData[1];
   102 			sSlot = readData[2];
   103 			operationType = OperationType.CARD_TO_SLOT;
   104 		}
   105 		else if("2".equals(sTurnCase))
   106 		{
   107 			sSlot = readData[1];
   108 			sCard = readData[2];
   109 			operationType = OperationType.SLOT_TO_CARD;
   110 		}
   111 		else
   112 		{
   113 			// do nothing
   114 		}
   115 		
   116 		if(sTurnCase == null || sCard == null || sSlot == null)
   117 		{
   118 			endgame = true;
   119 			return;
   120 		}
   121 		
   122 		slot = opponent.getSlot(Integer.parseInt(sSlot));
   123 		for(ICard iCard: new ICard[]{
   124 				new Attack(),
   125 				new Copy(),
   126 				new Dbl(),
   127 				new Dec(),
   128 				new Get(),
   129 				new Help(),
   130 				new I(),
   131 				new Inc(),
   132 				new K(),
   133 				new Put(),
   134 				new Revive(),
   135 				new S(),
   136 				new Succ(),
   137 				new Zero(),
   138 				new Zombie(),
   139 				}){
   140 			if(sCard.equals(iCard.getName())){
   141 				card = iCard;
   142 				break;
   143 			}
   144 		}
   145 		Batch batch = new Batch(operationType, card, slot);
   146 		opponent.applyBatch(batch);
   147 	}
   148 	
   149 	private static void writeMove()
   150     {
   151 		Batch batch = strategy.move(proponent, opponent);
   152 		System.out.print(batch);
   153 		proponent.applyBatch(batch);
   154     }
   155 	
   156 	public static void resetCallCount()
   157 	{
   158 		callcount = 0;
   159 	}
   160 	
   161 	public static int incCallCount()
   162 	{
   163 		callcount++;
   164 		
   165 		return callcount;
   166 	}
   167 	
   168 	public static boolean getAutoMode()
   169 	{
   170 		return automodeapply;
   171 	}
   172 	
   173 	public static void setAutoMode(boolean b){
   174 		automodeapply = b;
   175 	}
   176 	
   177 	public static void main(String[] args) throws IOException
   178     {
   179 	    String playerNum = args[0];
   180 
   181 	    if("1".equals(playerNum))
   182 	    {
   183 	    	proponent.setPlayerNumber(1);
   184 	    	opponent.setPlayerNumber(0);
   185 	    	readOpponent();
   186 	    } else {
   187 	    	proponent.setPlayerNumber(0);
   188 	    	opponent.setPlayerNumber(1);
   189 	    }
   190 	    
   191 	    while(!endgame)
   192 	    {
   193 		    writeMove();
   194 		    readOpponent();
   195 	    }
   196     }
   197 }