Thread: Java Prog help with battleship game

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Java Prog help with battleship game

    i need help on how to make my program read the col and how to place ships ..

    can anyone help here is what i have to work with.

    Code:
    public class Board {
    
    	// 0 = blank, 1 = "X", -1 = "O"
    	int grid[][];
    	String q = "  | A | B | C | D | E | F | G | H | I | J |";
    	String c = "  |___|___|___|___|___|___|___|___|___|___|";
    
    	Board() {
    
    		grid = new int[10][10];
    		for (int r = 0; r < grid.length; r++)
    			for (int c = 0; c < grid[r].length; c++)
    				grid[r][c] = 0;
    
    	}
    	
    	int getGrid(int c, int r) {
    		return grid[c][r];
    	}
    
    }
    
    
    
    	draw(human, comp);
    		input=IN.readLine();
    		int Variable = Integer.parseInt(IN.readLine());
    	}
    	
    	static void draw(Player P, Computer C) {
    		String eq = "  ========================================= =========================================\n";
    		String bar = "  _________________________________________ _________________________________________\n";
    
    		System.out.print("\t                       |  +_-_BATTLESHIP_-_+  |\n" + eq);
    		System.out.print("                   PLAYER                                   COMPUTER\n" + bar);
    	    String q ="  | A | B | C | D | E | F | G | H | I | J | | A | B | C | D | E | F | G | H | I | J |\n"+bar;
    		System.out.print(q);
    		String row = "  |___|___|___|___|___|___|___|___|___|___| |___|___|___|___|___|___|___|___|___|___|";
    
    		for (int r = 0; r < 10; r++) {
    			System.out.print((r + 1));
    			if (r < 9) System.out.print(" ");
    			for (int c = 0; c < 10; c++) {
    				System.out.print("| " + P.getMark(c, r) + " ");
    			}
    			System.out.print("| ");
    			for (int c = 0; c < 10; c++) {
    				System.out.print("| " + C.getMark(c, r) + " ");
    			}
    			System.out.print("|\n" +row+"\n");
    		}
    		
    	}
    	
    	
    public class Player {
    	Board fred = new Board();
    	Ship carrier = new Ship(5, "Aircraft Carrier");
    	Ship patrol =new Ship(2, "Patrol Boat");
    	Ship Cruiser= new Ship( 3, "Cruiser");
    	Ship Destroyer= new Ship(4, "Destroyer");
    	Ship Submarine = new Ship( 3, "Submarine");
    	// ships
    	
    	
    	char getMark(int c, int r) {
    		switch (fred.getGrid(c, r)) {
    		case 1:		return '*';	//hit
    		case -1:	return 'O';	//miss
    		case 50:		return 'A';//Aircraft Carrier 5
    		case 40:		return 'C';//Cruiser 3
    		case 30:		return 'D';//Destroyer 4
    		case 20:		return 'S';//Submarine 3 hits
    		case 10:		return 'P';//Patrol Boat 2 hits
    		default:	return ' ';
    		}
    	}
    }
    
    
    public class Ship{
    	String name;
    	int length;
    
    	 Ship(int L, String N){
    		length = L;
    		name = N;
    	 }
    	 
    	 int getLength() {
    		 
    		 return length;
    	 }
    	 
    	 String getName() {
    		 return name;
    	 }
    }

    Please help thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't program in C++, only C, but here's the way to display your ships.

    Print up the board, separately. Draw all your lines, including any ships that are present in the original set up of the board.

    To show the ships, use a formula like this:

    X position = left margin (# of spaces), + (col number * (col width - 1) + col width in spaces divided by 2). If your cells or blocks are an odd number, the mark denoting a ship should now be centered in the X (horizontal), part of the right cell or block.

    Y position = top margin + (row number * (height of cells - 1) + (height of a cell / 2)

    Again, if your cell is an odd number of rows in height, your mark denoting a ship should now be centered in the Y (vertical), portion of the cell.

    I'd suggest using an array of struct's to easily track what's happening with the ship, and what cells it's resting upon, etc.

    The above forumula may change, depending on whether you use an array's zero'th row and column, or you skip it, and start with row 1 and col 1 as your first row and column. Obviously, these are made for the latter. Adjust as needed for the former.

    When you're showing updates, don't re-draw the whole board, or all the ships. Just adjust the ship that has moved or been damaged, etc., on the most current move. That stops the flicker stuff.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It would be better to ask this on a Java board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. Replies: 26
    Last Post: 03-20-2004, 02:59 PM
  5. Replies: 6
    Last Post: 08-07-2003, 02:05 PM