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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

110 Arrays<br />

Note:<br />

That the case statement which effects the change between the player’s mark has a when others<br />

clause. This is required as <strong>in</strong> theory a player can take any character value. The code for this<br />

‘impossible’ eventuality is the null statement.<br />

The character object player holds a representation of the current player’s mark, <strong>in</strong> this case either the<br />

character 'X' or 'O'. The object player is <strong>in</strong>itially set to 'X', and after each player's move is changed to the<br />

other player’s mark.<br />

8.4.3 Display<strong>in</strong>g the Board<br />

The procedure Display uses the method Cell <strong>in</strong> class Board to help display a textual representation of the<br />

board on the user’s term<strong>in</strong>al. The procedure Display is implemented as follows:<br />

with Class_Board, <strong>Ada</strong>.Text_Io;<br />

use Class_Board, <strong>Ada</strong>.Text_Io;<br />

procedure Display( The:<strong>in</strong> Board ) is<br />

beg<strong>in</strong><br />

for I <strong>in</strong> 1 .. 9 loop<br />

Put( Cell( The, I ) );<br />

case I is<br />

--after pr<strong>in</strong>t<strong>in</strong>g counter<br />

when 3 | 6 => -- pr<strong>in</strong>t Row Separator<br />

New_L<strong>in</strong>e; Put("---------"); --<br />

New_L<strong>in</strong>e;<br />

when 9 => -- pr<strong>in</strong>t new l<strong>in</strong>e<br />

New_L<strong>in</strong>e;<br />

when 1 | 2 | 4 | 5 | 7 | 8 => -- pr<strong>in</strong>t Col separator<br />

Put(" | ");<br />

end case;<br />

end loop;<br />

end Display;<br />

The procedure Display pr<strong>in</strong>ts the board on to the player’s term<strong>in</strong>al. The strategy for pr<strong>in</strong>t<strong>in</strong>g the board is to<br />

pr<strong>in</strong>t each cell followed by a character sequence appropriate for its position on the board. The text to be pr<strong>in</strong>ted<br />

after each square of the array sqrs has been pr<strong>in</strong>ted is as follows:<br />

Pr<strong>in</strong>ted board show<strong>in</strong>g<br />

array <strong>in</strong>dex of cell <strong>in</strong> array<br />

Sqrs<br />

1 | 2 | 3<br />

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

4 | 5 | 6<br />

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

7 | 8 | 9<br />

After pr<strong>in</strong>t<strong>in</strong>g cell:<br />

Text to be pr<strong>in</strong>ted<br />

(Us<strong>in</strong>g <strong>Ada</strong>.Text_Io)<br />

1,2,4,5,7 and 8 Put(" | ");<br />

3 and 6 New_L<strong>in</strong>e;<br />

Put("---------");<br />

New_L<strong>in</strong>e;<br />

9 New_L<strong>in</strong>e;<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!