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.

112 Arrays<br />

The functions valid returns true if the square selected is not occupied by a previously played counter.<br />

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

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

return Pos <strong>in</strong> Board_Array'Range and then<br />

The.Sqrs( Pos ) = ' ';<br />

end Valid;<br />

Note:<br />

The use of and then so that the check on the board is only made if the position is valid.<br />

The function Cell returns the contents of a cell on the noughts and crosses board. This method is used to<br />

<strong>in</strong>terrogate the state of the board, without hav<strong>in</strong>g to know how the state is stored. Us<strong>in</strong>g this method, pr<strong>in</strong>t<strong>in</strong>g of<br />

the state of the board can be separated from the code that manipulates the 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 sets the state of the board back to its <strong>in</strong>itial 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 />

The Procedure State returns the current state of play as represented by the board. A two-dimensional array<br />

All_W<strong>in</strong>_L<strong>in</strong>es holds the co-ord<strong>in</strong>ates of the eight possibles w<strong>in</strong> l<strong>in</strong>es. The co-ord<strong>in</strong>ates <strong>in</strong> this array are used<br />

to f<strong>in</strong>d any l<strong>in</strong>e that conta<strong>in</strong>s three cells either conta<strong>in</strong><strong>in</strong>g all X’s or O’s.<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<br />

return W<strong>in</strong>;<br />

-- Found a 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 so draw<br />

else<br />

return Playable;<br />

-- Still playable<br />

end if;<br />

end State;<br />

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