Advertisement
NLKappa

Project

May 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. public class PlayField {
  2.     private byte[][] playField = new byte[10][10];
  3.     /*
  4.     0 - пусто
  5.     1 - корабль
  6.     2 - разбитый корабль
  7.     -1 - пусто - прострелено
  8.      */
  9.  
  10.     public byte[][] HiddenField(){
  11.         byte[][] toReturn = playField;
  12.         for(int x=0; x< 10; x++){
  13.             for(int y=0; y< 10; y++)
  14.                 if(toReturn[x][y] == 1)
  15.                     toReturn[x][y] = 0;
  16.             }
  17.  
  18.         return toReturn;
  19.         }
  20.  
  21.     private byte[] ships = new byte[4]; //Количество кораблей (индекс - (длина-1))
  22.     public int ShipCount(int length){
  23.         return ships[length-1];
  24.         }
  25.  
  26.     private boolean CheckEmptyPoint(int x, int y){
  27.         if (x > 9 ||
  28.             x < 0 ||
  29.             y > 9 ||
  30.             y < 0)
  31.             return true;
  32.         return !(playField[x][y] > 0);
  33.         }
  34.  
  35.     private boolean CheckEmptyArea(int top, int bottom, int left, int right){
  36.         for(int x = left; x< right; x++){
  37.             for(int y = top; y< bottom; y++){
  38.                 if(!CheckEmptyPoint(x, y)) return false;
  39.                 }
  40.             }
  41.         return  true;
  42.         }
  43.  
  44.     public boolean PlaceShip(int x, int y, boolean rightDir, int length){
  45.         if(ships[length-1] >= (5-length)) return false;
  46.         int deltaX=0, deltaY=0;
  47.         if(rightDir){
  48.             if(x+length-1 > 9) return false;
  49.             if(CheckEmptyArea(y-1, y+1, x-1, x+length))
  50.                 return false;
  51.             deltaX=1;
  52.             }
  53.         else{
  54.             if(y+length-1 > 9) return false;
  55.             if(CheckEmptyArea(y-1, y+length, x-1, x+1))
  56.                 return false;
  57.             deltaY=1;
  58.             }
  59.         int curX=x, curY=y;
  60.         for(int i=0; i< length; i++){
  61.             playField[curX][curY] = 1;
  62.             curX += deltaX;
  63.             curY += deltaY;
  64.             }
  65.         ships[length-1]++;
  66.         return true;
  67.         }
  68.  
  69.     private boolean CheckShipDestroyed(int x, int y){
  70.         boolean rightDir = !(CheckEmptyPoint(x+1, y) && CheckEmptyPoint(x-1, y));
  71.         int vectorX=0, vectorY=0;
  72.         if(rightDir)
  73.             vectorX=1;
  74.         else
  75.             vectorY=1;
  76.         while(!CheckEmptyPoint(x-vectorX, y-vectorY)){ // в начальное положение
  77.             x -= vectorX;
  78.             y -= vectorY;
  79.             }
  80.         int oriX=x, oriY=y, length=0;
  81.         while(!CheckEmptyPoint(x, y)){
  82.             if(playField[x][y] == 1) return false;
  83.             x += vectorX;
  84.             y += vectorY;
  85.             length++;
  86.             }
  87.         // Выделяем пустые клетки вокруг разбитого корабля
  88.         DestroyedShipOutline(oriX, oriY, rightDir, length);
  89.         return true;
  90.         }
  91.  
  92.     private void SetPointShot(int x, int y){
  93.         if(x > 9 || x < 0 || y > 9 || y < 0) return;
  94.         playField[x][y] = -1;
  95.         }
  96.  
  97.     private void DestroyedShipOutline(int oriX, int oriY, boolean rightDir, int length){
  98.         oriX--; oriY--; length++;
  99.         if(rightDir){
  100.             for(int curX = oriX; curX< oriX+length; curX++)
  101.                 SetPointShot(curX, oriY);
  102.             SetPointShot(oriX, oriY+1);
  103.             SetPointShot(oriX+length, oriY+1);
  104.             oriY += 2;
  105.             for(int curX = oriX; curX< oriX+length; curX++)
  106.                 SetPointShot(curX, oriY);
  107.             }
  108.         else{
  109.             for(int curY = oriY; curY< oriY+length; curY++)
  110.                 SetPointShot(oriX, curY);
  111.             SetPointShot(oriX+1, oriY);
  112.             SetPointShot(oriX+1, oriY+length);
  113.             oriX += 2;
  114.             for(int curY = oriY; curY< oriY+length; curY++)
  115.                 SetPointShot(oriX, curY);
  116.             }
  117.         }
  118.  
  119.     enum ShotType{MISS, HIT, KILL, ERROR}
  120.     public ShotType Shoot(int x, int y){
  121.         if(x> 9 || y> 9) return ShotType.ERROR;
  122.         switch(playField[x][y]){
  123.             case 0:
  124.                 playField[x][y] = -1;
  125.                 return ShotType.MISS;
  126.             case 1:
  127.                 playField[x][y] = 2;
  128.                 return CheckShipDestroyed(x, y) ?
  129.                         ShotType.KILL :
  130.                         ShotType.HIT;
  131.             default: return ShotType.ERROR;
  132.             }
  133.         }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement