Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ocean
- {
- public Ship [][] ships=new Ship[10][10];
- public String [][] map=new String [10][10];
- public int shotsFired;
- public int hitCount;
- public int shipsSunk;
- public Ocean()
- {
- for(int i=0; i<=9;i++)
- {
- for(int j=0; j<=9;j++)
- {
- ships [i][j]=new EmptySea();
- map[i][j]=".";
- shotsFired=0;
- hitCount=0;
- shipsSunk = 0;
- }
- }
- }
- public void placeAllShipsRandomly()
- {
- int row=-1,column=-1;
- Battleship btlShip=new Battleship();
- do
- {
- int orient = rnd(1,4);
- if(orient%2==0)
- {
- btlShip.setHorizontal(true);
- row = rnd(0,9);
- column=rnd(0,6);
- }
- else
- {
- btlShip.setHorizontal(false);
- row = rnd(0,6);
- column=rnd(0,9);
- }
- }while(!btlShip.okToPlaceShipAt(row,column,btlShip.isHorizontal(),this));
- btlShip.placeShipAt(row,column,btlShip.isHorizontal(),this);
- Cruiser [] cruisers = new Cruiser[2];
- for(int i=0;i<2;i++)
- {
- do
- {
- int orient = rnd(1,4);
- if(orient%2==0)
- {
- cruisers[i].setHorizontal(true);
- row = rnd(0,9);
- column=rnd(0,7);
- }
- else
- {
- cruisers[i].setHorizontal(false);
- row = rnd(0,7);
- column=rnd(0,9);
- }
- }while(!cruisers[i].okToPlaceShipAt(row,column,cruisers[i].isHorizontal(),this));
- cruisers[i].placeShipAt(row,column,cruisers[i].isHorizontal(),this);
- }
- Destroyer [] destroyers = new Destroyer[3];
- for(int i=0;i<3;i++)
- {
- do
- {
- int orient = rnd(1,4);
- if(orient%2==0)
- {
- destroyers[i].setHorizontal(true);
- row = rnd(0,9);
- column=rnd(0,8);
- }
- else
- {
- destroyers[i].setHorizontal(false);
- row = rnd(0,8);
- column=rnd(0,9);
- }
- }while(!destroyers[i].okToPlaceShipAt(row,column,destroyers[i].isHorizontal(),this));
- destroyers[i].placeShipAt(row,column,destroyers[i].isHorizontal(),this);
- }
- Submarine [] submarines = new Submarine[4];
- for(int i=0;i<4;i++)
- {
- do
- {
- int orient = rnd(1,4);
- submarines[i].setHorizontal(true);
- row = rnd(0,9);
- column=rnd(0,8);
- }while(!submarines[i].okToPlaceShipAt(row,column,submarines[i].isHorizontal(),this));
- submarines[i].placeShipAt(row,column,submarines[i].isHorizontal(),this);
- }
- }
- public boolean isOccupied(int row,int column)
- {
- if(row>9||row<0||column>9||column<0)
- return false;
- else if(ships[row][column] instanceof EmptySea)
- return false;
- else
- return true;
- }
- public boolean shootAt(int row,int column)
- {
- if(ships[row][column]instanceof EmptySea)
- {
- shotsFired+=1;
- map[row][column]="-";
- return false;
- }
- else
- {
- shotsFired+=1;
- hitCount+=1;
- map[row][column]="s";
- if(ships[row][column].horizontal)
- {
- ships[row][column].hit[column-ships[row][column].getBowColumn()]=true;
- if(ships[row][column].isSunk())
- {
- for(int i=ships[row][column].getBowColumn();i<ships[row][column].getBowColumn()+ships[row][column].length;i++)
- {
- map[row][i]="x";
- }
- shipsSunk+=1;
- }
- }
- else
- {
- ships[row][column].hit[row-ships[row][column].getBowRow()]=true;
- if(ships[row][column].isSunk())
- {
- for(int i=ships[row][column].getBowRow();i<ships[row][column].getBowRow()+ships[row][column].length;i++)
- {
- map[i][column]="x";
- }
- shipsSunk+=1;
- }
- }
- return true;
- }
- }
- public int getShotsFires()
- {
- return shotsFired;
- }
- public int getHitCount()
- {
- return hitCount;
- }
- public int getShipsSunk()
- {
- return shipsSunk;
- }
- public boolean isGameOver()
- {
- if(shipsSunk==10)
- return true;
- else
- return false;
- }
- public Ship [][] getShipArray()
- {
- return this.ships;
- }
- public void print()
- {
- System.out.printf(" 0 1 2 3 4 5 6 7 8 9");
- for(int i=0; i<=9;i++)
- {
- System.out.printf(i+" ");
- for(int j=0; j<=9;j++)
- {
- System.out.printf(map[i][j].toString()+" ");
- }
- }
- }
- public static int rnd(int min, int max)
- {
- max -= min;
- return (int) (Math.random() * ++max) + min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement