# Module 1: Creating a new class and methods

## Creating A New Console App

1. After you have opened up Visual Studio select File --> New Project.
2. Select Console App under the Windows Classic Desktop Section
3. You can name it `ConsoleGame`

![alt text](https://github.com/eleven-fifty-academy/dotnet-152-csharpconsolegame/tree/be04d4e177d4fd996670123150574e6bda4ad03c/docs/PartOne/assets/Console.PNG)

## Creating a new class

Let's begin. First thing you'll need to do is create a new **class**. For those of you who don't remember, this is done by **right-clicking** on the Console App you just created and selecting **Add -> New Item**. `Class` should be the first thing that appears in the pop-up menu. For the purposes of this Gitbook, the name of this new `class` is going to be `Character.cs`.

![alt text](https://github.com/eleven-fifty-academy/dotnet-152-csharpconsolegame/tree/be04d4e177d4fd996670123150574e6bda4ad03c/docs/PartOne/assets/class.png)

![alt text](https://github.com/eleven-fifty-academy/dotnet-152-csharpconsolegame/tree/be04d4e177d4fd996670123150574e6bda4ad03c/docs/PartOne/assets/Character.PNG)

## Creating properties

The properties here represent qualities of the `object` we're creating in the class. For the purposes of creating a character for this tutorial, the `properties` and `types` for those properties are:

* Name (string)
* Level (int)
* Luck (int)
* MaxHealth (int)
* CurrentHealth (int)

For those who do not remember, the shortcut for setting up a `property` is typing `prop` followed by pressing `Tab` twice.

```csharp
    public string Name { get; set; }
    public int Level { get; set; }
    public int Luck { get; set; }
    public int MaxHealth { get; set; }
    public int CurrentHealth { get; set; }
```

## Creating methods

Now comes `methods`. What do our characters need to do in the context of battling other characters? Again, for the purposes of this Gitbook, the following are the assumed method signatures. You can add them in directly below the `properties`.

* public int Attack(Character defender)
* public bool IsAlive()
* public void Rest()
* public void Defeat(Character defender)

Some of these methods are easier than others. `IsAlive()`, for instance, should check if the character's `CurrentHealth` is greater than 0. The method should return `true` or `false` based on the value of `CurrentHealth`. `Rest()` should just reset the character's `CurrentHealth` to their `MaxHealth`.

`Attack()` and `Defeat()` are somewhat complicated and will be covered more in-depth below.

## Attack Method

While the broad strokes are going to be covered in this section, a snippet of sample code for this method will be included at the end of the instructions.

1. Create a new instance of Random. If you call it a couple times in quick succession, `Random` isn't *actually all that random*.  Because of this, we will want to find a way to seed the `Random` instance with something more random. This is probably the hardest part of the `method`, so let's cover how this can be done. It's going to cover some terms you won't be familiar with yet, but what those are aren't actually important for the purposes of this project -- just know that you'll learn more about them later.
   1. **Call on the object** `Guid`**.**
   2. **On that call, call the method** `NewGuid()`**.**
   3. **On that call, call the method** `GetHashCode()`**.**
2. **Instantiate** a new `double` variable called `attackMultiplier`, setting it equal to a call of `NextDouble()` on your random instance, plus 1. This is because `NextDouble()` will give you a number between 0 and 1.
3. **Instantiate** a new `double` variable called `damage`, setting it equal to `Level` multiplied by the `attackMultiplier` variable you just created.
4. Create an `int` variable called `critChance`, setting it equal to a call of the `Next(101)` method on your random instance. This will create a number between 1 and 100. We pass 101 in instead of 100 because the `Next()` method is exclusive of the number passed into it, meaning that it will use numbers below, not including, the number passed in.
5. Write an **if statement** to check if `Luck` is greater than or equal to the `critChance` variable; if it is, multiply damage by two and write a message to the console indicating that there was a critical hit.
6. Create an `int` variable called `finalDam` and set it equal to `Math.Round(damage)`; you will need to **cast** this to an `int` by adding `(int)` to the beginning of the call. The `.Round()` function rounds your double to the appropriate whole number.&#x20;
7. Subtract the `finalDam` variable from the defender's `currentHealth` property.
8. Return the `finalDam` variable.

```csharp
public int Attack(Character defender)
{
    Random rnd = new Random(Guid.NewGuid().GetHashCode());
    double attackMultiplier = rnd.NextDouble() + 1;
    double damage = Level * attackMultiplier;

    int critChance = rnd.Next(101);
    if (Luck >= critChance)
    {
        damage *= 2;
        Console.WriteLine("Critical hit!");
    }

    int finalDam = (int)Math.Round(damage);
    defender.CurrentHealth -= finalDam;

    return finalDam;
}
```

## Defeat Method

This method isn't as involved as the `Attack()` method. This is for advancing the player's `Level` property when they defeat an enemy.

1. Create a `double` variable called `dExp` and set it equal to the defender's `Level` property divided by 10.
2. Create an `int` variable called `experience` and set it equal to `Math.Round(dExp)`. Much like step 6 in the previous section, you will want to cast this to an `int`.
3. Add the `experience` variable to the `Level` property, and **increment** the `Luck` variable.

```csharp
public void Defeat(Character defender)
{
    double dExp = defender.Level / 10;
    int experience = (int)Math.Round(dExp);

    Level += experience;
    Luck ++;
}
```

## IsAlive Method

Checks if the character's `CurrentHealth` is greater than 0; if it is, return `true`, otherwise return `false`.

```csharp
    public bool IsAlive()
        {
            if (CurrentHealth > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
```

## Rest Method

Resets the character's `CurrentHealth` to their `MaxHealth`.

```csharp
    public void Rest()
        {
            CurrentHealth = MaxHealth;
        }
```

Great! You've gotten through some of the hardest parts of Part 1. Now we're going to move on to setting up your constructor.
