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)
turtle@13
     1
/***********************************************
turtle@13
     2
 * icfpc-2011
turtle@13
     3
 * بِسْمِ اللهِ الرَّحْمنِ الرَّحِيمِِ
turtle@13
     4
 * Time: 18.06.2011 11:37:46
turtle@13
     5
 ***********************************************/
turtle@13
     6
package ru.alphatech.icfpc.game;
turtle@13
     7
turtle@14
     8
import java.io.BufferedReader;
turtle@14
     9
import java.io.IOException;
turtle@14
    10
import java.io.InputStreamReader;
turtle@14
    11
indvdum@96
    12
import ru.alphatech.icfpc.cards.Attack;
indvdum@96
    13
import ru.alphatech.icfpc.cards.Copy;
indvdum@96
    14
import ru.alphatech.icfpc.cards.Dbl;
indvdum@96
    15
import ru.alphatech.icfpc.cards.Dec;
indvdum@96
    16
import ru.alphatech.icfpc.cards.Get;
indvdum@96
    17
import ru.alphatech.icfpc.cards.Help;
indvdum@96
    18
import ru.alphatech.icfpc.cards.I;
indvdum@96
    19
import ru.alphatech.icfpc.cards.ICard;
indvdum@96
    20
import ru.alphatech.icfpc.cards.Inc;
indvdum@96
    21
import ru.alphatech.icfpc.cards.K;
indvdum@96
    22
import ru.alphatech.icfpc.cards.Put;
indvdum@96
    23
import ru.alphatech.icfpc.cards.Revive;
indvdum@96
    24
import ru.alphatech.icfpc.cards.S;
indvdum@96
    25
import ru.alphatech.icfpc.cards.Succ;
indvdum@96
    26
import ru.alphatech.icfpc.cards.Zero;
indvdum@96
    27
import ru.alphatech.icfpc.cards.Zombie;
indvdum@96
    28
import ru.alphatech.icfpc.game.Batch.OperationType;
indvdum@55
    29
import ru.alphatech.icfpc.game.strategy.IStrategy;
akhmetovlz@133
    30
import ru.alphatech.icfpc.game.strategy.LenarStrategy;
indvdum@55
    31
turtle@13
    32
