这个项目是使用数组制作一个2d战舰项目。如果你击中,它会产生一个" X。"如果它是向上,向下,向右或向左一个槽,那么它就是" H"为了热。如果距离它的两个位置,那就是" W" W" W" W" W" W"为了温暖。 " C"对于三个插槽来说很酷,而对于其他一切都没有。 每次我去测试我的代码时,它第一次完全正常工作,但之后它将每个输入标记为命中,当它应该标记为" H"," W&# 34;," C"或" M。"
import java.util.Scanner;
public class Battleship
{
public static void main ()
{
int rowLoc = 0;
int colLoc = 0;
int rowGuess;
int colGuess;
int guesses = 1;
int shipNum = 5;
//variable declaration
String[][] battleGrid = new String[10][10];
String[][] battleGridKnown = new String[10][10];
//grid declaration
System.out.println("Welcome to Battleship!\nFive ships are hidden in row 0-9 and columns 0-9.");
System.out.println("After each guess, you will been told if your guess was...");
System.out.println("- Hit (X)\n- Close (H for hot)\n- Somewhat close (W for warm)\n- Not that close (C for cool)\n- Miss (M).");
//welcome message
for (int r = 0; r < battleGrid.length; r++)
{
for (int c = 0; c < battleGrid[r].length; c++)
{
battleGrid[r][c] = "-";
}
}
//initializes what is in battleGrid by default
for (int r = 0; r < battleGridKnown.length; r++)
{
for (int c = 0; c < battleGridKnown[r].length; c++)
{
battleGridKnown[r][c] = "-";
}
}
//initializes what is in battleGridKnown by default
for (int k = 1; k <= shipNum; k++)
{
do
{
rowLoc = (int)(Math.random() * 10 + 0);
colLoc = (int)(Math.random() * 10 + 0);
} while(battleGrid[rowLoc][colLoc] == "S");
battleGrid[rowLoc][colLoc] = "S";
}
//sets ships and also the stuff around the ships
/*for (int r = 0; r < battleGrid.length; r++)
{
for (int c = 0; c < battleGrid[r].length; c++)
{
System.out.print(battleGrid[r][c] + "");
}
System.out.println("");
}
System.out.println("");
//test printer*/
while(shipNum > 0)
{
System.out.print("Enter your row guess (0-9): ");
Scanner in = new Scanner (System.in);
rowGuess = in.nextInt();
System.out.print("Enter your column guess (0-9): ");
colGuess = in.nextInt();
// enter row + column guesses
if (battleGrid[rowGuess][colGuess] == battleGrid[rowLoc][colLoc])
{
shipNum--;
battleGridKnown[rowGuess][colGuess] = "X";
System.out.println("Hit! There are " + shipNum + " ships remaining.");
System.out.println("Guess attempt number: " + guesses + ".");
for (int r = 0; r < battleGridKnown.length; r++)
{
for (int c = 0; c < battleGridKnown[r].length; c++)
{
System.out.print(battleGridKnown[r][c] + "");
}
System.out.println("");
}
System.out.println("");
}
else if(rowGuess == rowLoc-- || rowGuess == rowLoc++ || colGuess == colLoc-- || colGuess == colLoc++)
{
battleGridKnown[rowGuess][colGuess] = "H";
System.out.println("Hot!");
System.out.println("Guess attempt number: " + guesses + ".");
for (int r = 0; r < battleGridKnown.length; r++)
{
for (int c = 0; c < battleGridKnown[r].length; c++)
{
System.out.print(battleGridKnown[r][c] + "");
}
System.out.println("");
}
System.out.println("");
}
else if(rowGuess == rowLoc - 2 || rowGuess == rowLoc + 2 || colGuess == colLoc - 2 || colGuess == colLoc + 2)
{
System.out.println("Warm...");
battleGridKnown[rowGuess][colGuess] = "W";
System.out.println("Guess attempt number: " + guesses + ".");
for (int r = 0; r < battleGridKnown.length; r++)
{
for (int c = 0; c < battleGridKnown[r].length; c++)
{
System.out.print(battleGridKnown[r][c] + "");
}
System.out.println("");
}
System.out.println("");
}
else if(rowGuess == rowLoc - 3 || rowGuess == rowLoc + 3 || colGuess == colLoc - 3 || colGuess == colLoc + 3)
{
System.out.println("Cool...");
battleGridKnown[rowGuess][colGuess] = "C";
System.out.println("Guess attempt number: " + guesses + ".");
for (int r = 0; r < battleGridKnown.length; r++)
{
for (int c = 0; c < battleGridKnown[r].length; c++)
{
System.out.print(battleGridKnown[r][c] + "");
}
System.out.println("");
}
System.out.println("");
}
else
{
System.out.println("Miss.");
System.out.println("Guess attempt number: " + guesses + ".");
for (int r = 0; r < battleGridKnown.length; r++)
{
for (int c = 0; c < battleGridKnown[r].length; c++)
{
battleGridKnown[rowGuess][colGuess] = "M";
System.out.print(battleGridKnown[r][c] + "");
}
System.out.println("");
}
System.out.println("");
}
guesses++;
}
System.out.println("Thank you for playing!\nYou found all the ships in " + guesses + " guesses.");
}
}