Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

using System.Text.RegularExpressions;// new import public class Game { // FILL IN other members of Game from assignment 3 // make sure to add your Enemy to this list somewhere else! protected List...

1 answer below »
using System.Text.RegularExpressions;
new import
public class Game
{
    
FILL IN other members of Game from assignment 3
    
    
make sure to add your Enemy to this list somewhere else!
protected List enemies = new List();
    
    public void InputLoop()
    {
        bool isRunning = true;
        while (isRunning)
        {
            
FILL IN HERE
            
this code handles attack parsing, find the right place for this inside of Game.InputLoop or make your own method
            if (input.StartsWith("attack"))
            {
                
regular expressions are great for parsing text. Most languages have them in the standard li
ary.
                
must add "using System.Text.RegularExpressions;" to imports in order to use
                var regex = new Regex(@"attack ([\w\s]+) with ([\w\s]+)", RegexOptions.IgnoreCase);
                var match = regex.Match(input);
                if (match.Success)
                {
                    string attackableName = match.Groups[1].Value;
                    string weaponName = match.Groups[2].Value;
                    
find matching weapon
                    IWeapon weapon = FindWeapon(weaponName);
                    
find matching enemy in room
                    IAttackable attackable = FindAttackable(attackableName);
                    if (weapon != null && attackable != null)
                    {
                        weapon.Attack(attackable);
                        validAction = true;
                    }
                }
            }
            
            
FILL IN HERE
        }
    }
    
    
helper method for the above InputLoop attack code
    public IWeapon FindWeapon(string name)
    {
        
case-insensitive search for a weapon with matching name using a "delegate"
        
the "item => item.Name.ToLower() == name.ToLower()" is the delegate. You can
        
also manually loop through the inventory to do the same thing using a foreach,
        
but the "delegate" shortens that up. We'll go over delegates more later.
        Item foundItem = inventory.Find(item => item.Name.ToLower() == name.ToLower());
        IWeapon weapon = foundItem as IWeapon;
this cast returns null if foundItem isn't an IWeapon
        if (weapon == null)
        {
            Console.WriteLine($"Couldn't find a weapon named {name}");
        }
        return weapon;
    }
    
helper method for the above InputLoop attack code
    public IAttackable FindAttackable(string name)
    {
        
case insensitive search for an enemy with this name
        Enemy enemy = enemies.Find(enemy => enemy.Name.ToLower() == name.ToLower());
        
case insensitive search for an "attackable" item with this name
        
cast to IAttackable (or null, if it doesn't match that interface)
        IAttackable attackableItem = cu
entRoom.Items.Find(item => item.Name.ToLower() == name.ToLower()) as IAttackable;
        
if we can't find an enemy or attackable item, print an e
o
        if (enemy == null && attackableItem == null)
        {
            Console.WriteLine($"Couldn't find an attackable named {name}");
        }
        
the following "??" is called a "null coalesce" operator.
        
If the left side is null, it will return the right side. Otherwise it returns the left side.
        
(if both are null, returns null)
        return enemy ?? attackableItem;
    }
}


Weeks ago, you received a mysterious letter claiming that your late grandfather (who you don't know anything about) left you his house and land in the mountains. Having no property yourself, this is a substantial inheritance. After a few days of hiking into the countryside, you come upon the house, opulent and imperial, standing proudly against the hills leading into the mountain behind it.
You are in the entrance hall. There is a door leading further into the house to the north, and an exit to the south.
north
Troll is now in laboratory
You are in the living room. There are doors to the north, south, east, and west. There is a staircase going down.
trophy case: (150 gold) A trophy case containing a massive golden cup
elven sword: (150 gold) A leaf-bladed longsword, elven crafted.
fancy rug: (100 gold) A large, oriental-style rug with exceptional craftsmanship.
pick up elven sword
You are in the living room. There are doors to the north, south, east, and west. There is a staircase going down.
trophy case: (150 gold) A trophy case containing a massive golden cup
fancy rug: (100 gold) A large, oriental-style rug with exceptional craftsmanship.
down
You are in a tidy cellar. There are ba
els of wine here. A door leads to the north, and a staircase goes up.
wine: (50 gold) A bottle of fine wine.
north
Troll is now in Skeleton Room
You are in a large li
ary. There are many books about anatomy, history, and alchemy. Some of the books are written in Elven. There is a door to the south.
necklace: (125 gold) A ruby necklace.
elven book: (100 gold) A tome written in the indecipherable elven dialect.
south
You are in a tidy cellar. There are ba
els of wine here. A door leads to the north, and a staircase goes up.
wine: (50 gold) A bottle of fine wine.
north
Troll is now in laboratory
You are in a large li
ary. There are many books about anatomy, history, and alchemy. Some of the books are written in Elven. There is a door to the south.
necklace: (125 gold) A ruby necklace.
elven book: (100 gold) A tome written in the indecipherable elven dialect.
pick up elven book
You find yourself in a strange laboratory. A lamp with a red filter lights the room. There is a secret passage to the south. There is a door with a skull to the north.
o
: (500 gold) The O
of Yendor, an ancient artifact that has been missing for many years.
flask: (200 gold) A flask encrusted with gems.
lamp: (30 gold) A lamp with a ruby-tinted filter.
Troll is in the room.
"oh no, run away"
Invalid action.
You find yourself in a strange laboratory. A lamp with a red filter lights the room. There is a secret passage to the south. There is a door with a skull to the north.
o
: (500 gold) The O
of Yendor, an ancient artifact that has been missing for many years.
flask: (200 gold) A flask encrusted with gems.
lamp: (30 gold) A lamp with a ruby-tinted filter.
Troll is in the room.
south
The Troll attacks and slays you!
Your score was: 0
D:\Work\UCLAExtension\CSharp_1\Week_6\Assignment4\Implementation\A6_TextAdventureOOP\bin\Debug\netcoreapp3.1\A6_TextAdventureOOP.exe (process 3360) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Weeks ago, you received a mysterious letter claiming that your late grandfather (who you don't know anything about) left you his house and land in the mountains. Having no property yourself, this is a substantial inheritance. After a few days of hiking into the countryside, you come upon the house, opulent and imperial, standing proudly against the hills leading into the mountain behind it.
You are in the entrance hall. There is a door leading further into the house to the north, and an exit to the south.
north
Troll is now in laboratory
You are in the living room. There are doors to the north, south, east, and west. There is a staircase going down.
trophy case: (150 gold) A trophy case containing a massive golden cup
elven sword: (150 gold) A leaf-bladed longsword, elven crafted.
fancy rug: (100 gold) A large, oriental-style rug with exceptional craftsmanship.
pick up elven sword
You are in the living room. There are doors to the north, south, east, and west. There is a staircase going down.
trophy case: (150 gold) A trophy case containing a massive golden cup
fancy rug: (100 gold) A large, oriental-style rug with exceptional craftsmanship.
west
You are in the painting room. There is a Harpsichord. A painting depicts a skeleton holding open a gateway to an underground passage. A male elf is entering the passage. A female elf is holding a strange o
. A human man stands to the side observing.
oil painting: (150 gold) The painting depicts a skeleton holding open a gateway to an underground passage. A male elf is entering the passage. A female elf is holding a strange o
. A human man stands to the side observing.
harpsichord: (300 gold) An incredibly heavy harpsichord.
attack harpsichord with elven sword
harpsichord was destroyed by elven sword!
You are in the painting room. There is a Harpsichord. A painting depicts a skeleton holding open a gateway to an underground passage. A male elf is entering the passage. A female elf is holding a strange o
. A human man stands to the side observing.
oil painting: (150 gold) The painting depicts a skeleton holding open a gateway to an underground passage. A male elf is entering the passage. A female elf is holding a strange o
. A human man stands to the side observing.
A destroyed harpsichord, worth 0
gold: (100 gold) Shiny gold coins.
pick up gold
You are in the painting room. There is a Harpsichord. A painting depicts a skeleton holding open a gateway to an underground passage. A male elf is entering the passage. A female elf is holding a strange o
. A human man stands to the side observing.
oil painting: (150 gold) The painting depicts a skeleton holding open a gateway to an underground passage. A male elf is entering the passage. A female elf is holding a strange o
. A human man stands to the side observing.
A destroyed harpsichord, worth 0
east
Troll is now in Skeleton Room
You are in the living room. There are doors to the north, south, east, and west. There is a staircase going down.
trophy case: (150 gold) A trophy case containing a massive golden cup
fancy rug: (100 gold) A large, oriental-style rug with exceptional craftsmanship.
down
You are in a tidy cellar. There are ba
els of wine here. A door leads to the north, and a staircase goes up.
wine: (50 gold) A bottle of fine wine.
pick up wine
You are in a tidy cellar. There are ba
els of wine here. A door leads to the north, and a staircase goes up.
north
Troll is now in laboratory
You are in a large li
ary. There are many books about anatomy, history, and alchemy. Some of the books are written in Elven. There is a door to the south.
necklace: (125 gold) A ruby necklace.
elven book: (100 gold) A tome written in the indecipherable elven dialect.
pick up elven book
You find yourself in a strange laboratory. A lamp with a red filter lights the room. There is a secret passage to the south. There is a door with a skull to the north.
o
: (500 gold) The O
of Yendor, an ancient artifact that has been missing for many years.
flask: (200 gold) A flask encrusted with gems.
lamp: (30 gold) A lamp with a ruby-tinted filter.
Troll is in the room.
attack Troll with elven sword
Troll is struck with elven sword!
You find yourself in a strange laboratory. A lamp with a red filter lights the room. There is a secret passage to the south. There is a door with a skull to the north
Answered Same Day Feb 17, 2021

Solution

Shweta answered on Feb 18 2021
150 Votes
76028Solution/A3_TextAdventureOOP/.vs/A3_TextAdventureOOP/v16/.suo
76028Solution/A3_TextAdventureOOP/A3_TextAdventureOOP/A3_TextAdventureOOP.csproj



Debug
AnyCPU
{894F148F-D470-4108-84DF-F46C0CE11815}
Exe
A3_TextAdventureOOP
A3_TextAdventureOOP
v4.7.2
512
true
true


AnyCPU
true
full
false
bin\Debug\
DEBUG;TRACE
prompt
4


AnyCPU
pdbonly
true
bin\Release\
TRACE
prompt
4
























76028Solution/A3_TextAdventureOOP/A3_TextAdventureOOP/App.config




76028Solution/A3_TextAdventureOOP/A3_TextAdventureOOP
in/Debug/A3_TextAdventureOOP.exe
76028Solution/A3_TextAdventureOOP/A3_TextAdventureOOP
in/Debug/A3_TextAdventureOOP.exe.config




76028Solution/A3_TextAdventureOOP/A3_TextAdventureOOP
in/Debug/A3_TextAdventureOOP.pd
76028Solution/A3_TextAdventureOOP/A3_TextAdventureOOP/Game.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
new import
namespace A3_TextAdventureOOP
{
public class Game
{
protected Scoreboard scoreboard = new Scoreboard();
protected Room cu
entRoom;
protected Room inventory = new Room();
protected string introduction = "";
public Game()
{
}
public void StartGame()
{
Console.WriteLine(introduction);
InputLoop();
PrintScore();
}
public void PrintScore()
{
Console.WriteLine($"Your score was: {scoreboard.Score}");
}

FILL IN other members of Game from assignment 3

make sure to add your Enemy to this list somewhere else!
protected Enemy enemies = new Enemy();
public void InputLoop()
{
bool isRunning = true;
while (isRunning)
{
while (true)
{
cu
entRoom.PrintRoom();
string input = Console.ReadLine();
if (input == "quit")
{
scoreboard.Score = (uint)(scoreboard.Score + inventory.Items.Sum(x => x.PointValue));

eak;
}
if (input.StartsWith("pick up"))
{
string[] cu
entItem = input.Split(' ');
string cu
ent = "";
if (cu
entItem.Length > 2)
{
for (int i = 2; i < cu
entItem.Length; i++)
{
if (cu
ent == "")
{
cu
ent = cu
entItem[i];
}
else
{
cu
ent = cu
ent + " " + cu
entItem[i];
}
}
if (cu
entRoom.Items.Any(x => x.Name == cu
ent))
{
if (inventory.Items == null)
{
inventory.Items = new List();
}
inventory.Items.Add(cu
entRoom.Items.Where(x => x.Name == cu
ent).FirstOrDefault());
scoreboard.Score = (uint)(scoreboard.Score + inventory.Items.Sum(x => x.PointValue));
cu
entRoom.Items.RemoveAll(x => x.Name == cu
ent);
}
else
{
Console.WriteLine("No such item found, please co
ect your entry");
}
}
else
{
Console.WriteLine("Please enter item to pick up");
}

do something
continue;
}
else if (input.StartsWith("drop"))
{
string[] cu
entItem = input.Split(' ');
string cu
ent = "";
if (cu
entItem.Length > 2)
{
for (int i = 2; i < cu
entItem.Length; i++)
{
if (cu
ent == "")
{
cu
ent = cu
entItem[i];
}
else
{
cu
ent = cu
ent + " " + cu
entItem[i];
}
}
if (cu
entRoom.Items.Any(x => x.Name == cu
ent))
{
if (inventory.Items == null)
{
inventory.Items = new List();
}
if (inventory.Items.Any(x => x.Name == cu
ent))
{
scoreboard.Score = (uint)(scoreboard.Score - inventory.Items.Sum(x => x.PointValue));
cu
entRoom.Items.Add(inventory.Items.Where(x => x.Name == cu
ent).FirstOrDefault());
}
else
{
Console.WriteLine("No such item found, please co
ect your entry");
}
}
else
{
Console.WriteLine("Please enter item to drop");
}

do something
continue;
}
}

this code handles attack parsing, find the right place for this inside of Game.InputLoop or make your own method
else if (input.StartsWith("attack"))
{

regular expressions are great for parsing text. Most languages have them in the standard li
ary.

must add "using System.Text.RegularExpressions;" to imports in order to use
var regex = new Regex(@"attack ([\w\s]+) with ([\w\s]+)", RegexOptions.IgnoreCase);
var match = regex.Match(input);
if (match.Success)
{
string attackableName = match.Groups[1].Value;
string weaponName = match.Groups[2].Value;

find matching weapon
IWeapon weapon = FindWeapon(weaponName);

find matching enemy in room
IAttackable attackable = FindAttackable(attackableName);
if (weapon != null && attackable != null)
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here