public class Run
turtle@13
    33
{
turtle@102
    34
	private static int callcount = 0;
turtle@102
    35
turtle@78
    36
	private static boolean automodeapply = false;
turtle@78
    37
turtle@15
    38
	private static boolean endgame = false;
turtle@15
    39
	
turtle@14
    40
	private static Player proponent = new Player();
turtle@14
    41
	
turtle@14
    42
	private static Player opponent = new Player();
turtle@14
    43
	
akhmetovlz@48
    44
	private static int registers[] = new int[4];
akhmetovlz@48
    45
	
akhmetovlz@48
    46
	static {
indvdum@99
    47
		proponent.setMyOpponent(opponent);
indvdum@99
    48
		opponent.setMyOpponent(proponent);
akhmetovlz@48
    49
		registers[0] = 0;
akhmetovlz@48
    50
		registers[1] = 1;
akhmetovlz@48
    51
		registers[2] = 2;
akhmetovlz@48
    52
		registers[3] = 3;
akhmetovlz@48
    53
	}
akhmetovlz@48
    54
	
akhmetovlz@133
    55
	private static IStrategy strategy = new LenarStrategy();
indvdum@55
    56
	
akhmetovlz@92
    57
	public static Slot getRegisterByIndex(int registerIndex){
akhmetovlz@92
    58
		return proponent.getSlot( registers[registerIndex] );
akhmetovlz@92
    59
	}
akhmetovlz@92
    60
	
akhmetovlz@92
    61
	public static int getRegisterSlotNum(int registerIndex ){
akhmetovlz@92
    62
		return registers[registerIndex];
akhmetovlz@48
    63
	}
akhmetovlz@48
    64
	
akhmetovlz@48
    65
	public static Player getOpponent(){
akhmetovlz@48
    66
		return opponent;
akhmetovlz@48
    67
	}
akhmetovlz@48
    68
	
akhmetovlz@48
    69
	public static Player getProponent(){
akhmetovlz@48
    70
		return proponent;
akhmetovlz@31
    71
	}
akhmetovlz@31
    72
	
turtle@126
    73
	private static String[] doDebugRead()
turtle@126
    74
	{
turtle@126
    75
		return new String[] {"1", "I", "0"};
turtle@126
    76
	}
turtle@126
    77
	
turtle@126
    78
	private static String[] doRealRead() throws IOException
turtle@126
    79
	{
turtle@126
    80
		BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
turtle@126
    81
turtle@126
    82
		return new String[] {breader.readLine(), breader.readLine(), breader.readLine()};
turtle@126
    83
	}
turtle@126
    84
	
turtle@14
    85
	private static void readOpponent() throws IOException
turtle@14
    86
	{
turtle@126
    87
		String[] readData = doRealRead();
turtle@126
    88
		String sTurnCase = readData[0];
indvdum@96
    89
		String sCard = null;
indvdum@96
    90
		String sSlot = null;
turtle@14
    91
		
indvdum@96
    92
		OperationType operationType = null;
indvdum@96
    93
		ICard card = null;
indvdum@96
    94
		Slot slot = null;
indvdum@96
    95
		
indvdum@96
    96
		if("1".equals(sTurnCase))
turtle@29
    97
		{
turtle@126
    98
			sCard = readData[1];
turtle@126
    99
			sSlot = readData[2];
indvdum@96
   100
			operationType = OperationType.CARD_TO_SLOT;
turtle@29
   101
		}
indvdum@96
   102
		else if("2".equals(sTurnCase))
turtle@29
   103
		{
turtle@126
   104
			sSlot = readData[1];
turtle@126
   105
			sCard = readData[2];
indvdum@96
   106
			operationType = OperationType.SLOT_TO_CARD;
turtle@29
   107
		}
turtle@29
   108
		else
turtle@29
   109
		{
turtle@29
   110
			// do nothing
turtle@29
   111
		}
turtle@15
   112
		
indvdum@96
   113
		if(sTurnCase == null || sCard == null || sSlot == null)
turtle@15
   114
		{
turtle@15
   115
			endgame = true;
indvdum@94
   116
			return;
turtle@15
   117
		}
indvdum@94
   118
		
indvdum@96
   119
		slot = opponent.getSlot(Integer.parseInt(sSlot));
indvdum@96
   120
		for(ICard iCard: new ICard[]{
indvdum@96
   121
				new Attack(),
indvdum@96
   122
				new Copy(),
indvdum@96
   123
				new Dbl(),
indvdum@96
   124
				new Dec(),
indvdum@96
   125
				new Get(),
indvdum@96
   126
				new Help(),
indvdum@96
   127
				new I(),
indvdum@96
   128
				new Inc(),
indvdum@96
   129
				new K(),
indvdum@96
   130
				new Put(),
indvdum@96
   131
				new Revive(),
indvdum@96
   132
				new S(),
indvdum@96
   133
				new Succ(),
indvdum@96
   134
				new Zero(),
indvdum@96
   135
				new Zombie(),
indvdum@96
   136
				}){
indvdum@96
   137
			if(sCard.equals(iCard.getName())){
indvdum@96
   138
				card = iCard;
indvdum@96
   139
				break;
indvdum@96
   140
			}
indvdum@96
   141
		}
indvdum@96
   142
		Batch batch = new Batch(operationType, card, slot);
indvdum@96
   143
		opponent.applyBatch(batch);
turtle@14
   144
	}
turtle@14
   145
	
turtle@14
   146
	private static void writeMove()
turtle@14
   147
    {
indvdum@90
   148
		Batch batch = strategy.move(proponent, opponent);
indvdum@90
   149
		System.out.print(batch);
indvdum@90
   150
		proponent.applyBatch(batch);
turtle@14
   151
    }
turtle@15
   152
	
turtle@102
   153
	public static void resetCallCount()
turtle@102
   154
	{
turtle@102
   155
		callcount = 0;
turtle@102
   156
	}
turtle@102
   157
	
turtle@102
   158
	public static int incCallCount()
turtle@102
   159
	{
turtle@102
   160
		callcount++;
turtle@102
   161
		
turtle@102
   162
		return callcount;
turtle@102
   163
	}
turtle@102
   164
	
turtle@78
   165
	public static boolean getAutoMode()
turtle@78
   166
	{
turtle@78
   167
		return automodeapply;
turtle@78
   168
	}
turtle@78
   169
	
indvdum@106
   170
	public static void setAutoMode(boolean b){
indvdum@106
   171
		automodeapply = b;
indvdum@106
   172
	}
indvdum@106
   173
	
turtle@14
   174
	public static void main(String[] args) throws IOException
turtle@13
   175
    {
turtle@13
   176
	    String playerNum = args[0];
turtle@15
   177
turtle@14
   178
	    if("1".equals(playerNum))
turtle@14
   179
	    {
indvdum@122
   180
	    	proponent.setPlayerNumber(1);
indvdum@122
   181
	    	opponent.setPlayerNumber(0);
turtle@14
   182
	    	readOpponent();
indvdum@122
   183
	    } else {
indvdum@122
   184
	    	proponent.setPlayerNumber(0);
indvdum@122
   185
	    	opponent.setPlayerNumber(1);
turtle@14
   186
	    }
turtle@14
   187
	    
turtle@15
   188
	    while(!endgame)
turtle@15
   189
	    {
turtle@15
   190
		    writeMove();
turtle@15
   191
		    readOpponent();
turtle@15
   192
	    }
turtle@13
   193
    }
turtle@13
   194
}