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.

Polymorphism 247<br />

The driver program for the game of noughts and crosses follows the same style as seen earlier <strong>in</strong> Section 8.4.5.<br />

with Class_Board, Class_Display_Board,<br />

<strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Class_Observe_Observer;<br />

use Class_Board, Class_Display_Board,<br />

<strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Class_Observe_Observer;<br />

procedure Ma<strong>in</strong> is<br />

Player : Character; --Either 'X' or 'O'<br />

Game : Board; --An <strong>in</strong>stance of Class Board<br />

Move : Integer; --Move from user<br />

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

Player := 'X';<br />

--Set player<br />

An <strong>in</strong>stance of an observer of the board is passed to the method Add_Observer so that it can be displayed.<br />

Add_Observer( Game, new Display_Board );<br />

Note:<br />

As a po<strong>in</strong>ter to the object is required, a dynamic <strong>in</strong>stance of the object is created for simplicity of code.<br />

Naturally, this could have been done by tak<strong>in</strong>g the access value of a non-dynamic <strong>in</strong>stance.<br />

The code for the logic of the game asks each player <strong>in</strong> turn for a move, when a player has entered a valid<br />

move, the method Notify_Observers is used to request a display of the new state of the board.<br />

while State( Game ) = Playable loop --While playable<br />

Put( Player & " enter move (1-9) : "); -- move<br />

Get( Move ); Skip_L<strong>in</strong>e; -- Get move<br />

if Valid( Game, Move ) then --Valid<br />

Add( Game, Move, Player ); -- Add to board<br />

Notify_Observers( Game );<br />

case State( Game ) is --Game is<br />

when W<strong>in</strong> =><br />

Put( Player & " w<strong>in</strong>s");<br />

when Playable =><br />

case Player is --Next player<br />

when 'X' => Player := 'O'; -- 'X' => 'O'<br />

when 'O' => Player := 'X'; -- 'O' => 'X'<br />

when others => null; --<br />

end case;<br />

when Draw =><br />

Put( "It's a draw ");<br />

end case;<br />

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

else<br />

Put("Move <strong>in</strong>valid"); New_L<strong>in</strong>e; --for board<br />

end if;<br />

end loop;<br />

New_L<strong>in</strong>e(2);<br />

end Ma<strong>in</strong>;<br />

16.11 Self-assessment<br />

• What is the difference between static and dynamic b<strong>in</strong>d<strong>in</strong>g?<br />

• What is an object’s tag?<br />

• What is a heterogeneous collection of objects? How are heterogeneous collections of objects created<br />

and used <strong>in</strong> <strong>Ada</strong>?<br />

• What is a view conversion? Why are view conversions required?<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!