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