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.

The specification for the class Cell which holds a counter is as follows:<br />

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

with Class_Counter;<br />

use Class_Counter;<br />

package Class_Cell is<br />

type Cell is private;<br />

type Cell_Holds is ( C_White, C_Black, Empty );<br />

procedure Initialize( The:<strong>in</strong> out Cell );<br />

function Holds( The:<strong>in</strong> Cell ) return Cell_Holds;<br />

procedure Add( The:<strong>in</strong> out Cell; Players_Counter:<strong>in</strong> Counter );<br />

procedure Display( The:<strong>in</strong> Cell );<br />

procedure Flip( The:<strong>in</strong> out Cell );<br />

function To_Colour( C:<strong>in</strong> Cell_Holds ) return Counter_Colour;<br />

private<br />

type Cell_Is is ( Empty_Cell, Not_Empty_Cell );<br />

type Cell is record<br />

Contents: Cell_Is := Empty_Cell;<br />

Item : Counter; --The counter<br />

end record;<br />

end Class_Cell;<br />

In the implementation of the package the procedure Initialize sets the contents of the cell to empty.<br />

package body Class_Cell is<br />

procedure Initialize( The:<strong>in</strong> out Cell ) is<br />

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

The.Contents := Empty_Cell; --Initialize cell to empty<br />

end Initialize;<br />

The procedure Holds returns the contents of the cell which is def<strong>in</strong>ed by the enumeration type<br />

Cell_Holds.<br />

function Holds( The:<strong>in</strong> Cell ) return Cell_Holds is<br />

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

case The.Contents is<br />

when Empty_Cell => --Empty<br />

return Empty;<br />

-- No counter<br />

when Not_Empty_Cell => --Counter<br />

case Rep( The.Item ) is<br />

when White => return C_White; -- white counter<br />

when Black => return C_Black; -- black counter<br />

end case;<br />

end case;<br />

end Holds;<br />

The next three procedures implement:<br />

• Add<strong>in</strong>g of a new counter <strong>in</strong>to a cell.<br />

• Display<strong>in</strong>g the contents of a cell.<br />

• Flipp<strong>in</strong>g the counter <strong>in</strong> the cell to the other colour.<br />

procedure Add(The:<strong>in</strong> out Cell; Players_Counter:<strong>in</strong> Counter) is<br />

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

The := (Not_Empty_Cell,Players_Counter);<br />

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