Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Array length problem

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-03-2008, 04:54 AM PM User | #1 calc7 New to the CF scene   Join Date: Oct 2008 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Array length problem At the moment I am primarily concerned with making the lengths on the ships connect to each other. Can anyone give me some sort of guidance on how to place ships lengths of 2 3 3 4 5 on the board, and without bumping with each other. this is what I have so far Code: ```import java.util.Scanner; import java.util.Random; public class Battleship { int grid[][]; int row; int col; public Battleship() {//random locations for ships grid = new int[10][10]; Random rnd = new Random(); //patrol int[] shipsLength = {2, 3, 3, 4, 5}; int[] ship = {1, 2, 3, 4, 5}; //test for 2x1 for(int x = 0; x < 5; x++) { row = rnd.nextInt(10); col = rnd.nextInt(10); grid[row][col] = ship[x]; } } public void display() { //horizontal lines System.out.println("----------------------------------------"); for (int line[] : grid) { for (int num : line) { // determine what to print for this position if (num == -1) System.out.printf("| %2s", "m " ); else if (num == 0 || num == 10) System.out.printf("| "); // don't print 0 or 10 else System.out.printf("| %2d", num); // this displays the 10 } // print the final bar of this row System.out.println("|"); // print horizontal line System.out.println("----------------------------------------"); } } public int menu() { Scanner sc = new Scanner(System.in); // display menu System.out.println("1. Enter a guess (row column): "); System.out.println("2. Display grid"); System.out.println("3. End game"); // get user's choice System.out.print("Enter choice: "); int choice = sc.nextInt(); return choice; } /////public boolean processGuess(int r, int c) GUESS PROCESSOR IS HERE { //CHECK POSITIONS } public int getRow() { return row; } public int getCol() { return col; } public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); Battleship gs = new Battleship(); int counter = 0; int choice; do { choice = gs.menu(); if (choice == 1) //guess { System.out.println("Enter your guess (row and column i.e. 1 9): "); int row = keyboard.nextInt(); int col = keyboard.nextInt(); } else if (choice == 2) // display { gs.display(); } } while(choice == 3); System.out.println("End of Battleship game"); } }``` Last edited by oracleguy; 10-04-2008 at 06:19 PM.. Reason: Please use code tags
 10-06-2008, 07:01 PM PM User | #2 shyam Senior Coder     Join Date: Jul 2005 Posts: 1,563 Thanks: 2 Thanked 163 Times in 160 Posts are all the ships aligned vertically, horizontally or both? __________________ You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:13 PM.