src/main/java/ru/alphatech/icfpc/game/Run.java
author indvdum
Sun, 19 Jun 2011 18:16:08 +0400
changeset 96 8c40512777a1
parent 95 598cbc609683
child 99 60187fe9405d
child 102 cc814352ab43
permissions -rw-r--r--
+ parsing opponent batch
     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.SimplyStrategy;
    31 
    32 public class Run
    33 {
    34 	private static boolean automodeapply = false;
    35 
    36 	private static boolean endgame = false;
    37 	
    38 	private static Player proponent = new Player();
    39 	
    40 	private static Player opponent = new Player();
    41 	
    42 	private static int registers[] = new int[4];
    43 	
    44 	static {
    45 		registers[0] = 0;
    46 		registers[1] = 1;
    47 		registers[2] = 2;
    48 		registers[3] = 3;
    49 	}
    50 	
    51 	private static IStrategy strategy = new SimplyStrategy();
    52 	
    53 	public static Slot getRegisterByIndex(int registerIndex){
    54 		return proponent.getSlot( registers[registerIndex] );
    55 	}
    56 	
    57 	public static int getRegisterSlotNum(int registerIndex ){
    58 		return registers[registerIndex];
    59 	}
    60 	
    61 	public static Player getOpponent(){
    62 		return opponent;
    63 	}
    64 	
    65 	public static Player getProponent(){
    66 		return proponent;
    67 	}
    68 	
    69 	private static void readOpponent() throws IOException
    70 	{
    71 		BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
    72 		String sTurnCase = breader.readLine();
    73 		String sCard = null;
    74 		String sSlot = null;
    75 		
    76 		OperationType operationType = null;
    77 		ICard card = null;
    78 		Slot slot = null;
    79 		
    80 		if("1".equals(sTurnCase))
    81 		{
    82 			sCard = breader.readLine();
    83 			sSlot = breader.readLine();
    84 			operationType = OperationType.CARD_TO_SLOT;
    85 		}
    86 		else if("2".equals(sTurnCase))
    87 		{
    88 			sSlot = breader.readLine();
    89 			sCard = breader.readLine();
    90 			operationType = OperationType.SLOT_TO_CARD;
    91 		}
    92 		else
    93 		{
    94 			// do nothing
    95 		}
    96 		
    97 		if(sTurnCase == null || sCard == null || sSlot == null)
    98 		{
    99 			endgame = true;
   100 			return;
   101 		}
   102 		
   103 		slot = opponent.getSlot(Integer.parseInt(sSlot));
   104 		for(ICard iCard: new ICard[]{
   105 				new Attack(),
   106 				new Copy(),
   107 				new Dbl(),
   108 				new Dec(),
   109 				new Get(),
   110 				new Help(),
   111 				new I(),
   112 				new Inc(),
   113 				new K(),
   114 				new Put(),
   115 				new Revive(),
   116 				new S(),
   117 				new Succ(),
   118 				new Zero(),
   119 				new Zombie(),
   120 				}){
   121 			if(sCard.equals(iCard.getName())){
   122 				card = iCard;
   123 				break;
   124 			}
   125 		}
   126 		Batch batch = new Batch(operationType, card, slot);
   127 		opponent.applyBatch(batch);
   128 	}
   129 	
   130 	private static void writeMove()
   131     {
   132 		Batch batch = strategy.move(proponent, opponent);
   133 		System.out.print(batch);
   134 		proponent.applyBatch(batch);
   135     }
   136 	
   137 	public static boolean getAutoMode()
   138 	{
   139 		return automodeapply;
   140 	}
   141 	
   142 	public static void main(String[] args) throws IOException
   143     {
   144 	    String playerNum = args[0];
   145 
   146 	    if("1".equals(playerNum))
   147 	    {
   148 	    	readOpponent();
   149 	    }
   150 	    
   151 	    while(!endgame)
   152 	    {
   153 		    writeMove();
   154 		    readOpponent();
   155 	    }
   156     }
   157 }