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.

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

The procedure Announce communicates with the human player the result of the game.<br />

procedure Announce(The:<strong>in</strong> Player; What:<strong>in</strong> State_Of_Game) is<br />

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

case What is<br />

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

Put("Player "); Display( The.Plays_With );<br />

Put(" has won");<br />

when Lose =><br />

Put("Player "); Display( The.Plays_With );<br />

Put(" has lost");<br />

when Draw =><br />

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

when others =><br />

raise Constra<strong>in</strong>t_Error;<br />

end case;<br />

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

end Announce;<br />

end Class_Player;<br />

9.7 Self-assessment<br />

• What is the function of the class Player?<br />

• What is the function of the class Cell?<br />

• Could the recursive function no_turned <strong>in</strong> the class Board be written non-recursively?<br />

• The procedure announce <strong>in</strong> the class Board has a case statement with a when others clause<br />

that can never occur. Why is this clause necessary?<br />

9.8 Exercises<br />

• Better ‘reversi’<br />

Modify the program to have a separate class for all <strong>in</strong>put and output.<br />

• Graphic ‘reversi’<br />

The program could be modified by provid<strong>in</strong>g additional classes to present a graphical display of the<br />

board. The display could enable the user to drop a counter <strong>in</strong>to a cell selected by a us<strong>in</strong>g a mouse.<br />

Describe the modifications required to implement this new version.<br />

• Implementation of a graphic ‘Reversi’<br />

Implement this new graphical version.<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!