替换2d数组索引,如果球员位置有一个列表的值



所以现在当玩家进入一个位置(pos),比如A5,它将用空字符串替换板数组索引,如果船不在那里。但如果有船,我想用hitShip字符串替换它

public static void placePiece(String[][] board, String pos) {
String empty = "0";
String hitShip = "X";
playerTargets.add(pos);
switch (pos) {
case "A1":
board[1][2] = empty;
break;
case "B1":
board[1][4] = empty;
break;
case "C1":
board[1][6] = empty;
break;
case "D1":
board[1][8] = empty;
break;
case "E1":
board[1][10] = empty;
break;
case "F1":
board[1][12] = empty;
break;
case "G1":
board[1][14] = empty;
break;
case "H1":
board[1][16] = empty;
break;
case "I1":
board[1][18] = empty;
break;
case "J1":
board[1][20] = empty;
break;

对于所有坐标(A1-J10)

ship1有值"A6","A7"和ship2有值"F1","F2".

List ship1 = Arrays.asList(ar[0][0], ar[1][0]);
List ship2 = Arrays.asList(ar[2][0], ar[3][0]);

我有这段代码打印命中/未命中消息取决于玩家是否击中船,但我不确定如何使用它来替换板数组的索引与hitShip变量。

// CHECKS IF THE PLAYER TARGETS HIT SHIP1
boolean hit = false;
for (Object string : ship1) {
if (pos.contains((CharSequence) string)) {
hit = true;
break;
}
}
// CHECKS IF THE PLAYER TARGETS HIT SHIP2
for (Object string : ship2) {
if (pos.contains((CharSequence) string)) {
hit = true;
break;
}
}
// PRINTS HIT/MISS MESSAGE DEPENDING ON WETHER THE PLAYER HIT A SHIP
if (hit) {
System.out.println("nHit!!!");
} else {
System.out.println("nMiss!!!");
}

编辑代码:

package intermediate;
import java.io.*;
import java.util.*;
import javax.*;
public class Battleship {
static ArrayList<String> playerTargets = new ArrayList<String>();
private static String[][] ships;
public static void main(String[] args) throws FileNotFoundException {
String[][] board = {
{ " ", " ", "A", " ", "B", " ", "C", " ", "D", " ", "E", " ", "F", " ", "G", " ", "H", " ", "I", " ",
"J" },
{ "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "2", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "3", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "4", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "5", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "6", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "7", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "8", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "9", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" " },
{ "1", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " " } };
System.out.println("Let's Play Battleship!");
printBoard(board);
for (int i = 1; i < 31; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose your target Ex.(A3): ");
String pos = scanner.next();
while (playerTargets.contains(pos)) {
System.out.println("Position taken! Please enter a valid position: ");
pos = scanner.next();
}
placePiece(board, pos);
String result = ships(board, pos);
// ENDS THE PROGRAM IF USER HAS HIT ALL THE SHIPS
if (result.length() > 0) {
printBoard(board);
System.out.println();
System.out.println(result);
break;
}
System.out.println("You have " + (30 - i) + " missiles left");
printBoard(board);
}
}
public static void printBoard(String[][] board) {
for (String[] row : board) {
for (String c : row) {
System.out.print(c);
}
System.out.println();
}
}
public static String ships(String[][] board, String pos) throws FileNotFoundException {
// READS SHIP POSITIONS FROM SHIP FILE
String filePath = "/Users/Otez/Desktop/ships.txt";
List<String[]> list = new ArrayList<>();
try {
Scanner scanner = new Scanner(new File(filePath));
while (scanner.hasNextLine()) {
String[] arr = scanner.nextLine().split(",");
for (String item : arr) {
list.add(new String[] { item });
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
String[][] ar = list.toArray(String[][]::new);
ships = new String[] {
new String[] { ar[0][0], ar[1][0] },
new String[] { ar[2][0], ar[3][0] }
};
// CHECKS IF THE PLAYER TARGETS HIT SHIP1
boolean hit = false;
for (Object string : ships) {
if (pos.contains((CharSequence) string)) {
hit = true;
break;
}
}
// PRINTS HIT/MISS MESSAGE DEPENDING ON WHETHER THE PLAYER HIT A SHIP
if (hit) {
System.out.println("nHit!!!");
} else {
System.out.println("nMiss!!!");
}
// CHECKS IF PLAYER TARGETS HIT THE SHIPS ON THE BOARD
if (playerTargets.containsAll(ships)) {
return "YOU SANK MY ENTIRE FLEET!!!n";
}
return "";
}
public static void placePiece(String[][] board, String pos) {
String empty = "0";
String hitShip = "X";
playerTargets.add(pos);
int firstIndex = Character.getNumericValue(pos.charAt(1));
int secondIndex = (pos.charAt(0) - 64) * 2;
board[firstIndex][secondIndex] = empty;
String characterToPlace = empty;
if (isShip(board, pos, ships)) {
characterToPlace = hitShip;
}
board[firstIndex][secondIndex] = characterToPlace;
}
public static boolean isShip(String[][] board, String pos, String[][] ar) {
for (String[] ship : ships) {
for (String shipPos : ship) {
if (shipPos.contains(pos)) {
return true;
}
}
}
return false;
}
}

如您所见,编写和维护大量的switch语句是一个不那么优雅的解决方案。我建议你做的第一件事,就是清理代码。仅从观察它,我就可以看到,随着pos的第一个字符中的字母在字母表中前进,您在board上使用的第二个索引增加了2。

那么让我们用ASCII的魔力来解析这些重复的代码:

public static void placePiece(String[][] board, String pos) {
String empty = "0";
String hitShip = "X";
playerTargets.add(pos);
int firstIndex = Character.getNumericValue(pos.charAt(1));
int secondIndex = (pos.charAt(0) - 64) * 2;
board[firstIndex][secondIndex] = empty;
}

现在无论pos是什么,都被标记为空。如果我是你,我现在会有一个单独的方法来确定船是否被击中-这是一个很好的实践,尝试使你的代码更模块化。

在我看来,你真的不需要使用列表,你可以只使用数组你的船:

String[] ship1 = new String[] {ar[0][0], ar[1][0]};
String[] ship2 = new String[] {ar[2][0], ar[3][0]};
//and we'll have an array of all these ships just to make things easier
String[][] ships = new String[] {ship1, ship2};

这些变量最好是全局的,因为我们不需要每次检查用户是否撞到船时都创建它们。

现在我们将创建一个函数来确定某个位置是否有一艘船——记住,模块化代码更容易更改和维护。我写的和你写的非常相似,只是在使用舰船数组时它变得更短了。

public static boolean isShip(String[][] board, String pos) {
for (String[] ship : ships) {
for (String shipPos : ship) {
if (shipPos.contains(pos)) {
return true;
}
}
}
return false;
}

现在,我们将新的isShip方法合并到重写的placePiece方法中:

public static void placePiece(String[][] board, String pos) {
String empty = "0";
String hitShip = "X";
playerTargets.add(pos);
int firstIndex = Character.getNumericValue(pos.charAt(1));
int secondIndex = (pos.charAt(0) - 64) * 2;
String characterToPlace = empty;
if (isShip(board, pos)) {
characterToPlace = hitShip;
}
board[firstIndex][secondIndex] = characterToPlace;
}

我没有测试代码,但从看它,它应该都工作。通过阅读代码,我建议您更多地了解:

  • 以模块化的方式构建你的代码
  • 写"干净"的代码

我建议在学习泛型之前不要使用Lists。


编辑:经过我们在评论中的讨论,并阅读您编辑的代码。我建议不要写:

String[] ship1 = new String[] { ar[0][0], ar[1][0] };
String[] ship2 = new String[] { ar[2][0], ar[3][0] };
String[][] ships = new String[] { ship1, ship2 };

你应该写:

private static String[][] ships;

,然后在ships方法中,在String[][] ar = list.toArray(String[][]::new);行之后,您可以编写以下内容:

ships = new String[] {
new String[] {ar[0][0], ar[1][0]},
new String[] {ar[2][0], ar[3][0]}
};

相关内容

最新更新