Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class Battleship {
- private Spot[][] battleship;
- private int guesses;
- private int hits;
- public Battleship(int rows, int columns)
- {
- battleship = new Spot[rows][columns];
- for(int i=0;i<rows;i++)
- {
- for(int j=0;j<columns;j++)
- {
- battleship[i][j] = new Spot();
- battleship[i][j].setShip(0);
- battleship[i][j].setVisited(false);
- }
- }
- }
- public void setBoard()
- {
- Random rand = new Random();
- int rotate = rand.nextInt(2);
- int rows = battleship.length;
- int columns = battleship[0].length;
- int w;
- int x;
- int y;
- int z;
- int ship;
- boolean valid=false;
- for(ship = 5; ship > 1; ship--)
- {
- while(!valid)
- if(rotate == 0)
- {
- valid = true;
- //orientation = 0, ship is vertical
- x = rand.nextInt(rows);
- y = rand.nextInt(columns+1-ship);
- for(z=0;z<ship;z++)
- {
- if(battleship[x][y+z].getShip()!=0)
- {
- valid = false;
- }
- }
- }
- else
- {
- valid = true;
- //ship is horizontal
- x = rand.nextInt(rows+1-ship);
- y = rand.nextInt(columns);
- for(z=0;z<ship;z++)
- {
- if(battleship[x+z][y].getShip()!=0)
- {
- valid = false;
- }
- }
- for(w=0;w<ship;w++)
- {
- battleship[x+w][y].setShip(ship);
- valid = false;
- }
- }
- }
- for(w=0;w<ship;w++)
- {
- battleship[x][y+w].setShip(ship);
- valid = false;
- }
- }
- public void printBoard(boolean reveal)
- {
- if(reveal)
- {
- for(int i=0;i<battleship.length;i++)
- {
- System.out.print("\n");
- System.out.print(" +-+-+-+-+-+-+-+");
- System.out.print("\n");
- for(int j=0;j<battleship[0].length;j++)
- {
- if(j==0)
- {
- System.out.print(i+1 + " ");
- }
- System.out.print("|" + battleship[i][j].getShip());
- if(j==battleship[0].length-1)
- {
- System.out.print('|');
- }
- }
- if(i+1==battleship.length)
- {
- System.out.print("\n");
- System.out.print(" +-+-+-+-+-+-+-+");
- System.out.print("\n ");
- for(int z=0;z<battleship[0].length;z++)
- {
- System.out.print(z+1 + " ");
- }
- }
- }
- System.out.printf("\n\n");
- }
- else
- {
- }
- }
- public void makeGuess()
- {
- }
- public void printStatistics()
- {
- }
- public boolean over()
- {
- return(hits==14);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement