Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2008, 03: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
calc7 is an unknown quantity at this point
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 05:19 PM.. Reason: Please use code tags
calc7 is offline   Reply With Quote
Old 10-06-2008, 06:01 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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
shyam is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Jump


All times are GMT +1. The time now is 03:50 AM.


Advertisement
Log in to turn off these ads.