# A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Two-Dimensional Arrays

You have already learnt how to use one dimensional arrays in your computer programs. You should be familiar with code such as:
This is great for storing lists of things, but what about if we want to simulate something more complex such as a game board or a map? Wouldn't it be great if we could use a two-dimensional array?
Most major programming languages allow you to use two-dimensional arrays. They work in much the same way as a one-dimensional array but allow you to specify a column index and a row index.
We can create the two-dimensional array shown above and assign values by doing the following:
Example: Two-Dimensional Arrays
Two-dimensional arrays are very useful and a good place to get started is to create your own version of the game Battleships with a 4 cell by 4 cell grid. See if you can win, or even break it!
We are modelling the following board using the two dimensional board variable:
0123
0xooo
1ooxo
2oooo
3oooo
Dim x, y As Integer
Dim board(3, 3) As Char
board(0, 0) = "x"
board(0, 1) = "o"
board(0, 2) = "o"
board(1, 0) = "o"
board(1, 1) = "o"
board(1, 2) = "x"
board(2, 0) = "o"
board(2, 1) = "o"
board(2, 2) = "o"
board(2, 0) = "o"
board(2, 1) = "o"
board(2, 2) = "o"
For z = 1 To 3
Console.WriteLine("This is guess number " & z)
Console.Write("please insert you x location:")
x = Console.ReadLine
Console.Write("please insert you y location:")
y = Console.ReadLine
If board(x, y) = "x" Then
Console.WriteLine("you win!")
End If
Next :
* Exercise: Two-Dimensional Arrays
* Declare an array to make a small checkers board of type char, 3 squares by 3 squares
* Answer:
* dim checkBoard(3,3) as char 'also checkBoard(2,2)
* create a chequered pattern using b for black and w for white
* Answer:
* checkBoard(1, 1) = "b"
* checkBoard(1, 2) = "w"
* checkBoard(1, 3) = "b"
* checkBoard(2, 1) = "w"
* checkBoard(2, 2) = "b"
* checkBoard(2, 3) = "w"
* checkBoard(3, 1) = "b"
* checkBoard(3, 2) = "w"
* checkBoard(3, 3) = "b"
* A much smarter way is to use a loop, this will allow for you to quickly create an board of any size you wish. There is a question coming up that will want you to build this!
* Write a sub routine to display this board (HINT: you need loops), that takes checkBoard as a parameter
* Answer:
* sub display(checkBoard)
* for x = 1 to 3
* for y = 1 to 3
* console.write(checkBoard(x,y))
* Next
* console.writeline
* Next
* Declare a chessBoard (8*8 squares), programmatically colour it in with b for black and w. You might want to look for a pattern in the colour assignments for the checker board above and make friends with the MOD function
* You might also go a little loopy trying to answer this question
* Answer:
* dim chessBoard(8,8) as char 'also chessBoard(7,7)
* for x = 1 to 8
* for y = 1 to 8
* if (x + y) MOD 2 = 1 then
* chessBoard(x,y) = "w"
* else
* chessBoard(x,y) = "b"
* end if
* next
* next
* display(chessBoard) ' using a slightly updated version of the subroutine display
* If you've done this you might want to get the program to print some massive boards, whatever floats your boat.
* Using the following two-dimensional array, grid(4,4):
* Write code to output the name CRAIG
* Insert MARY on row 2 (the third row)
* Overwite STEVE with SAM
* Answer:
* Console.Writeline(grid(3,0) & grid(3,1) & grid(3,2) & grid(3,3) & grid(3,4))
* grid(2,0) = "M"
* grid(2,1) = "A"
* grid(2,2) = "R"
* grid(2,3) = "Y"
* grid(1,0) = "S"
* grid(1,1) = "A" ' you could skip this
* grid(1,2) = "M"
* grid(1,3) = ""
* grid(1,4) = ""

## Noughts and Crosses

Another game you might play on a 2D grid is the popular game of noughts and crosses:
To do this we need to build a 3 by 3 grid:
Next we need a subroutine to display the grid, passing the board as a parameter
We need the ability to check whether the game has been won and who has won it. We'll do this by building a win check that returns true if the specified player has won, or false otherwise. We need to pass it some values to check, namely the board - b and the player you are checking for - p. Both are passed byVal as we don't need to change them:
It's a bit of a rubbish game so far, so we better let someone move:need the ability for players to play a move:
We need to end the game in a draw if all the spaces have been taken
Finally we'll bring everything together
* Exercise: More Two-Dimensional arrays
* Fix the program above so that an incorrect move allows the user to try again:
* Code Output
* xxo
* o--
* x - make your move in the format: x,y
* 1,2
* x - make your move in the format: x,y
* 1,2
* x - make your move in the format: x,y
* Answer:
* sub move(byRef b As Char, byVal p As Char)
* dim x,y as integer
* dim move as string
* Do
* console.writeline(p + " - make your move in the format: x,y")
* move = console.readline
* x = move.Substring(0, 1)
* y = move.Substring(2, 1)
* While b(x + 1,y + 1) <> "-"
* b(x + 1,y + 1) = p
* display(byVal b)
* end sub
* Fix the program above so that is checks rows, columns and diagonals for a winning position. Try to keep your answer below 19 lines and 550 characters.
* Answer:
* function win(ByVal b(,) As Char, byVal p As Char)
* for x = 0 to 2
* if b(x,0) = p AND b(x,1) = p AND b(x,2) = p then 'check columns
* console.writeline(p + " WINS!")
* return true
* next
* for y = 0 to 2
* if b(0,y) = p AND b(1,y) = p AND b(2,y) = p then 'check rows
* console.writeline(p + " WINS!")
* return true
* next
* if b(0,0) = p AND b(1,1) = p AND b(2,2) = p then 'second row check
* console.writeline(p + " WINS!")
* return true
* end if
* if b(0,2) = p AND b(1,1) = p AND b(2,0) = p then 'second row check
* console.writeline(p + " WINS!")
* return true
* end if
* return false 'if the game hasn't been won
* end function
* Extension: Many more dimensional arrays
* You have met one and two dimensional arrays so far, but this isn't the limit to the number of dimensions that you can use. For example you might want to model a three dimensional world by using a 3D array
* Dim space(2, 2, 2) As integer ' 3 dimensional array