An 2 D(imensional) array has two directions :
Direction X for horizontal
Direction Y for vertical.
An array is always the same type, string, integer, double but never can be mixed types. Also note that array indexes start from “0”. Individual positions are called “elements”.
So if you have an array lets say “intBoard(15,15)” there are 16 elements horizontally (X) and 16 elements vertically (Y).
As an excercise, please workout the following code without running it (cheating). Make sure you predict those values correctly by double checking your results. Here it is :
// Dimensioning (creating) an 2D array
// containing 16 elements horizontally (X) and
// 16 elements vertically (Y)
Dim intBoard(15,15) As Integer
Dim intLoop_X_Direction As Integer
Dim intLoop_Y_Direction As Integer
Dim intValue As Integer
intValue = 0
For intLoop_Y_Direction = 0 To 15
// Outer For loop moving vertically
For intLoop_X_Direction = 0 To 15
// Inner For loop moving horizontally
intBoard(intLoop_X_Direction, intLoop_Y_Direction) = intValue
intValue = intValue + 1
Next intLoop_X_Direction
Next intLoop_Y_Direction
// Without running the code and see the results,
// take a piece of paper and write down what the
// value of the following positions are :
// intBoard(15,15) :
// intBoard(15,14) :
// intBoard(14,15) :
// Hint :
// When an array or any datastructure is not clear,
// draw it out in a drawing program or on paper.
// You will see it is easier to understand.
// Now considering the next code :
intValue = 0
For intLoop_Y_Direction = 0 To 15
// Outer For loop moving vertically
intValue = intValue + 1
For intLoop_X_Direction = 0 To 15
// Inner For loop moving horizontally
intBoard(intLoop_X_Direction, intLoop_Y_Direction) = intValue
Next intLoop_X_Direction
Next intLoop_Y_Direction
// Now workout what happens with the positions :
// intBoard(15,15) :
// intBoard(15,14) :
// intBoard(14,15) :
// Don't test it but work it out without running the above code.
// When you can predict what happens correctly, you do
// understand 2 dimensional arrays.
However, for your board I would use a “Class array”. This has the advantage that you can store more data for a particular field in one element of the class array. For example the horizontal, vertical board position, also what type of “ship” is occupying that element, amount of hits and so on.
For applying Artificial Intelligence (AI), Scirra has some very good tutorials on this subject. As an excercise, look at those tutorials and apply that information with Xojo.
This all can look very difficult and complex and sometimes it is. However when developing any kind of computer program, break things in smaller parts. As an example :
- Create the board
- Fill the board with default values
- Create the type of ships
- Put those ships on the board
- …
Now you have a general structure or idea what in the example above the beginning of the game has to do.
Now you workout those individual points. This method can make a problem which you fail to overview, nicely visible. Don’t just start coding as you go. Plan, structure and then code.
You see, when you organize and structure your idea and creations, you will finish your battleship project nicely.
Hope this helps.
Chris