src/ru/bosony/icfpc/Mine.java
branchindvdum
changeset 2 be45d8e4c165
parent 0 80446fa5641a
equal deleted inserted replaced
1:f83e1e778061 2:be45d8e4c165
     1 package ru.bosony.icfpc;
       
     2 
       
     3 import java.nio.file.FileVisitOption;
       
     4 import java.util.LinkedHashMap;
       
     5 import java.util.Map;
       
     6 
       
     7 import ru.bosony.icfpc.cells.Cell;
       
     8 
       
     9 /**
       
    10  * Шахта
       
    11  * 
       
    12  * @author akhmetovlz
       
    13  *
       
    14  */
       
    15 public class Mine
       
    16 {
       
    17 	private Map<Character, Cell> cellPrototypes = new LinkedHashMap<Character, Cell>();
       
    18 	
       
    19 	private Cell cells[][][];
       
    20 	private int index = 0;
       
    21 	
       
    22 	public Mine(int n, int m, String content)
       
    23 	{
       
    24 		initCellPrototypes();
       
    25 		
       
    26 		cells = new Cell[2][n][m];
       
    27 		
       
    28 		int index = 0;
       
    29 		
       
    30 		for(int i=0; i<n; ++i)
       
    31 		for(int j=0; j<m; ++j)
       
    32 		{
       
    33 			cells[index][i][j] = cellPrototypes.get(content.charAt(index++));
       
    34 		}
       
    35 	}
       
    36 	
       
    37 	private Cell[][] firstBuffer()
       
    38 	{
       
    39 		return cells[index];
       
    40 	}
       
    41 	
       
    42 	private Cell[][] secondBuffer()
       
    43 	{
       
    44 		return cells[index==0 ? 1 : 0];
       
    45 	}
       
    46 	
       
    47 	private void swapBuffers()
       
    48 	{
       
    49 		index = index==0 ? 1 : 0;
       
    50 	}
       
    51 	
       
    52 	private void initCellPrototypes()
       
    53 	{
       
    54 		for(Cell c : Cell.values())
       
    55 		{
       
    56 			cellPrototypes.put(c.getASCII(), c);
       
    57 		}
       
    58 	}	
       
    59 
       
    60 	public void print()
       
    61 	{
       
    62 		for(int i=0; i<cells.length; ++i)
       
    63 		{
       
    64 			for(int j=0; j<cells[i].length; ++j)
       
    65 			{
       
    66 				System.out.print(firstBuffer()[i][j]);
       
    67 			}
       
    68 			System.out.println();
       
    69 		}
       
    70 	}
       
    71 	
       
    72 	public void updateAfterMove()
       
    73 	{
       
    74 		for(int i=0; i<cells.length; ++i)
       
    75 		for(int j=0; j<cells[i].length; ++j)
       
    76 		{
       
    77 			secondBuffer()[i][j] = firstBuffer()[i][j];
       
    78 		}
       
    79 		
       
    80 		for(int i=1; i<cells.length-1; ++i)
       
    81 		for(int j=cells[i].length-1; j>1; --j)
       
    82 		{
       
    83 			switch(firstBuffer()[i][j])
       
    84 			{
       
    85 				case ROCK:
       
    86 				{
       
    87 					if(firstBuffer()[i][j-1] == Cell.EMPTY)
       
    88 					{
       
    89 						secondBuffer()[i][j] = Cell.EMPTY;
       
    90 					}
       
    91 				}
       
    92 				break;
       
    93 			}
       
    94 		}
       
    95 	}
       
    96 	
       
    97 }