Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Battleship Program Coding Assistance

  1. #1
    nekaneka19 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Battleship Program Coding Assistance

    My goal is to create a java program which will create a modified version of battleship using the following methods:


    public static void main(String args[])
    The main method.

    private static void readFile(String filename, int[][] player)
    The method to populate the array for each player.

    private static boolean allSunk(int[][] player)
    The method to check if all ships are sunk.

    private static String print(int[][] player)
    The method to print the board.

    private static String checkSunk(int[][] player)
    The method to check for a particular ship sunk on the board (this is called inside the
    print method)

    Below is my program that I have now, and I was curious if anyone can help me debug it, and/or put me on the right path to completing it.
    Java Code:
    import java.util.StringTokenizer;
    
    import javax.swing.JOptionPane;
    
    //Note: Failed to get program running correctly
    public class Battleship {
    	public static void main(String[] args) {
    		// Declare board
    		int[][] player1 = new int[5][5];
    		int[][] player2 = new int[5][5];
    
    		// Reads in files
    		readFile("player1Spring2012.txt", player1);
    		readFile("player2Spring2012.txt", player2);
    
    		boolean turn = true;
    		while (true) {
    			String input = null;
    			if (true) {
    				input = JOptionPane
    						.showInputDialog("Player 1, enter a coordinate: \nPlayer 2's board: \n"
    								+ print(player2));
    			} else {
    				input = JOptionPane
    						.showInputDialog("Player 2, enter a coordinate: \nPlayer 1's board: \n"
    								+ print(player1));
    			}
    			if (input == null || input.equals("-1")) {
    				return;
    			}
    			StringTokenizer st = new StringTokenizer(input, " , ");
    			int r = Integer.parseInt(st.nextToken());
    			int c = Integer.parseInt(st.nextToken());
    		}
    	}
           
            //Method that reads in a file.
    	public static void readFile(String filename, int[][]) {
    		TextFileInput tfi = new TextFileInput(filename);
    
    		String line = tfi.readLine();
    		while (line != null) {
    			StringTokenizer st = new StringTokenizer(line, "|");
    			int tokens = st.countTokens();
    			while (st.hasMoreTokens()) {
    				StringTokenizer st1 = new StringTokenizer(st.nextToken(), ", ");
    				int r = Integer.parseInt(st1.nextToken());
    				int c = Integer.parseInt(st1.nextToken());
    			}
    		}
    	}
    
    	// Method will print the board.
    	private static String print(int[][] player) {
    	String rtn = "";
    	for (int i = 0; i < player.length; i++){
    		for (int j = 0; j< player[i].length; j++){
    			if(player[i][j]==-1) {
    				rtn  = "H";}
    			else if (player[i][j] == -2){
    				rtn += "X";}
    			else rtn += "+";
    			}
    		rtn += "/n"; }
    
    	//Helper method which will check if a ship has sunk (Called within print method)
    private static String checkSunk(int[][] player) {
    
    	
    }
    }
    
    	// Method will check if the game is over.
    	public static boolean allSunk(int[][] arr, int r, int c) {
    		for (int i = 0; i < arr.length; i++) {
    			for (int j = 0; j < arr[i].length; j++) {
    				if (arr[i][j] > 0)
    					return false;
    			}
    		}
    		return true;
    	}
    }
    Last edited by nekaneka19; 03-21-2012 at 06:21 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Battleship Program Coding Assistance

    Do you have any specific questions or problems?
    Please ask them.

    Please edit your code and give it proper formatting. It has no indentation for nesting levels of logic which makes it hard to read.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Battleship Program Coding Assistance

    The indentation and formatting would have been lost in the course of copying from the cross post. Laziness knows no bounds.
    Java- BattleShip Programming Issue? - Yahoo! Answers

    db
    nekaneka19 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    nekaneka19 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Battleship Program Coding Assistance

    Quote Originally Posted by Norm View Post
    Do you have any specific questions or problems?
    Please ask them.

    Please edit your code and give it proper formatting. It has no indentation for nesting levels of logic which makes it hard to read.
    My problem is that I am unable to get the program to run, mainly, I am unable to apply the logic of what I need to do into coded form.

    Given that this is a Hw assignment, I don't want the answer straight given to me, but any assistance on how I should approach it would greatly be appreciated.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Battleship Program Coding Assistance

    Quote Originally Posted by nekaneka19 View Post
    My problem is that I am unable to get the program to run, mainly, I am unable to apply the logic of what I need to do into coded form.

    Given that this is a Hw assignment, I don't want the answer straight given to me, but any assistance on how I should approach it would greatly be appreciated.
    Again, please put in the effort to present readable well-formatted code. Most of us won't put in the effort to read your code if you won't put in the effort to format it correctly.

    Next, tell us if your current code doesn't compile or if it compiles, does it show any exceptions, and if so, post the full messages.

  6. #6
    nekaneka19 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Battleship Program Coding Assistance

    Quote Originally Posted by Fubarable View Post
    Again, please put in the effort to present readable well-formatted code. Most of us won't put in the effort to read your code if you won't put in the effort to format it correctly.

    Next, tell us if your current code doesn't compile or if it compiles, does it show any exceptions, and if so, post the full messages.
    The current code I have posted doesn't compile and has the following two errors:

    Line 39: TextFileInput cannot be resolved to a type.
    Line 67: Illegal modifier for parameter checkSunk; only final is permitted.
    Last edited by nekaneka19; 03-21-2012 at 06:21 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Battleship Program Coding Assistance

    Do you have a class called TextFileInput?

    For the second one, I'd recommend laying out your code as you do in the readFile method, that would show you where your brackets are going wrong.
    Please do not ask for code as refusal often offends.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Battleship Program Coding Assistance

    It looks like you are missing a class definition for TextFileInput.


    Check that the {}s are properly paired. The code has a very poor technique of hiding }s.
    }s should be on a line by themselves and vertically in line below the start of the line with the pairing {

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

Similar Threads

  1. Having trouble, Battleship program
    By Corrrrr in forum New To Java
    Replies: 15
    Last Post: 03-09-2012, 03:08 AM
  2. Help with Battleship program! 2D arrays
    By tylerkung in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 01:45 PM
  3. need help creating a battleship like program
    By ss1 in forum New To Java
    Replies: 12
    Last Post: 08-19-2011, 12:00 AM
  4. Battleship 2D Array Program
    By AaronHopkins in forum New To Java
    Replies: 6
    Last Post: 04-05-2011, 04:38 AM
  5. Ladder Program (Need Assistance)
    By Grenade in forum Jobs Discussion
    Replies: 1
    Last Post: 12-14-2010, 10:21 PM

Posting Permissions

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