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.

Case study: Design of a game 145<br />

In the implementation of the class Class_Player the procedure Set sets the colour for the player’s<br />

counter.<br />

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

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

package body Class_Player is<br />

procedure Set(The:<strong>in</strong> out Player; C:<strong>in</strong> Counter_Colour ) is<br />

A_Counter : Counter;<br />

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

Set( A_Counter, C ); --Set colour<br />

The.Plays_With := A_Counter; --Player is play<strong>in</strong>g with<br />

end Set;<br />

The procedure Get_Move communicates with the human player us<strong>in</strong>g a simple text based <strong>in</strong>teraction.<br />

procedure Get_Move(The:<strong>in</strong> Player; Row,Column:out Integer) is<br />

Valid_Move : Boolean := False;<br />

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

while not Valid_Move loop<br />

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

Put("Please enter move "); Display( The.Plays_With );<br />

Put(" row column : "); Get( Row ); Get( Column );<br />

Valid_Move := True;<br />

exception<br />

when Data_Error =><br />

Row := -1; Column := -1; Skip_L<strong>in</strong>e;<br />

when End_Error =><br />

Row := 0; Column := 0;<br />

return;<br />

end;<br />

end loop;<br />

end Get_Move;<br />

Note: A player can pass a turn by enter<strong>in</strong>g a coord<strong>in</strong>ate of 0, 0.<br />

The counter that the player plays with is returned by the function My_Counter.<br />

function My_Counter( The:<strong>in</strong> Player ) return Counter is<br />

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

return The.Plays_With;<br />

end My_Counter;<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!