/***********************************************
 * icfpc-2011
 * بِسْمِ اللهِ الرَّحْمنِ الرَّحِيمِِ
 * Time: 18.06.2011 11:37:46
 ***********************************************/
package ru.alphatech.icfpc.game;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import ru.alphatech.icfpc.cards.Attack;
import ru.alphatech.icfpc.cards.Copy;
import ru.alphatech.icfpc.cards.Dbl;
import ru.alphatech.icfpc.cards.Dec;
import ru.alphatech.icfpc.cards.Get;
import ru.alphatech.icfpc.cards.Help;
import ru.alphatech.icfpc.cards.I;
import ru.alphatech.icfpc.cards.ICard;
import ru.alphatech.icfpc.cards.Inc;
import ru.alphatech.icfpc.cards.K;
import ru.alphatech.icfpc.cards.Put;
import ru.alphatech.icfpc.cards.Revive;
import ru.alphatech.icfpc.cards.S;
import ru.alphatech.icfpc.cards.Succ;
import ru.alphatech.icfpc.cards.Zero;
import ru.alphatech.icfpc.cards.Zombie;
import ru.alphatech.icfpc.game.Batch.OperationType;
import ru.alphatech.icfpc.game.strategy.IStrategy;
import ru.alphatech.icfpc.game.strategy.SimplyStrategy;

public class Run
{
	private static int callcount = 0;

	private static boolean automodeapply = false;

	private static boolean endgame = false;
	
	private static Player proponent = new Player();
	
	private static Player opponent = new Player();
	
	private static int registers[] = new int[4];
	
	static {
		proponent.setMyOpponent(opponent);
		opponent.setMyOpponent(proponent);
		registers[0] = 0;
		registers[1] = 1;
		registers[2] = 2;
		registers[3] = 3;
	}
	
	private static IStrategy strategy = new SimplyStrategy();
	
	public static Slot getRegisterByIndex(int registerIndex){
		return proponent.getSlot( registers[registerIndex] );
	}
	
	public static int getRegisterSlotNum(int registerIndex ){
		return registers[registerIndex];
	}
	
	public static Player getOpponent(){
		return opponent;
	}
	
	public static Player getProponent(){
		return proponent;
	}
	
	private static void readOpponent() throws IOException
	{
		BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
		String sTurnCase = breader.readLine();
		String sCard = null;
		String sSlot = null;
		
		OperationType operationType = null;
		ICard card = null;
		Slot slot = null;
		
		if("1".equals(sTurnCase))
		{
			sCard = breader.readLine();
			sSlot = breader.readLine();
			operationType = OperationType.CARD_TO_SLOT;
		}
		else if("2".equals(sTurnCase))
		{
			sSlot = breader.readLine();
			sCard = breader.readLine();
			operationType = OperationType.SLOT_TO_CARD;
		}
		else
		{
			// do nothing
		}
		
		if(sTurnCase == null || sCard == null || sSlot == null)
		{
			endgame = true;
			return;
		}
		
		slot = opponent.getSlot(Integer.parseInt(sSlot));
		for(ICard iCard: new ICard[]{
				new Attack(),
				new Copy(),
				new Dbl(),
				new Dec(),
				new Get(),
				new Help(),
				new I(),
				new Inc(),
				new K(),
				new Put(),
				new Revive(),
				new S(),
				new Succ(),
				new Zero(),
				new Zombie(),
				}){
			if(sCard.equals(iCard.getName())){
				card = iCard;
				break;
			}
		}
		Batch batch = new Batch(operationType, card, slot);
		opponent.applyBatch(batch);
	}
	
	private static void writeMove()
    {
		Batch batch = strategy.move(proponent, opponent);
		System.out.print(batch);
		proponent.applyBatch(batch);
    }
	
	public static void resetCallCount()
	{
		callcount = 0;
	}
	
	public static int incCallCount()
	{
		callcount++;
		
		return callcount;
	}
	
	public static boolean getAutoMode()
	{
		return automodeapply;
	}
	
	public static void setAutoMode(boolean b){
		automodeapply = b;
	}
	
	public static void main(String[] args) throws IOException
    {
	    String playerNum = args[0];

	    if("1".equals(playerNum))
	    {
	    	readOpponent();
	    }
	    
	    while(!endgame)
	    {
		    writeMove();
		    readOpponent();
	    }
    }
}
