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