|Why are you using a DataTable? It's a bit of a sledgehammer to crack a nut ...
You need a 10 x 10 area of "squares" which can contain four values: Empty, Ship, Hit, and Miss. So the simplest way to do that is you create an
enum that has all of those values:
public enum Square
You can then create an array of that:
Square[,] MyBoard = new Square[10,10];Or two:
Square[,] HisBoard = new Square[10,10];You can now reference any particular Square via indexes:
if (MyBoard[x, y] == Square.Ship)
MyBoard[x, y] = Square.Hit;
}Create a method to clear a board:
public static Square[,] Clear(Square[,] board)
for (int x = 0; x < board.GetLength(0); x++)
for (int y = 0; y < board.GetLength(1); y++)
board[x, y] = Square.Empty;
}And you can start getting ready for a new game:
Square[,] MyBoard = new Square[10,10];
Square[,] HisBoard = new Square[10,10];
Clear(HisBoard);Then just write another method to load your ships in: it accepts a board as a parameter, calls Clear. and then decides where to put the ships. (Initially, I'd try to write it using "single space" ships to make the code easier, then start working on the more complex shapes you really need).
In fact what I'd do is more complex than that: I'd encapsulate the Square array in a class called Board, several ship classes (Carrier, BattleShip, Cruiser, Submarine, Destroyer) derived from a base Ship class) and let it handle the mundane stuff so that most of the time you are manipulating Board objects:
MyBoard.place(myDestroyer, x, y, Orientation.Horizontal);
MyBoard.Bomb(6, 7);But if you haven't reached classes and instances yet then a basic array will be fine.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!