about summary refs log tree commit diff stats
path: root/perl/game.pl
blob: a6bcb00c564325f68a711e5c1e1eed5ae6b51094 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%rooms = (
  entrance => {
    description => "You enter a dimly lit room with a heavy wooden door to the north and a narrow hallway to the east.",
    exits => { north => "hallway", east => "storage" },
    items => [],
  },
  hallway => {
    description => "The hallway stretches to the north and south. A faint light flickers at the end of the south corridor.",
    exits => { north => "bedroom", south => "livingRoom" },
    items => [],
  },
  bedroom => {
    description => "The bedroom is cozy and inviting. A large bed dominates the room, and a dresser stands against the wall.",
    exits => { south => "hallway" },
    items => [],
  },
  livingRoom => {
    description => "The living room is spacious and warmly lit. A comfortable couch sits in front of a fireplace, and a large bookshelf lines the wall.",
    exits => { north => "hallway" },
    items => [],
  },
  kitchen => {
    description => "The kitchen is well-equipped with modern appliances and a large dining table. A faint glow emanates from a flashlight on the countertop.",
    exits => { south => "livingRoom" },
    items => ["flashlight"],
  },
  backyard => {
    description => "The backyard is a tranquil oasis with lush greenery and a towering oak tree. A gentle breeze rustles the leaves, creating a peaceful atmosphere.",
    exits => { north => "kitchen" },
    items => [],
  },
);


$currentRoom = $rooms{entrance};
@inventory = ();

do {
  print $currentRoom->{description} . "\n";

  $input = <STDIN>;
  chomp($input);

  if ($input eq "leave") {
    exit;
  } elsif ($input eq "inventory") {
    print "Inventory: " . join(", ", @inventory) . "\n";
  } elsif ($input =~ /^take\s+(\w+)/) {
    $itemName = $1;
    if (grep { $_ eq $itemName } @{$currentRoom->{items}}) {
      push @inventory, $itemName;
      splice @{$currentRoom->{items}}, grep { $_ eq $itemName } 1;
      print "You picked up the $itemName.\n";
    } else {
      print "There is no $itemName in this room.\n";
    }
  } elsif ($input eq "help") {
    print "Available commands: north, south, east, west, inventory, take item, leave\n";
  } else {
    if (exists $currentRoom->{exits}->{$input}) {
      $currentRoom = $rooms{$currentRoom->{exits}->{$input}};
    } else {
      print "Invalid direction. Please enter a valid direction or type 'help' for assistance or 'leave' to end the exploration.\n";
    }
  }
} while (1);