-
December 10th, 2008, 03:46 PM
#1
Help on a bit of coding!!!
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 boat7,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();
{
}
}
{
}
}
}
}
}
}
-
December 10th, 2008, 03:56 PM
#2
Re: Help on a bit of coding!!!
I have no idea what you are talking about. Also, can you please use code tags?
-
December 10th, 2008, 04:23 PM
#3
Re: Help on a bit of coding!!!
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?
Please rate my post if it was helpful for you. Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
-
December 10th, 2008, 07:32 PM
#4
Re: Help on a bit of coding!!!
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.
Code:
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...
Last edited by rliq; December 10th, 2008 at 07:44 PM.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|