Results 1 to 3 of 3
  1. #1
    afarag is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Angry Battleship again...

    Sorry but I'm having a lot of trouble with this game due to the teachers rules. I am trying to print the board but all I am getting is the border numbers the Interface methods I use are this :
    Java Code:
    public void printBoard() {
    		// TODO Auto-generated method stub
    		System.out.println("  1  2  3  4  5  6  7  8  9  10");
    		for (int y=0; y< board.length; y++){
    			System.out.print(y+1+ "  ");
    			for (int x=0; x< board.length; x++){
    				if (board[y][x] == '~'){
    					
    					System.out.print("~  ");
    				} else {
    					System.out.print(board[y][x] + "  ");
    				}
    			}
    			System.out.println();
    			System.out.println();
    		}
    		
    	}
    	
    	
    
    	
    	public void initBoard(char[][] board) {
    		
    		for (int y=0; y< board.length; y++){
    			for (int x=0; x< board.length; x++){
    				board[y][x]= '~';
    			}
    		}
    	}
    and my main method looks like this so far:
    Java Code:
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		int count1 = 0;
    		
    		char [][] board = new char[10][10];
    		
    		Player player1 = new Player_af483();
    		Player player2 = new Player_af483();
    		player1.initBoard(board);
    		System.out.println("Player 1");
    		player1.printBoard();
    		player1.placeShips();
    It doesn't print out the '~' character but only the numbers so it's a blank board. The program doesn't initiate the board to the whole array having '~'.

    This is the output :
    Player 1
    1 2 3 4 5 6 7 8 9 10
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    this is what I want:
    Welcome to Battleship!
    Player 1
    1 2 3 4 5 6 7 8 9 10
    1 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    2 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    3 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    4 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    5 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    6 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    7 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    8 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    9 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    10 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    Last edited by afarag; 04-19-2014 at 01:53 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: Battleship again...

    all I am getting is the border numbers
    Please post the program's output
    and an example of what you want the output to be.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Battleship again...

    It seems that you have multiple boards.
    Your player class seems to have a board. That is printed.
    The initBoard method gets some other board as parameter and then this board variable is hiding the board of the class.

    So you initialise the board from your main method and print the board of the player.

    Konrad

Similar Threads

  1. battleship
    By redblack in forum New To Java
    Replies: 1
    Last Post: 07-18-2013, 04:31 AM
  2. Battleship
    By Matthieu105 in forum New To Java
    Replies: 6
    Last Post: 11-28-2012, 03:24 PM
  3. Battleship program
    By buckbuck2 in forum New To Java
    Replies: 2
    Last Post: 11-28-2012, 02:57 AM
  4. Battleship UML help
    By Lucid15 in forum New To Java
    Replies: 0
    Last Post: 03-12-2012, 04:15 AM
  5. battleship in java
    By matthewandalex5 in forum New To Java
    Replies: 2
    Last Post: 03-01-2012, 06:09 AM

Tags for this Thread

Posting Permissions

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