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.

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

The procedure Now_Play<strong>in</strong>g records the colour of the current player. This <strong>in</strong>formation is used by<br />

subsequent methods add and Check_Move.<br />

procedure Now_Play<strong>in</strong>g(The:<strong>in</strong> out Board; C:<strong>in</strong> Counter_Colour) is<br />

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

The.Player := C;<br />

--Player<br />

case C is<br />

--Opponent<br />

when White => The.Opponent := Black;<br />

when Black => The.Opponent := White;<br />

end case;<br />

end Now_Play<strong>in</strong>g;<br />

The procedure Display displays a representation of the reversi board on the output device. For this<br />

implementation of the game, the output device is an ANSI text-based term<strong>in</strong>al.<br />

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

Dashes: Str<strong>in</strong>g( 1 .. The.Sqrs'Length*4+1 ) := (others=>'-');<br />

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

Screen_Clear;<br />

--Clear screen<br />

Put( Dashes ); New_L<strong>in</strong>e; --Top<br />

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

Put("|");<br />

--Cells on l<strong>in</strong>e<br />

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

Put(" "); Display( The.Sqrs(X,Y) ); Put(" |");<br />

end loop;<br />

New_L<strong>in</strong>e; Put( Dashes ); New_L<strong>in</strong>e; --Bottom l<strong>in</strong>es<br />

end loop;<br />

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

Put( "Player X has " );<br />

Put( Integer(The.Score(Black)), Width=>2 );<br />

Put( " counters" ); New_L<strong>in</strong>e;<br />

Put( "Player O has " );<br />

Put( Integer(The.Score(White)), Width=>2 );<br />

Put( " counters" ); New_L<strong>in</strong>e;<br />

end Display;<br />

The function Check_Move checks the validity of a proposed move on the board. This function is decomposed<br />

<strong>in</strong>to the function F<strong>in</strong>d_Turned which calculates the number of pieces that will be turned if a move is made <strong>in</strong>to<br />

the specified square.<br />

function Check_Move( The:<strong>in</strong> Board; X,Y:<strong>in</strong> Integer )<br />

return Move_Status is<br />

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

if X = 0 and then Y = 0 then<br />

return Pass;<br />

elsif X <strong>in</strong> Board_Index and then Y <strong>in</strong> Board_Index then<br />

if Holds( The.Sqrs( X, Y ) ) = Empty then<br />

if F<strong>in</strong>d_Turned(The, X, Y) > 0 then<br />

return Ok;<br />

end if;<br />

end if;<br />

end if;<br />

return Invalid;<br />

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