2017-09-10 10 views
1

私が取り組んでいるクラスの課題を完成しようとしています。私は、コンピュータに対して位置クラス、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 ; 
    } 
} 

私は現在、配列リストの印刷を持って内容私はそれが正常に動作するかどうかを知るために私はちょうど知られている船の場所を入力することができます。

+0

コードが期待どおりに動作しない場合は、質問をする前にまずデバッグを行ってから、その情報を使用して、問題の原因と原因を特定する必要があります。これについてどうやって行くのかわからない場合は、[小さなプログラムをデバッグする方法]を見てください。(http://ericlippert.com/2014/03/05/how-to-debug-small-プログラム/)。それはあなたの直接の問題を解決することはできませんが、それはあなたがそれをあなた自身で解決するのに役立ちますあなたが従うことができる手順を与えるでしょう。 –

+0

それでも問題が解決しない場合でも、質問をより集中して簡単に答えることができるように、少なくとも問題を分離するのに役立ちます。 –

+0

私は信じています。私は、arrayListにuserGuessが正しく評価されていないかどうかを調べるif文に絞り込んだ。正しく評価されていないかどうかを調べるにはどうすればいいのだろうか。 – TheMidget149

答えて

2

ArrayList#contains(...)を動作させるには、LocationクラスがequalsおよびhashCodeをオーバーライドする必要があります。それがあなたの問題とその解決策です。

行と列の最終フィールドを作成し、それらを使用して等価性をチェックし、hashCodeを計算します(これを行うには、不変式を使用する必要があります)。以下のような

何か:ArrayList APIcontains(...)メソッドエントリで

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; 
    } 
} 

:このリストに指定された要素が含まれている場合

trueを返します。より正式には、このリストに(o == null?e == null:o.equals(e))などの少なくとも1つの要素eが含まれている場合にのみtrueを返します。

このように、このメソッドでは、.equals(...)メソッドを使用して包含をチェックしています。

+0

これは多くの複製があることを知っているので、コミュニティのwikiとして回答しています。 –

+1

ありがとうございました!私たちはそれについてまだ教えられていないし、私は本書のカバーされた章のすべてを読んだ。もっと個人的な調査をする時間があると思います! – TheMidget149

+0

@ TheMidget149:それは本当です。APIを使い始める。この問題の原因を示すリンクを含め、私の答えの一番下にある編集に注目してください。 –

関連する問題