A-level Computing/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:

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:

Declare an array to make a small checkers board of type char, 3 squares by 3 squares

create a chequered pattern using  for black and   for white

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  this board (HINT: you need loops), that takes checkBoard as a parameter

Declare a chessBoard (8*8 squares), programmatically colour it in with  for black and. 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

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
 * Insert  on row 2 (the third row)
 * Overwite  with

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  if the specified player has won, or   otherwise. We need to pass it some values to check, namely the board -  and the player you are checking for -. Both are passed  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

Fix the program above so that an incorrect move allows the user to try again:

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

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.

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



