Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Damien Bell <SkyeShatter@gmail.com>
- */
- import java.util.*;
- public class BattleShip {
- public static void main(String[] args){
- Scanner input = new Scanner(System.in);
- System.out.println("Welcome to battleship\n\n");
- char[][] ArrayPlayer= new char[10][10]; // Create an Array for the player
- Arrays MyPlayers = new Arrays(ArrayPlayer); // Set MyPlayers object to contain array ArrayPlayer, called from Arrays.java
- char[][] ArrayCPU= new char[10][10]; // Create an Array for the player
- Arrays CPUPlayers = new Arrays(ArrayCPU); // Set MyPlayers object to contain array ArrayPlayer, called from Arrays.java
- boolean[][] PlayerCheck = new boolean[10][10];
- boolean[][]CPUCheck = new boolean[10][10];
- Random generator = new Random(); // Create a random to be used later on.
- //Patrol Boat
- Boat PatrolBoat = new Boat();
- PatrolBoat.health=2;
- PatrolBoat.name="Patrol Boat";
- PatrolBoat.letter='P';
- //Submarine
- Boat Submarine = new Boat();
- Submarine.health=3;
- Submarine.name="Submarine";
- Submarine.letter='S';
- //Destroyer
- Boat Destroyer = new Boat();
- Destroyer.health=4;
- Destroyer.name="Destroyer";
- Destroyer.letter='D';
- //Battleship
- Boat BattleShip = new Boat();
- BattleShip.health=5;
- BattleShip.name="BattleShip";
- BattleShip.letter='B';
- //CPU ship copy
- Boat CPUPatrolBoat = PatrolBoat;
- Boat CPUSubmarine = Submarine;
- Boat CPUDestroyer = Destroyer;
- Boat CPUBattleShip = BattleShip;
- Boat SelectedBoat; // Create a boat called Selected Boat
- Boat[] boatarray = {PatrolBoat, Submarine, Destroyer, BattleShip}; // Boat array used in select, boat here gets applied to selectedboat
- Boat[] BoatCPU = {CPUPatrolBoat, CPUSubmarine, CPUDestroyer, CPUBattleShip};
- int iSelect, x=0, y=0, i=0, j=0,k=0, iOrientation, a, b, z; // initialize heaps of variables
- //_________________________________________________________________________________________________________
- // Initializing arrays
- //_________________________________________________________________________________________________________
- //Initialize ArrayPlayer with Empty spaces
- for (i=0; i<ArrayPlayer.length; i++){
- for (j=0; j< ArrayPlayer.length; j++){
- ArrayPlayer[i][j]=' ';
- }
- }//End player init
- for (i=0; i<ArrayCPU.length; i++){
- for (j=0; j< ArrayCPU.length; j++){
- ArrayCPU[i][j]=' ';
- }
- }//End true set.
- //Initialize playerCheck array to true-- This is used later to check for collision.
- for (i=0; i<PlayerCheck.length; i++){
- for (j=0; j< PlayerCheck.length; j++){
- PlayerCheck[i][j]=true;
- }
- }//End true set.
- //Same as above but for cpu
- for (i=0; i<CPUCheck.length; i++){
- for (j=0; j< CPUCheck.length; j++){
- CPUCheck[i][j]=true;
- }
- }//End true set.
- //_________________________________________________________________________________________________________
- //_________________________________________________________________________________________________________
- // Player Placement
- //_________________________________________________________________________________________________________
- for (i=0; i < 4; i++){
- System.out.println("Press 1 to place your patrol boat, 2 to place your submarine, 3 to place your destroyer, or 4 to place your battleship.");//Prompt user
- iSelect =input.nextInt(); // Get value for select from array
- SelectedBoat=boatarray[iSelect-1]; // Select boat from array
- SelectedBoat.used +=1;
- //Ensure boats aren't used more than once
- while (SelectedBoat.used > 1){
- System.out.println("This boat has already been used, try again: ");
- iSelect =input.nextInt(); // Get value for select from array
- SelectedBoat=boatarray[iSelect-1]; // Select boat from array
- SelectedBoat.used +=1;
- }
- System.out.println("Press 1 for Vertical, 2 for Horizontal: "); // Prompt for horiz or vert.
- iOrientation = input.nextInt(); // Get horiz or vert
- while (iOrientation < 1 || iOrientation > 2){
- System.out.println("You have selected an incorrect value, try again: ");
- iOrientation=input.nextInt();
- }
- //Choose the boat's location and convert it from letter / number to just a letter.
- System.out.println("Choose where you would like the piece to appear on a grid (A1 is top left J10 is bottom right): ");
- String c="";//Empty string / create string / dereference string
- c = input.next(); // Allow user to place boat
- SelectedBoat.location = Boatloc.BoatLoc(c); // Transform String from letter / number to 2 numbers for grid placement
- x=SelectedBoat.location[0]; // Set y to y value
- y=SelectedBoat.location[1];// Set x to X value.
- //_________________________________________________________________________________________________________
- // Check if boats go out of bounds
- //_________________________________________________________________________________________________________
- z = SelectedBoat.health;
- if (iOrientation==1){
- while (y + SelectedBoat.health > 9){ // while ship will go out of bounds--vert
- System.out.println("Placing a boat here will make it go out of bounds, try again: ");
- c="";
- c = input.next(); // Allow user to guess
- SelectedBoat.location = Boatloc.BoatLoc(c); // Transform String from letter / number to 2 numbers for grid placement
- y=SelectedBoat.location[0]; // Set y to y value
- x=SelectedBoat.location[1];// Set x to X value.
- }
- }
- else if (iOrientation==2){// while ship will go out of bounds--Horiz
- while (x + SelectedBoat.health > 9){
- System.out.println("Placing a boat here will make it go out of bounds, try again: ");
- c="";
- c = input.next(); // Allow user to guess
- SelectedBoat.location = Boatloc.BoatLoc(c); // Transform String from letter / number to 2 numbers for grid placement
- y=SelectedBoat.location[0]; // Set y to y value
- x=SelectedBoat.location[1];// Set x to X value.
- }
- }//End ori if
- //_________________________________________________________________________________________________________
- // Actual piece placement
- //_________________________________________________________________________________________________________
- //Vertical if Orientation
- if (iOrientation == 1){ // If orientation is vertical
- MyPlayers.AddBoatVert(x,y,SelectedBoat.health,SelectedBoat.letter);// Add boat letter at selected location
- PlayerCheck[x][y]=false;
- }// End vert orientation if/
- //Horizontal if orientation
- if (iOrientation == 2){//If boat orientation is horizontal, place as such
- for (j=0; j <SelectedBoat.health; j++){ // While < health, place more
- MyPlayers.AddBoatHoriz(y,x, SelectedBoat.health,SelectedBoat.letter);// Add boat letter at selected location
- PlayerCheck[x][y]=false;
- x++;
- }// end placement for
- }// End horizontal orientation if
- Arrays.DisplayMap(ArrayPlayer);// Player graph output.
- }// End placement for
- /*
- //_________________________________________________________________________________________________________
- // CPU placement
- //_________________________________________________________________________________________________________
- boolean[] isOkay= new boolean [5];
- boolean isNotOkay=false;
- for (i=0; i<5; i++){
- isOkay[i]=false;
- }
- for (i=0; i<4; i++){
- x=generator.nextInt(9);//X location
- y=generator.nextInt(9);// Y location
- iOrientation=generator.nextInt(1)+1;//Orientation
- SelectedBoat=BoatCPU[i];
- //Check out of bounds vert
- if (iOrientation==1){
- while (y + SelectedBoat.health > 9){
- y=generator.nextInt(); // Set y to y value
- }//End check while
- }//end orientation if
- //Check out of bounds horizontal
- if (iOrientation==2){
- while (x + SelectedBoat.health > 9){
- x=generator.nextInt(); // Set y to y value
- } //end check while
- }//end orientation if
- a=x;
- b=y;
- if (iOrientation==1){
- while (b + SelectedBoat.health > 9){
- b=generator.nextInt(); // Set y to y value
- }//End check while
- }//end orientation if
- //Check out of bounds horizontal
- if (iOrientation==2){
- while (a + SelectedBoat.health > 9){
- a=generator.nextInt(); // Set y to y value
- } //end check while
- }//end orientation if
- for (j=0; j<SelectedBoat.health; j++){
- isOkay[j]= Arrays.ArrayCheckPlayer(CPUCheck, a, b);
- if (isOkay[j]){
- //null
- }
- else if(isOkay[j]==false){//is not okay.
- isNotOkay=true;
- }
- a++;
- }// Is Not Okay Set for.
- if (isNotOkay==true){//if it is NOT okay
- i--;
- } // end Is Not Okay if
- if (iOrientation==1 && isNotOkay == false){
- for (j=0; j<SelectedBoat.health; j++) {
- ArrayCPU[x][y]=SelectedBoat.letter;
- y++;
- }
- }
- else if (iOrientation==2 && isNotOkay == false){
- for (j=0; j<SelectedBoat.health; j++) {
- ArrayCPU[x][y]=SelectedBoat.letter;
- x++;
- }
- }
- } // End CPU placement for
- *
- */
- }// End main
- }// End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement