18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Arrays 113<br />

8.4.5 Putt<strong>in</strong>g it all together<br />

When compiled and run, a possible <strong>in</strong>teraction between two players could be as follows:<br />

X’s first move O’s first move X’s second move O’s second move<br />

X | |<br />

---------<br />

| |<br />

---------<br />

| |<br />

X | |<br />

---------<br />

| |<br />

---------<br />

| O |<br />

X | | X<br />

---------<br />

| |<br />

---------<br />

| O |<br />

X | O | X<br />

---------<br />

| |<br />

---------<br />

| O |<br />

X’s third move O’s third move X’s fourth move<br />

X | O | X<br />

---------<br />

| X |<br />

---------<br />

| O |<br />

X | O | X<br />

---------<br />

| X |<br />

---------<br />

O | O |<br />

X | O | X<br />

---------<br />

| X |<br />

---------<br />

O | O | X<br />

As can be seen<br />

to go first is a<br />

clear advantage.<br />

8.5 Multidimensional arrays<br />

Arrays can have any number of dimensions. For example, the noughts and crosses board could have been<br />

represented by a two-dimensional array as follows:<br />

Size_TTT : constant := 3;<br />

subtype Board_Index is Integer range 1 .. Size_TTT;<br />

type Board_Array is<br />

array( Board_Index, Board_Index ) of Character;<br />

type Board is record<br />

Sqrs : Board_Array := ( others => (others => ' ') );<br />

end record;<br />

The: Board;<br />

Note:<br />

The two-dimensional <strong>in</strong>itialization of the board to spaces is as follows:<br />

sqrs : Board_grid:=( others => (others => ' ') );<br />

This type of <strong>in</strong>itialization is expla<strong>in</strong>ed <strong>in</strong> Section 8.6 Initializ<strong>in</strong>g an array.<br />

With this representation of the board <strong>in</strong>dividual elements are accessed as follows:<br />

The.Sqrs(1,2) := 'X';<br />

The.Sqrs(2,3) := 'X';<br />

The.Sqrs(3,2) := 'X';<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!