我正在使用课程创建战舰游戏。游戏首先询问2名玩家的名字然后要求玩家设置2个点。玩家来回捡点,直到船沉没并显示胜利者。
我坚持的地方是尝试将这些点保存到具有相应玩家名称的2D阵列中。我想为两位球员使用一分,只是为了做空。我将不胜感激任何建议。
runBattleship主类:输入和输出
Scanner keyboard = new Scanner(System.in);
Players p1 = new Players();
Players p2 = new Players();
Grid px = new Grid();
Grid py = new Grid();
System.out.print("Welcome to Battleship game");
System.out.println("");
System.out.print("Player 1 enter name: ");
p1.setName(keyboard.next());
System.out.print("Player 2 enter name: ");
p2.setName(keyboard.next());
System.out.println("");
System.out.println("Player 1 select point x and y (5x5 grid)");
px.setPointX(keyboard.nextInt());
py.setPointY(keyboard.nextInt());
玩家类:玩家的getter和setter名称,并从Grid调用对象以获得积分。
String name;
int pointX, pointY;
Players(){
name = "";
}
public Players(String name, Grid pointX, Grid pointY){
this.name = name;
this.pointX = pointX.getPointX();
this.pointY = pointY.getPointY();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
网格类:点用户输入的getter和setter
Ship[][] board = new Ship[5][5];
int pointX, pointY;
public int getPointX() {
return pointX;
}
public void setPointX(int pointX) {
this.pointX = pointX;
}
public int getPointY() {
return pointY;
}
public void setPointY(int pointY) {
this.pointY = pointY;
}
船级:我计划在适当的地方保存船只的地方
String name;
int px, py;
public void placeShipAt(Grid pointX, Grid pointY, Players name){
this.name = name.getName();
this.px = pointX.getPointX();
this.py = pointY.getPointY();
}