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.

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

The procedure Status returns the current state of the game. This may be a draw if both players have passed<br />

on their last go.<br />

function Status ( The:<strong>in</strong> Board ) return State_Of_Game is<br />

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

if The.Score( The.Opponent ) = 0 then<br />

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

end if;<br />

if (The.Sqrs'Length(1) * The.Sqrs'Length(2) =<br />

The.Score(The.Opponent)+The.Score(The.Player)) or<br />

(The.Last_Move(Black)=Pass and The.Last_Move(White)=Pass)<br />

then<br />

if The.Score(The.Opponent) = The.Score(The.Player)<br />

then return Draw;<br />

end if;<br />

if The.Score(The.Opponent) < The.Score(The.Player)<br />

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

else<br />

return Lose;<br />

end if;<br />

end if;<br />

return Play;<br />

end;<br />

Whilst not used, the function Contents is provided so that another user of the class Board could f<strong>in</strong>d the<br />

contents of <strong>in</strong>dividual cells.<br />

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

return Cell_Holds is<br />

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

return Holds( The.Sqrs( X, Y ) );<br />

end Contents;<br />

end Class_Board;<br />

The package Class_Player is responsible for communicat<strong>in</strong>g with the actual human player play<strong>in</strong>g the<br />

game.<br />

with Class_Counter, Class_Board;<br />

use Class_Counter, Class_Board;<br />

package Class_Player is<br />

type Player is private;<br />

procedure Set( The:<strong>in</strong> out Player; C:<strong>in</strong> Counter_Colour );<br />

procedure Get_Move(The:<strong>in</strong> Player; Row,Column:out Integer);<br />

function My_Counter( The:<strong>in</strong> Player ) return Counter;<br />

procedure Announce( The:<strong>in</strong> Player; What:<strong>in</strong> State_Of_Game );<br />

private<br />

type Player is record<br />

Plays_With : Counter;<br />

end record;<br />

end Class_Player;<br />

--Player's counter<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!