Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 60
  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    You need to create an instance of the Run class named run: Run run = new Run();

    -1 is not a valid array index. Make sure to use numbers from 0 to array length-1

    Every time you call randomrow() you should get a different value. You use its return value to test if a square is occupied and then get a new number to make the move. Call it once and use that value instead of getting new values.
    Why do you subtract one ? What would 0 - 1 give you?


    Please post the full text of the compiler error messages.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    not quite sure where to put the Run run = new Run(); ... in the Battleship (main) or in the run.java class? Also getting the error where i have
    Java Code:
    run.attack(pos, compfield);
    			run.compattack(pos, myfield);
    in Battleship.java saying:
    Cannot make a static reference to the non-static method attack(int[][], int[][]) from the type run

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    Put the definition of the run variable and the creation of an instance of the Run class before you try to use it. Just before the while loop for example.

    Java coding standards say class names should begin with uppercase letters. The class name should be Run.
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    This is what i have now, and am getting the same static reference error
    Java Code:
    Run run = new Run();
    		while (hit < 20 && comphit < 20) {
    			System.out.println("     Your Field");
    			System.out.println("");
    			board.displayField(myfield);
    			System.out.println("     Computer Field");
    			System.out.println("");
    			comboard.displayField(compfield);
    			Run.attack(pos, compfield);
    			Run.compattack(pos, myfield);
    		}

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    Create an instance of the Run class:
    Run run = new Run();

    Then use run in the loop:
    run.attack(...
    If you don't understand my response, don't ignore it, ask a question.

  6. #26
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    got it! thanks again... now something is up when the computer gets a hit on me, it points me to the following line of code
    Java Code:
    game.setCompHits();
    and gives me the error:
    Exception in thread "main" java.lang.NullPointerException
    at battleship.Run.compattack(Run.java:81)
    at battleship.Battleship.run(Battleship.java:83)
    at battleship.Battleship.main(Battleship.java:46)
    for reference this is what i have in my Battleship class, or the relevant parts that is..
    Java Code:
    	public int getHits() {
    		return hit;
    	}
    	
    	public void setHits() {
    		hit ++;
    	}
    	
    	public int getCompHits() {
    		return comphit;
    	}
    	
    	public void setCompHits(){
    		comphit ++;
    	}
    	
    	public int getLastMove(){
    		return lastmove;
    	}
    	
    	public void setLastMove(){
    		lastmove = 3;
    	}

  7. #27
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    Exception in thread "main" java.lang.NullPointerException
    at battleship.Run.compattack(Run.java:81)
    Look in the compattack method at line 81. What variable is null on that line? Then backtrack in the program to find out why that variable does not have a valid value.

    What is the value of the variable: game at the line of code you posted?
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    of game? i declared it as
    Java Code:
    public final class Run {
    	private Battleship game;
    the value SHOULD be equal to 0 right? HEre is where i declared the value in Battleship
    Java Code:
    private int comphit = 0;
    and my setCompHits method in Battleship
    Java Code:
    public void setCompHits(){
    		comphit++;
    	}

  9. #29
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    Where do you assign a value to the game variable? It must have the address of the instance of the Battleship class that is where the methods and values are being kept. You could pass a reference to it when you call the Run classes constructor by using the this reference variable: = new Run(this); and then in Run's constructor save the passed value in the game variable.
    If you don't understand my response, don't ignore it, ask a question.

  10. #30
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    wait, i am confused. All i am trying to do is access the setCompHits() method in the Battleship class to simply add one to the comphits value.

  11. #31
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    You need a reference to the Battleship object to call its methods.
    If you don't understand my response, don't ignore it, ask a question.

  12. #32
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    I am confused as to where to put what you suggested

  13. #33
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    Which part?
    Create an instance of Run?
    Save the passed parameter?
    If you don't understand my response, don't ignore it, ask a question.

  14. #34
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    Ok got that to work as well. My next problem i noticed is that the following code:
    Java Code:
    public int randomrow() {
    		Random r = new Random();
    		int randrow = r.nextInt(9);
    		return randrow;
    	}
    I believe is a problem, because it could possibly generate a 0 couldnt it? Won't it be a random number from 0-9? How would i go about fixing this, because it is currently throwing off my row and columns by 1 sometimes.

  15. #35
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    0 to 9 are good values for indexing into an array of 10 elements.
    Won't it be a random number from 0-9?
    When in doubt about what a method does, go read about it in the AP doc.
    If you don't understand my response, don't ignore it, ask a question.

  16. #36
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    Getting a couple of weird errors and have a few final questions.
    1. I am sometimes getting a "shot" by the computer where the number is 10, and obviously, will not show up as a miss or a number "2" on myField. It's a sorta rare appearance, but would like to hear suggested culprits
    FROM the console:
    Computer Missed at 10 , 8
    lastmove now = 0

    Your Field

    ----------------

    1 2 3 4 5 6 7 8 9
    -----------------
    1 | 0 0 0 0 0 0 0 0 0
    2 | 0 0 0 0 0 0 0 0 0
    3 | 0 0 0 0 0 0 1 0 0
    4 | 0 0 0 0 0 0 1 0 0
    5 | 0 0 0 0 0 0 1 0 0
    6 | 0 0 0 1 1 0 1 0 0
    7 | 0 0 0 1 1 0 0 0 0
    8 | 0 0 0 0 1 0 0 0 0
    9 | 0 0 1 1 1 1 1 0 0
    2.
    Java Code:
    private boolean lastmove = false;
    I've declared that boolean in the Battleship class, and want to be able to modify it in the Run class, do i have to create a getter and setter for the boolean, and what would these look like?

    3. I am trying to come up with a way to instill a simple AI, when the boolean mentioned above is true, it will enter a different method in Run other than compattack called compattackhit and somehow check to see if nearby spaces from the coordinates of the last hit shot are legal, and if they are fire. Not sure on how to maximize the AI on this, and could use some insight.
    Last edited by Lucid15; 04-09-2012 at 05:34 AM.

  17. #37
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    The grid you print looks like it is one column and one row short of showing all 10.

    A getter returns the value, a setter sets the value.

    Save the x,y of the last hit to be able to shoot near it. When you get 2 hits you know the ship's orientation and can continue down the line
    If you don't understand my response, don't ignore it, ask a question.

  18. #38
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    So it seems for this to happen I need to come up
    With a way to determine if a particular ship has been "sunk", any suggestions?

  19. #39
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Help with Battleship Game

    Does the ship know where it is located? Can it keep track of each hit it receives?
    If you don't understand my response, don't ignore it, ask a question.

  20. #40
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    I do not think it knows where its located, it knows it size and name, how would i keep track of the hits on each ship? make a new constructor in the ship class? I am not sure how i would do this, with respect to its location that i randomly generate.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Battleship game - arrays
    By makig in forum New To Java
    Replies: 9
    Last Post: 11-17-2011, 07:21 PM
  2. Battleship Game - really need help
    By thrashsynergy in forum Java Applets
    Replies: 5
    Last Post: 04-25-2011, 07:21 PM
  3. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM
  4. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 01:09 AM
  5. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •