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.

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

private<br />

Size: constant := 8;<br />

--8 * 8 Board<br />

subtype Board_Index is Integer range 1 .. Size; --<br />

type Board_Array is array (Board_Index, Board_Index) of Cell;<br />

type Score_Array is array (Counter_Colour) of Natural;<br />

type Move_Array is array (Counter_Colour) of Move_Status;<br />

type Board is record<br />

Sqrs : Board_Array; --Game board<br />

Player : Counter_Colour; --Current Player<br />

Opponent : Counter_Colour; --Opponent<br />

Score : Score_Array; --Runn<strong>in</strong>g score<br />

Last_Move: Move_Array; --Last move is<br />

end record;<br />

end Class_Board;<br />

The body of Class_Board conta<strong>in</strong>s specifications of functions and procedures which are used <strong>in</strong> the<br />

decomposition of the methods of the class Board.<br />

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

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

package body Class_Board is<br />

procedure Next( The:<strong>in</strong> Board; X_Co,Y_Co:<strong>in</strong> out Board_Index;<br />

Dir:<strong>in</strong> Natural; Res:out Boolean);<br />

function F<strong>in</strong>d_Turned( The:<strong>in</strong> Board; X,Y: <strong>in</strong> Board_Index )<br />

return Natural;<br />

procedure Turn_Counters(The: <strong>in</strong> out Board; X,Y: <strong>in</strong> Board_Index;<br />

Total: out Natural );<br />

function No_Turned(The:<strong>in</strong> Board; O_X,O_Y:<strong>in</strong> Board_Index;<br />

Dir:<strong>in</strong> Natural;<br />

N:<strong>in</strong> Natural := 0 ) return Natural;<br />

procedure Capture(The:<strong>in</strong> out Board; X_Co, Y_Co:<strong>in</strong> Board_Index;<br />

Dir:<strong>in</strong> Natural );<br />

The procedure setup populates the board with empty cells and the <strong>in</strong>itial central grid of four counters.<br />

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

Black_Counter: Counter; --A black counter<br />

White_Counter: Counter; --A white counter<br />

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

Set( Black_Counter, Black ); --Set black<br />

Set( White_Counter, White ); --Set white<br />

for X <strong>in</strong> The.Sqrs'range(1) loop<br />

for Y <strong>in</strong> The.Sqrs'range(2) loop<br />

Initialize( The.Sqrs(X,Y) ); --To empty<br />

end loop;<br />

end loop;<br />

Add( The.Sqrs( Size/2, Size/2 ), Black_Counter );<br />

Add( The.Sqrs( Size/2, Size/2+1 ), White_Counter );<br />

Add( The.Sqrs( Size/2+1, Size/2 ), White_Counter );<br />

Add( The.Sqrs( Size/2+1, Size/2+1 ), Black_Counter );<br />

The.Score( Black ) := 2; The.Score( White ) := 2;<br />

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