java
1import java.util.Scanner; 2public class yay{ 3 public static void main(String [] args){ 4 Scanner input = new Scanner(System.in); 5 6 7 boolean winTheGame = false; 8 9 boolean p = false; 10 boolean p1 = false; 11 boolean p2 = false; 12 13 boolean b = false; 14 boolean b1 = false; 15 boolean b2 = false; 16 boolean b3 = false; 17 18 boolean d = false; 19 boolean d1 = false; 20 boolean d2 = false; 21 boolean d3 = false; 22 boolean d4 = false; 23 24 while(!winTheGame){//while not all sunk 25 String guess = input.nextLine(); 26 27 int row = guess.charAt(0) - 'A'; 28 int col = guess.charAt(1) - '1'; 29 30 int [][] ships = new int [6][6]; 31 char [][] guesses = new char [6][6]; 32 33 boolean hitOrMiss = false; 34 35 36 37 if(guess.equals("A1")){ 38 System.out.println("A1 is a hit"); 39 p1 = true; 40 if(p2){ 41 System.out.println("You sunk the patrol boat"); 42 } 43 } 44 if(guess.equals("B1")){ 45 System.out.println("B1 is a hit"); 46 p2 = true; 47 if(p1){ 48 System.out.println("You sunk the Patrol Boat"); 49 p = true; 50 } 51 } 52 53 54 if(guess.equals("B5")){ 55 System.out.println("B5 is a hit"); 56 b1 = true; 57 if(b2 && b3){ 58 System.out.println("You sunk the destroyer"); 59 } 60 } 61 if(guess.equals("B6")){ 62 System.out.println("B6 is a hit"); 63 b2 = true; 64 if(b1 && b3){ 65 System.out.println("You sunk the destroyer"); 66 } 67 } 68 if(guess.equals("B7")){ 69 System.out.println("B7 is a hit"); 70 b3 = true; 71 if(b1 && b2){ 72 System.out.println("You sunk the destoryer"); 73 b = true; 74 } 75 } 76 77 78 if(guess.equals("F3")){ 79 System.out.println("F3 is a hit"); 80 d1 = true; 81 if(d2 && d3 && d4){ 82 System.out.println("You sunk the battleship"); 83 } 84 } 85 if(guess.equals("F4")){ 86 System.out.println("F4 is a hit"); 87 d2 = true; 88 if(d1 && d3 && d4){ 89 System.out.println("You sunk the battleship"); 90 } 91 } 92 if(guess.equals("F5")){ 93 System.out.println("F5 is a hit"); 94 d3 = true; 95 if(d1 && d2 && d4){ 96 System.out.println("You sunk the battleship"); 97 } 98 } 99 if(guess.equals("F6")){ 100 System.out.println("F6 is a hit"); 101 d4 = true; 102 if(d1 && d2 && d3){ 103 System.out.println("You sunk the battleship"); 104 d = true; 105 } 106 } 107 108 109 if(p && b && d){ 110 winTheGame = true; 111 } 112 113 114 } 115 116 System.out.println("You win"); 117 } 118 119}
**上記のコード、動作するのですが、プログラミングの本質が違うことに築いたので書き直したいです。
本来は二次元配列を2つ使ってinputと比較するつもりでした。
int[][] ships = new int [6][6];
char [][] guesses = new char[6][6];
これで2つのボードを比較します。
なのでif条件判断文ではguessで指定した位置にshipがあればtrueで中に入り込ませます。
**
あなたの回答
tips
プレビュー