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.

322 A Text user <strong>in</strong>terface<br />

function State( The:<strong>in</strong> Board ) return Game_State is<br />

subtype Position is Integer range 1 .. 9;<br />

type W<strong>in</strong>_L<strong>in</strong>e is array( 1 .. 3 ) of Position;<br />

type All_W<strong>in</strong>_L<strong>in</strong>es is range 1 .. 8;<br />

Cells: constant array ( All_W<strong>in</strong>_L<strong>in</strong>es ) of W<strong>in</strong>_L<strong>in</strong>e :=<br />

( (1,2,3), (4,5,6), (7,8,9), (1,4,7),<br />

(2,5,8), (3,6,9), (1,5,9), (3,5,7) ); --All w<strong>in</strong> l<strong>in</strong>es<br />

First : Character;<br />

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

for Pwl <strong>in</strong> All_W<strong>in</strong>_L<strong>in</strong>es loop --All Pos W<strong>in</strong> L<strong>in</strong>es<br />

First := The.Sqrs( Cells(Pwl)(1) ); --First cell <strong>in</strong> l<strong>in</strong>e<br />

if First /= ' ' then -- Looks promis<strong>in</strong>g<br />

if First = The.Sqrs(Cells(Pwl)(2)) and then<br />

First = The.Sqrs(Cells(Pwl)(3)) then return W<strong>in</strong>;<br />

end if;<br />

end if;<br />

end loop;<br />

if The.Moves >= 9 then --Check for draw<br />

return Draw;<br />

-- Board full<br />

else<br />

return Playable;<br />

-- Still playable<br />

end if;<br />

end State;<br />

The procedure Cell returns the contents of a cell on the play<strong>in</strong>g board.<br />

function Cell( The:<strong>in</strong> Board; Pos:<strong>in</strong> Integer ) return Character is<br />

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

return The.Sqrs( Pos );<br />

end Cell;<br />

The procedure reset resets the board to its <strong>in</strong>itial empty state.<br />

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

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

The.sqrs := ( others => ' '); --All spaces<br />

The.moves := 0; --No of moves<br />

end reset;<br />

end Class_Board;<br />

22.5.2 Package Pack_Program<br />

The package Pack_Program conta<strong>in</strong>s the publicly visible procedure Play that will play the GUI based game<br />

aga<strong>in</strong>st two human opponents. In the private part of the package, the procedure Play is broken down <strong>in</strong>to further<br />

procedures and functions. The variables <strong>in</strong> the private part of the specification will be visible to all these<br />

procedure and functions.<br />

These variables def<strong>in</strong>e w<strong>in</strong>dows that are used <strong>in</strong> the play<strong>in</strong>g of the game of noughts and crosses.<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!