私が取り組んでいるクラスの課題を完成しようとしています。私は、コンピュータに対して位置クラス、arraylist、および2D配列を使用して戦艦ゲームを構築することになっています。ユーザーは5x5ボードで8つの推測を得ます。私は以下の指示に従っているので、より明確です。arraylistを使ったJava戦艦:ユーザー入力を配列リストと比較する
arraylistに保存されている船の位置オブジェクトにユーザーが(行、列形式で)推測しているかどうかを確認しようとしていますが、入力内容に関係なく常にelseとして評価しています。それをミスとしてマークします(別名ボード上にXを配置します)。私は間違って何をしていますか?ここでdirections page 2
directions page 1 は、これまでの私のコードです:
ドライバクラス:
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 ;
}
}
私は現在、配列リストの印刷を持って内容私はそれが正常に動作するかどうかを知るために私はちょうど知られている船の場所を入力することができます。
コードが期待どおりに動作しない場合は、質問をする前にまずデバッグを行ってから、その情報を使用して、問題の原因と原因を特定する必要があります。これについてどうやって行くのかわからない場合は、[小さなプログラムをデバッグする方法]を見てください。(http://ericlippert.com/2014/03/05/how-to-debug-small-プログラム/)。それはあなたの直接の問題を解決することはできませんが、それはあなたがそれをあなた自身で解決するのに役立ちますあなたが従うことができる手順を与えるでしょう。 –
それでも問題が解決しない場合でも、質問をより集中して簡単に答えることができるように、少なくとも問題を分離するのに役立ちます。 –
私は信じています。私は、arrayListにuserGuessが正しく評価されていないかどうかを調べるif文に絞り込んだ。正しく評価されていないかどうかを調べるにはどうすればいいのだろうか。 – TheMidget149