Java battleship using arraylist: comparing user input to array list -


i trying finish assignment class have been working on. supposed build battleship game using location class, arraylist, , 2d array against computer. user gets 8 guesses on 5x5 board. attacthing directions below more clear.

i stuck trying check if users guess (in row, col form) matches location object of ship stored in arraylist, however, no matter input, evaluates else, , marks miss (aka places x on board). doing wrong?

directions page 1 directions page 2

here code far:

driver class:

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 hold location objects         initialboard(board); // prints initial board state         random computer = new random(); //create num gen computer placements         int row, col;         scanner user = new scanner(system.in);          //stuff doing things         //puts comp's placements in location         for(int = 0; <= 4; ++) {             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;         {             system.out.println("you have " + turnsleft + " turns left." + "\n"                 + "there " + numships + " ships left.");             system.out.println("please make 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 invalid.");             }              else if (board[row][col] == 'x' || board[row][col] == '*') {                 system.out.println("you have guessed location");             }             for(location loc: location1) {                 if(location1.contains(userguess)) {                     location1.remove(userguess);                     board[row][col] = '*';                     updateboard(board);                     system.out.println("you hit 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 signify open water                  //(this may need change.                  //will make board o's                 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();         }       } } 

location class:

public class location {     private int row;     private int col;      //getters , 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 ;     } } 

i have array list printing contents can input known ship locations see if working properly.

the location class must override equals , hashcode arraylist#contains(...) work. that's problem , solution.

make row , col final fields , use them check equality , calculate hashcode (you must use invariants this).

something like:

package pkg1;  public class location {     private final int row;     private final int col;      // getters , setters     public int getrow() {         return row;     }      public int getcol() {         return col;     }      // make field immutable!     // public void setrow(int row) {     // this.row = row;     // }      // make 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;     } } 

in contains(...) method entry in arraylist api:

returns true if list contains specified element. more formally, returns true if , if list contains @ least 1 element e such (o==null ? e==null : o.equals(e)).

so can see, method uses .equals(...) method check containment.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -