Advertisement
Guest User

BattleShip.java

a guest
Apr 30th, 2011
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. /**
  2. *
  3. * @author Damien Bell <SkyeShatter@gmail.com>
  4. */
  5. import java.util.*;
  6. public class BattleShip {
  7. public static void main(String[] args){
  8. Scanner input = new Scanner(System.in);
  9.  
  10. System.out.println("Welcome to battleship\n\n");
  11. char[][] ArrayPlayer= new char[10][10]; // Create an Array for the player
  12. Arrays MyPlayers = new Arrays(ArrayPlayer); // Set MyPlayers object to contain array ArrayPlayer, called from Arrays.java
  13. char[][] ArrayCPU= new char[10][10]; // Create an Array for the player
  14. Arrays CPUPlayers = new Arrays(ArrayCPU); // Set MyPlayers object to contain array ArrayPlayer, called from Arrays.java
  15. boolean[][] PlayerCheck = new boolean[10][10];
  16. boolean[][]CPUCheck = new boolean[10][10];
  17. Random generator = new Random(); // Create a random to be used later on.
  18.  
  19. //Patrol Boat
  20. Boat PatrolBoat = new Boat();
  21. PatrolBoat.health=2;
  22. PatrolBoat.name="Patrol Boat";
  23. PatrolBoat.letter='P';
  24.  
  25. //Submarine
  26. Boat Submarine = new Boat();
  27. Submarine.health=3;
  28. Submarine.name="Submarine";
  29. Submarine.letter='S';
  30.  
  31. //Destroyer
  32. Boat Destroyer = new Boat();
  33. Destroyer.health=4;
  34. Destroyer.name="Destroyer";
  35. Destroyer.letter='D';
  36.  
  37. //Battleship
  38. Boat BattleShip = new Boat();
  39. BattleShip.health=5;
  40. BattleShip.name="BattleShip";
  41. BattleShip.letter='B';
  42.  
  43. //CPU ship copy
  44. Boat CPUPatrolBoat = PatrolBoat;
  45. Boat CPUSubmarine = Submarine;
  46. Boat CPUDestroyer = Destroyer;
  47. Boat CPUBattleShip = BattleShip;
  48.  
  49.  
  50.  
  51. Boat SelectedBoat; // Create a boat called Selected Boat
  52. Boat[] boatarray = {PatrolBoat, Submarine, Destroyer, BattleShip}; // Boat array used in select, boat here gets applied to selectedboat
  53. Boat[] BoatCPU = {CPUPatrolBoat, CPUSubmarine, CPUDestroyer, CPUBattleShip};
  54. int iSelect, x=0, y=0, i=0, j=0,k=0, iOrientation, a, b, z; // initialize heaps of variables
  55.  
  56. //_________________________________________________________________________________________________________
  57. // Initializing arrays
  58. //_________________________________________________________________________________________________________
  59. //Initialize ArrayPlayer with Empty spaces
  60. for (i=0; i<ArrayPlayer.length; i++){
  61. for (j=0; j< ArrayPlayer.length; j++){
  62. ArrayPlayer[i][j]=' ';
  63. }
  64. }//End player init
  65.  
  66. for (i=0; i<ArrayCPU.length; i++){
  67. for (j=0; j< ArrayCPU.length; j++){
  68. ArrayCPU[i][j]=' ';
  69. }
  70. }//End true set.
  71.  
  72. //Initialize playerCheck array to true-- This is used later to check for collision.
  73. for (i=0; i<PlayerCheck.length; i++){
  74. for (j=0; j< PlayerCheck.length; j++){
  75. PlayerCheck[i][j]=true;
  76. }
  77. }//End true set.
  78.  
  79. //Same as above but for cpu
  80. for (i=0; i<CPUCheck.length; i++){
  81. for (j=0; j< CPUCheck.length; j++){
  82. CPUCheck[i][j]=true;
  83. }
  84. }//End true set.
  85. //_________________________________________________________________________________________________________
  86.  
  87.  
  88. //_________________________________________________________________________________________________________
  89. // Player Placement
  90. //_________________________________________________________________________________________________________
  91. for (i=0; i < 4; i++){
  92. 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
  93. iSelect =input.nextInt(); // Get value for select from array
  94. SelectedBoat=boatarray[iSelect-1]; // Select boat from array
  95. SelectedBoat.used +=1;
  96.  
  97. //Ensure boats aren't used more than once
  98. while (SelectedBoat.used > 1){
  99. System.out.println("This boat has already been used, try again: ");
  100. iSelect =input.nextInt(); // Get value for select from array
  101. SelectedBoat=boatarray[iSelect-1]; // Select boat from array
  102. SelectedBoat.used +=1;
  103. }
  104.  
  105. System.out.println("Press 1 for Vertical, 2 for Horizontal: "); // Prompt for horiz or vert.
  106. iOrientation = input.nextInt(); // Get horiz or vert
  107. while (iOrientation < 1 || iOrientation > 2){
  108. System.out.println("You have selected an incorrect value, try again: ");
  109. iOrientation=input.nextInt();
  110. }
  111. //Choose the boat's location and convert it from letter / number to just a letter.
  112. System.out.println("Choose where you would like the piece to appear on a grid (A1 is top left J10 is bottom right): ");
  113. String c="";//Empty string / create string / dereference string
  114. c = input.next(); // Allow user to place boat
  115. SelectedBoat.location = Boatloc.BoatLoc(c); // Transform String from letter / number to 2 numbers for grid placement
  116. x=SelectedBoat.location[0]; // Set y to y value
  117. y=SelectedBoat.location[1];// Set x to X value.
  118.  
  119.  
  120. //_________________________________________________________________________________________________________
  121. // Check if boats go out of bounds
  122. //_________________________________________________________________________________________________________
  123. z = SelectedBoat.health;
  124. if (iOrientation==1){
  125. while (y + SelectedBoat.health > 9){ // while ship will go out of bounds--vert
  126. System.out.println("Placing a boat here will make it go out of bounds, try again: ");
  127. c="";
  128. c = input.next(); // Allow user to guess
  129. SelectedBoat.location = Boatloc.BoatLoc(c); // Transform String from letter / number to 2 numbers for grid placement
  130. y=SelectedBoat.location[0]; // Set y to y value
  131. x=SelectedBoat.location[1];// Set x to X value.
  132. }
  133. }
  134. else if (iOrientation==2){// while ship will go out of bounds--Horiz
  135. while (x + SelectedBoat.health > 9){
  136. System.out.println("Placing a boat here will make it go out of bounds, try again: ");
  137. c="";
  138. c = input.next(); // Allow user to guess
  139. SelectedBoat.location = Boatloc.BoatLoc(c); // Transform String from letter / number to 2 numbers for grid placement
  140. y=SelectedBoat.location[0]; // Set y to y value
  141. x=SelectedBoat.location[1];// Set x to X value.
  142.  
  143. }
  144. }//End ori if
  145.  
  146. //_________________________________________________________________________________________________________
  147. // Actual piece placement
  148. //_________________________________________________________________________________________________________
  149.  
  150. //Vertical if Orientation
  151. if (iOrientation == 1){ // If orientation is vertical
  152. MyPlayers.AddBoatVert(x,y,SelectedBoat.health,SelectedBoat.letter);// Add boat letter at selected location
  153. PlayerCheck[x][y]=false;
  154. }// End vert orientation if/
  155.  
  156. //Horizontal if orientation
  157. if (iOrientation == 2){//If boat orientation is horizontal, place as such
  158. for (j=0; j <SelectedBoat.health; j++){ // While < health, place more
  159. MyPlayers.AddBoatHoriz(y,x, SelectedBoat.health,SelectedBoat.letter);// Add boat letter at selected location
  160. PlayerCheck[x][y]=false;
  161. x++;
  162. }// end placement for
  163. }// End horizontal orientation if
  164. Arrays.DisplayMap(ArrayPlayer);// Player graph output.
  165. }// End placement for
  166.  
  167.  
  168. /*
  169. //_________________________________________________________________________________________________________
  170. // CPU placement
  171. //_________________________________________________________________________________________________________
  172. boolean[] isOkay= new boolean [5];
  173. boolean isNotOkay=false;
  174. for (i=0; i<5; i++){
  175. isOkay[i]=false;
  176. }
  177. for (i=0; i<4; i++){
  178. x=generator.nextInt(9);//X location
  179. y=generator.nextInt(9);// Y location
  180. iOrientation=generator.nextInt(1)+1;//Orientation
  181. SelectedBoat=BoatCPU[i];
  182.  
  183. //Check out of bounds vert
  184. if (iOrientation==1){
  185. while (y + SelectedBoat.health > 9){
  186. y=generator.nextInt(); // Set y to y value
  187. }//End check while
  188. }//end orientation if
  189. //Check out of bounds horizontal
  190. if (iOrientation==2){
  191. while (x + SelectedBoat.health > 9){
  192. x=generator.nextInt(); // Set y to y value
  193. } //end check while
  194. }//end orientation if
  195.  
  196. a=x;
  197. b=y;
  198. if (iOrientation==1){
  199. while (b + SelectedBoat.health > 9){
  200. b=generator.nextInt(); // Set y to y value
  201. }//End check while
  202. }//end orientation if
  203. //Check out of bounds horizontal
  204. if (iOrientation==2){
  205. while (a + SelectedBoat.health > 9){
  206. a=generator.nextInt(); // Set y to y value
  207. } //end check while
  208. }//end orientation if
  209. for (j=0; j<SelectedBoat.health; j++){
  210. isOkay[j]= Arrays.ArrayCheckPlayer(CPUCheck, a, b);
  211. if (isOkay[j]){
  212. //null
  213. }
  214. else if(isOkay[j]==false){//is not okay.
  215. isNotOkay=true;
  216. }
  217. a++;
  218. }// Is Not Okay Set for.
  219.  
  220. if (isNotOkay==true){//if it is NOT okay
  221. i--;
  222. } // end Is Not Okay if
  223.  
  224. if (iOrientation==1 && isNotOkay == false){
  225. for (j=0; j<SelectedBoat.health; j++) {
  226. ArrayCPU[x][y]=SelectedBoat.letter;
  227. y++;
  228. }
  229. }
  230. else if (iOrientation==2 && isNotOkay == false){
  231. for (j=0; j<SelectedBoat.health; j++) {
  232. ArrayCPU[x][y]=SelectedBoat.letter;
  233. x++;
  234. }
  235. }
  236.  
  237. } // End CPU placement for
  238. *
  239. */
  240. }// End main
  241. }// End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement