Results 1 to 2 of 2
  1. #1
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default need urgent help...

    Hello... I'm not entirley new to Java but I have some problems with an assignment that I'm doing. I hope that you guys can help me with a little bit.
    We are asked to write a battleship game. We are asked to enter the location of four ships: a BattleShip, Destroyer, Cruiser & Tug. Theres 2 boards for the game.. A challenger board and a program board.
    Once the ships have been placed the game boards should be shown as follows. Notice the challenger's ship locations are shown, but the locations of the program's ships are hidden and it is the user's task to try and sink them. (While the program is shooting back.). The user should be prompted for the co-ordinate of the bow (top leftmost square) of the ship and the direction which can be vertical "V" or horizontal "H". For example:

    The game should be something like this:

    Challenger Program
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
    0~ ~ ~ D D D D ~ ~ ~ 0~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    1~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 1~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    2~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 2~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    3~ ~ ~ ~ B ~ ~ C C C 3~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    4~ ~ ~ ~ B ~ ~ ~ ~ ~ 4~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    5~ ~ ~ ~ B ~ ~ ~ ~ ~ 5~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    6~ ~ ~ ~ B ~ ~ ~ ~ ~ 6~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    7~ ~ ~ ~ B ~ ~ ~ ~ ~ 7~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    8~ T T ~ ~ ~ ~ ~ ~ ~ 8~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    9~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 9~ ~ ~ ~ ~ ~ ~ ~ ~ ~

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

    Anyway...
    I've done this so far... but the code below shows only one board. I need to
    show to boards.. one for the program and one for the challenger.
    And I need to know how to enter the vertical/horizontal co-ordinates.. what code shoud I add.

    The code is aliitle bit messy.. but any help would be really appreciated.
    Thanks alot in advance.


    -------------------------------------------------------------
    Java Code:
    public class BattleshipGame {
    	
    	//This class represents the game board and the rules of the game
    	
    	private char[][] board;
    	private int numberOfMoves; //keeps track of the number of moves
    	
    	public final static int MOVE_MISS    = 0;
    	public final static int MOVE_HIT     = 1;
    	public final static int MOVE_INVALID = 2;
    	
    	public final static String[] ResultMessages = {"Miss", "Hit!", "Invalid Move"};	
    
    	public BattleshipGame() {
    		board = new char[10][10];
    		initializeBoard();
    		placeShip(4, true, 'B');  // battleship
    		placeShip(3, false, 'D'); // destroyer
    		placeShip(2, true, 'C');  // cruiser
    		placeShip(1, false, 'T'); // tug
    
    		numberOfMoves = 0;
    	}
    
    	public void initializeBoard() {
    		// This sets all the squares to be waves
    		for (int i = 0; i < board.length; ++i) {
    			for (int j = 0; j < board[i].length; ++j) {
    				board[i][j] = '~';
    			}
    		}
    	}
    
    	public void placeShip(int shipSize, boolean xOriented, char symbol) {
    		// Places a ship of size shipSize, 
    		// either along the x or y axis (no diagonal ships)
    		int startx = 0, starty = 0;
    		int x, y;
    
    		if (xOriented) {
    			x = 1;
    			y = 0;
    		} else {
    			x = 0;
    			y = 1;
    		}
    
    		boolean goodSpot = false;
    		while (!goodSpot) {
    			// Choose a random spot to put the end of the ship
    			startx = (int) (Math.random() * (board.length - shipSize*x));	
    			starty = (int) (Math.random() * (board[0].length - shipSize*y));
    			goodSpot = true;
    			
    			// Verify that nothing else is already placed in any of the squares you need
    			for (int i=0; i < shipSize; ++i) {
    				if (board[startx+(i*x)][starty+(i*y)] != '~') {
    					goodSpot = false;
    				}	
    			}
    		}	
    		for (int i=0; i < shipSize; ++i) {
    			board[startx+(i*x)][starty+(i*y)] = symbol;	
    		}
    	}
    
    	public void printGameBoard(boolean showShipPositions) {
    		
    		//This method displays the game board
    		//if showShipPositions is true the positions of
    		//the ships are also shown
    		
    		String output = "Moves: " + numberOfMoves + "\n\n";
    		System.out.println(output);
    		
    		output = "  ";
    		for (int i = 0; i < board.length; ++i) {
    			output = output +  i + " ";
    		}	
    		System.out.println(output);
    
    		for (int i = 0; i < board.length; ++i) {
    			output = "" + i + " ";		
    
    			for (int j = 0; j < board[i].length; ++j) {
    		
    				if (board[i][j] == '.' || board[i][j] == '*') {
    					output += board[i][j] + " ";
    				} 
    				else if(showShipPositions){
    					//show the positions of the ships
    				    output += board[i][j] + " ";	
    				}
    				else{
    					output += '~' + " ";
    				}
    			}
    			output += "\n";
    			System.out.println(output);
    		}
    	}
    
    
    	public int move(int x, int y) {
    
    		numberOfMoves++;
    
    		// check to see if the coordinates are out of bounds
    		if (x >= board.length || x < 0 || 
    			y >= board[0].length || y < 0) {
    			return MOVE_INVALID;
    		}
    
    		// see if it's a miss
    		if (board[x][y] == '~' || board[x][y] == '.') {
    			board[x][y] = '.';
    			return MOVE_MISS;
    		}
    		else {
    			//it must be a hit
    			board[x][y] = '*';
    			return MOVE_HIT;
    		}
    	}
    
    	public boolean isWon() {
    		//determine if the game is won yet
    		boolean won = true;
    
    		for (int i = 0; i < board.length; ++i) {
    			for (int j = 0; j < board[i].length; ++j) {
    				if (board[i][j] != '~' && board[i][j] != '*' && board[i][j] != '.') {
    					won = false;
    				}
    			}
    		}
    
    		return won;
    	}
    }
    Last edited by JavaBean; 11-12-2007 at 11:33 AM. Reason: Code placed inside [code] tag.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
        public void printGameBoard(boolean showShipPositions) {
            //This method displays the game board
            //if showShipPositions is true the positions of
            //the ships are also shown
    
            String output = "Moves: " + numberOfMoves + "\n\n";
            System.out.println(output);
    
            output = "   ";
            for (int i = 0; i < 2*board.length; ++i) {
                output = output +  (i%board.length) + " ";
            }
            output += "\n   -";
            for (int i = 0; i < 2*board.length-1; ++i) {
                output = output +  "--";
            }
            System.out.println(output);
    
            for (int i = 0; i < board.length; ++i) {
                output = "" + i + " |";		
                for (int j = 0; j < 2*board[i].length; ++j) {
                    if(j > board[i].length-1) {
                        output += '~' + " ";
                    } else {
                        if (board[i][j] == '.' || board[i][j] == '*') {
                            output += board[i][j] + " ";
                        } else if(showShipPositions) {
                            //show the positions of the ships
                            output += board[i][j] + " ";	
                        } else {
                            output += '~' + " ";
                        }
                    }
                }
    //            output += "\n";
                System.out.println(output);
            }
        }

Similar Threads

  1. Hi, need some urgent help!
    By jdark in forum New To Java
    Replies: 2
    Last Post: 04-18-2008, 06:50 AM
  2. Help me ...urgent!
    By googgoo in forum New To Java
    Replies: 7
    Last Post: 04-05-2008, 08:46 AM
  3. please help urgent
    By ananas7777 in forum AWT / Swing
    Replies: 2
    Last Post: 12-25-2007, 08:30 AM
  4. NEED urgent help...
    By bezdapez in forum New To Java
    Replies: 7
    Last Post: 11-27-2007, 04:40 PM
  5. Very Urgent
    By Swamipsn in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 01:56 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
  •