Click to See Complete Forum and Search --> : Help on a bit of coding!!!


ekulfest
December 10th, 2008, 02:46 PM
how can i make an input by the user which places ships on the grid

namespace battleships20
{
enum seastate
{
EmptySea,
Attacked,
Battleship,
Cruiser,
Submarine,
Destroyer
};

class BattleShipsPlayer
{
const int SEA_WIDTH = 10;
const int SEA_HEIGHT = 10;
static int[,] sea = new int[SEA_WIDTH, SEA_HEIGHT];

static void clearSea()
{
// Clears the sea to empty
}
static void placeShips()
{

// Places ships as follows:
// Battleship: (0,2) and (1,6)
// Cruiser: (0,4), (1,0) and (9,9)
// Submarine: (5,5)
// Rowing boat:(7,8)
}
static void displaySea()
{


}
// Displays the sea on the users screen

static void Main(string[] args)
{

{
string userName;

Console.Write("Please Enter your Name: ");

userName = Console.ReadLine();



Console.WriteLine("");

//On the next line, display the input.

//{0} is where the variable value will go. It

//is followed by a comma and variable name userName

Console.WriteLine("Hello {0}!", userName);

Console.WriteLine("Enter the coordinates of your ship");

//The Console.Readline will pause the program until you
{

for (int x = 0; x < SEA_WIDTH; x++)
{

for (int y = 0; y < SEA_HEIGHT; y++)
{

Console.Write(sea[x, y]);

Console.Write("\t");

}

Console.WriteLine();

{

}
}

{

}
}
}
}
}
}

BigEd781
December 10th, 2008, 02:56 PM
I have no idea what you are talking about. Also, can you please use code tags?

toraj58
December 10th, 2008, 03:23 PM
what kond of Grid are you talikng about. if you mean GridView! but it is Windows control but your application is console application!!!

do you know what do you want to do?

rliq
December 10th, 2008, 06:32 PM
You will need to look up System.Random, to choose left/top (of the ship) and H/V orientation.
When placing ships, a separate Boolean[,] array could be used to indicate if the sea-square is empty or occupied.


foreach (Ship ship in ships)
{
while (true)
{
// generate random x,y and orientation for the ship

// determine from Boolean matrix if ship will fit at this random location

if (/* ship fits at that position */)
{
// update boolean matrix (set to one's where the ship is)
// update your real matrix (set squares to ship.Name etc)
break;
}
}
}
Note: If you place too many Titanics in too small of a lake, then the internal loop may never end...