in reply to Adventure Game ->object
Having all these objects, now it is easy to write someting like a main server. In pseudo code it's quite that simple:Item + new($name, $points) + getName, getPoints Room + new($name, $description, $room_north, $room_south, $room_west, $room +_east, @item) + addItem($item), removeItem($name) + getName, getDescription, ... Map + load($filename) + initialize_rooms($filename) + getRoom($name), getRoom($row, $col) Player + new($name, $current_room, $points) + move($direction) + take($item_name) + drop($item_name) # perhaps a good addition + throw_away($item_name) # perhaps also a good addition + look($item_name) + getName, getCurrentRoom, getPoints, getInventory : returns @item Game + new($map, %player_name_and_current_rooms) + quit + getMap, getPlayer : return @player
While a client (one player) would now only do the graphics (pseudocode):my %player_names_and_start_room_names = ( bob => 'dark_hall', jay => 'forest', ); $map = Map->load($map_filename); $map->initialize_rooms($room_filename); $game->new($map, %player_names...);
Of course, that's quite only my idea of an object oriented design. But to answer your main question, in my opinion, it's important to seperate the parts that are valid in all adventure games (rules, basic actions, a map) from them that are only valid in your (your current map, nr of players, ...) specific adventure games.while (1) { print "You are in room", $player->getCurrentRoom->getName; print "There are several items in this room:", map {$_->getName}, $player->getCurrentRoom->getItems; print "In the north, there is ", $player->getCurrentRoom->getNorthRoom->getName; ... print "Choose: move (N),(S),(W),(E); (T)ake, (L)ook, (I)nventory, ( +Q)uit"; my $action = read_action; if ($action =~ /[NSWE]/) { $player->move($action) or print "Couldn't move to $action"; } elsif ($action =~ /[TL]/) { my $item = read_item; if ($action eq 'T') { $player->take($item) or print "Couldn't take $item"; } else { print $player->getCurrentRoom->getItem($item)->getDescripti +on || "No description available"; } } }
Greetings,
Janek
|
|---|