我正在尝试为我一直在努力的课程完成作业。我应该使用位置类,arraylist和2D阵列对抗计算机来构建战舰游戏。用户在5x5板上获得8次猜测。我正在接触下面的指示,所以它更清楚。
我目前正试图检查用户猜测(行,列形式)是否与存储在arraylist中的船舶的位置对象匹配,但是,无论您输入什么,它总是将其评估为其他,并标记它作为一个小姐(也就是在棋盘上放置一个X)。我做错了什么?
directions page 1 directions page 2
到目前为止,这是我的代码:
驱动程序类:
import java.util.Random; import java.util.Scanner; import java.util.ArrayList;
public class battleshipDriver {
public static void main(String[] args) {
//fields
char [][] board = new char[5][5]; // game board array
ArrayList<Location> Location1 = new ArrayList<>(); // array list to hold location objects
initialBoard(board); // prints initial board state
Random computer = new Random(); //create num gen for computer placements
int row, col;
Scanner user = new Scanner(System.in);
//stuff that is doing things
//puts comp's placements in Location
for(int i = 0; i <= 4; i ++) {
row = computer.nextInt(5);
col = computer.nextInt(5);
Location battleship = new Location(row, col);
Location1.add(battleship);
}
System.out.println(Location1);
int turnsLeft = 8;
int numShips = 4;
do {
System.out.println("You have " + turnsLeft + " turns left." + "\n"
+ "There are " + numShips + " ships left.");
System.out.println("Please make a guess (row, column)");
row = user.nextInt();
col = user.nextInt();
Location userGuess = new Location(row, col);
if(row>4 || col>4 ) {
System.out.println("Your move is invalid.");
}
else if (board[row][col] == 'X' || board[row][col] == '*') {
System.out.println("You have already guessed that location");
}
for(Location loc: Location1) {
if(Location1.contains(userGuess)) {
Location1.remove(userGuess);
board[row][col] = '*';
updateBoard(board);
System.out.println("You hit a ship");
break;
}
else {
board[row][col] = 'X';
updateBoard(board);
break;
}
}
}while(turnsLeft != 0);
}
//printBoard method
public static void initialBoard(char[][] board) {
//for loops iterate through each
for(int row = 0; row< board.length; row++) {
for(int col = 0; col < board[row].length; col++) {
board [row][col] = 'O'; //assigns O to signify open water
//(this may need to change. Most likely
//will always make the board O's only
System.out.print(board[row][col] + " ");
}
System.out.println();
}
}
public static void updateBoard(char[][] board) {
for(int row = 0; row< board.length; row++) {
for(int col = 0; col < board[row].length; col++) {
System.out.print(board[row][col] + " ");
}
System.out.println();
}
}
}
位置等级:
public class Location {
private int row;
private int col;
//getters and setters
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public void setRow(int row) {
this.row = row;
}
public void setCol(int col) {
this.col = col;
}
//constructors
public Location(int row, int col) {
this.row = row;
this.col = col;
}
public String toString() {
return row + ", " + col ;
}
}
我目前有数组列表打印其内容,以便我可以输入已知的发货地点,看看我是否正常工作。
答案 0 :(得分:2)
Location类必须覆盖equals和ArrayList#contains(...)
的hashCode才能工作。这是你的问题及其解决方案。
创建row和col final字段并使用它们来检查相等性并计算hashCode(你必须只使用不变量来执行此操作)。
类似的东西:
package pkg1;
public class Location {
private final int row;
private final int col;
// getters and setters
public int getRow() {
return row;
}
public int getCol() {
return col;
}
// make the field immutable!
// public void setRow(int row) {
// this.row = row;
// }
// make the field immutable!
// public void setCol(int col) {
// this.col = col;
// }
// constructors
public Location(int row, int col) {
this.row = row;
this.col = col;
}
public String toString() {
return row + ", " + col;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + col;
result = prime * result + row;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Location other = (Location) obj;
if (col != other.col)
return false;
if (row != other.row)
return false;
return true;
}
}
ArrayList API中的contains(...)
方法条目:
如果此列表包含指定的元素,则返回true。更正式地说,当且仅当此列表包含至少一个元素e时才返回true(o == null?e == null:o.equals(e))。
如您所见,该方法使用.equals(...)
方法检查包含。