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.

Arrays 109<br />

8.4.1 The class Board<br />

The specification for the class Board is def<strong>in</strong>ed by the package Class_board as follows:<br />

package Class_Board is<br />

type Board is private;<br />

type Game_State is ( W<strong>in</strong>, Playable, Draw );<br />

procedure Add( The:<strong>in</strong> out Board; Pos:<strong>in</strong> Integer;<br />

Piece:<strong>in</strong> Character );<br />

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

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

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

return Character;<br />

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

-- Not a concern of the client<br />

end Class_Board;<br />

8.4.2 Implementation of the game<br />

Us<strong>in</strong>g the above package specification, the follow<strong>in</strong>g code will facilitate the play<strong>in</strong>g the game of noughts and<br />

crosses between two human players. The procedure Display will display the state of the board onto the user’s<br />

term<strong>in</strong>al. By factor<strong>in</strong>g out the code to display the board, from the class Board the class can be re-used <strong>in</strong> other<br />

programs that may use a different form of display, for example a GUI (Graphical User Interface).<br />

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

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

procedure Ma<strong>in</strong> is<br />

Player : Character; --Either 'X' or 'O'<br />

Game : Board; --An <strong>in</strong>stance of Class Board<br />

Move : Integer; --Move from user<br />

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

Player := 'X';<br />

--Set player<br />

while State( Game ) = Playable loop --While playable<br />

Put( Player & " enter move (1-9) : "); -- move<br />

Get( Move ); Skip_L<strong>in</strong>e; -- Get move<br />

if Valid( Game, Move ) then --Valid<br />

Add( Game, Move, Player ); -- Add to board<br />

Display( Game );<br />

-- Display board<br />

case State( Game ) is --Game is<br />

when W<strong>in</strong> =><br />

Put( Player & " w<strong>in</strong>s");<br />

when Playable =><br />

case Player is --Next player<br />

when 'X' => Player := 'O'; -- 'X' => 'O'<br />

when 'O' => Player := 'X'; -- 'O' => 'X'<br />

when others => null; --<br />

end case;<br />

when Draw =><br />

Put( "It's a draw ");<br />

end case;<br />

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

else<br />

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

end if;<br />

end loop;<br />

New_L<strong>in</strong>e(2);<br />

end Ma<strong>in</strong>;<br />

--for 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!