Tic Tac Toe and Magic Squares |
We first looked at a tic-tac-toe program to see how to access the elements of a two dimensional array. Examining this program showed how to map from the location of a mouse click to a row and column in a two-dimensional array and how to traverse a row, column, or diagonal of a two-dimensional array.
We then looked at a program that generates "magic squares" and stores them in two-dimensional arrays. A square array of numbers is said to be a "magic square" if all the rows, columns, and diagonals add up to the same number. For example, here is a 3 ×3 magic square:
4 9 2 3 5 7 8 1 6
Each row, column, and diagonal of this square add up to 15.
Tic Tac Toe and Magic Squares |