-3

So I am trying to make this wizard battle game which will heavily use Random Number Generator for plethora of things such as choosing level, name, and spells for a enemy wizard. I have generated the basic number generator but I am wondering how do I call this to Main class? Here is the code for what I have done so far. I am absolutely new to programming so I do apologize.

using System;
namespace Battle_Wizard
{
    class Program
    {

        static void Main(string[] args)
        {
            string Player;
            Console.WriteLine("Hello Wizard! What is your name: (Insert your name) ");
            Player = Console.ReadLine();
            Console.WriteLine("So your name is "+ Player + "? " + "What a stupid name. \nPRESS ANY BUTTON FOR A BATTLE!");
            Console.ReadKey();
           
        }
        public class Wizards 
        {
            string [] names = {"Ifeus","Avutaz","Alvapan","Inawyn","Agrukey","Unageor","Anvigron","Ubus","Enoviar","Unitor"};
            string [] spells = {"A Alakablam ","Y0ur m0m ","A Karate Chop ","Abra-kadabra ","A 12 Gauge Shotgun ","Telekinesis "};
            string [] deathmessages = {" set their pants on fire by ", " shot in the face with a ", " perished painfully with  "};
        }

            public class NumberGenerator 
        {
            Random rnd = new Random();
            int EnemyRoll = new Random().Next( 1, 10 );
            int PlayerRoll = new Random().Next( 1, 10 ); 
        }
    }
}
Mikalai Lushchytski
  • 1,563
  • 1
  • 9
  • 18
KomoGit
  • 3
  • 3
  • What exactly do you want this NumberGenerator class to do? Right now it only has private fields and nothing else, so you can't do anything with it. You can create an instance in main with the line `NumberGenerator generator = new NumberGenerator();`. Then when the class has public members, you'll be able to call them from main. – Verpous Oct 11 '20 at 15:05

2 Answers2

0

Your NumberGenerator class is public however, you're going to want to make that static. The reason for this is because you don't want your NumberGenerator to be able to be instantiated. What is instantiation, I'm glad you asked.

Here's an example of instantiation.

public class Car {

    //CAR properties sometimes referred to as "Member Variables."
    private string _color;
    private int _wheels;

    //Initialization method. This is what tells the compiler what goes where and it's typing. 
    public Car (string color, int wheels) {
        _color = color;
        _wheels = wheels;
    }
}

public SomeClass {
    //Instantiation of that car we created above. 
    Car someCar = new Car ("Blue", 4);
    
    //Using the properties of that Car object.
    string someColor = someCar.wheels;
    int someWheelNum = someCar.wheels;
}

Next, you need to think of your class NumberGenerator as a simple file to hold other methods. Inside of a class, you can have properties, For example, you might have a static name that is accessible from outside. However since we're doing a static class, that's not necessary. We do need a method of some sort within that class.

public int getRandomNum() {
    //Do something here.
    return someInt
}

Finally, all you need to do is use it with dot syntax.

int someInt = NumberGenerator.getRandomNum();

I encourage you to go learn to build Class Objects as a next step. It's a precursor to what you're attempting to do. Build a class object that's a car with some computed properties.

Here's a great tutorial video for you to follow. https://www.youtube.com/watch?v=ZqDtPFrUonQ

Dharman
  • 30,962
  • 25
  • 85
  • 135
xTwisteDx
  • 2,152
  • 1
  • 9
  • 25
  • I truly appriciate the time you took to make this answer. The tutorial was insightful but it was way too complicated for me to understand so it kind of flew over my head. Again though, thanks a lot. – KomoGit Oct 11 '20 at 15:13
0

Here you are creating 3 instances of Random with the new keyword. It is better to use a single instance within the NumberGenerator class.

public class NumberGenerator 
{
    Random rnd = new Random();
    int EnemyRoll = new Random().Next( 1, 10 );
    int PlayerRoll = new Random().Next( 1, 10 ); 
}

I would rewrite this class like this:

public class NumberGenerator
{
    Random rnd;

    public NumberGenerator() 
    {
        rnd = new Random();
    }

    // use seperate methods for each thing you want to generate
    int generateEnemyRoll()
    {
        return rnd.Next(1, 10);
    }
    int generatePlayerRoll()
    {
        return rnd.Next(1, 10);
    }
    string generateDeathMessage()
    {
        return Wizards.deathmessages[rnd.Next(0, Wizards.deathmessages.Length)];
    }
    // etc for all the other things you need to generate

}

Also, on the Wizards class, you have three arrays of strings which I presume are never going to change (during runtime), so you can put static on them so they don't require an object reference to access (You don't need to create a Wizard object to access the strings). You could also just put these within the RandomGenerator class or within the generateDeathMessage() method.

Like so...

public class Wizard
{
    static string [] names = {"Ifeus","Avutaz","Alvapan","Inawyn","Agrukey","Unageor","Anvigron","Ubus","Enoviar","Unitor"};
    static string [] spells = {"A Alakablam ","Y0ur m0m ","A Karate Chop ","Abra-kadabra ","A 12 Gauge Shotgun ","Telekinesis "};
    static string [] deathmessages = {" set their pants on fire by ", " shot in the face with a ", " perished painfully with  "};
}

How to use from the main class? Main is a method not a class, but from the Main method you can do this...

static void Main(string[] args)
{
    string Player;
    Console.WriteLine("Hello Wizard! What is your name: (Insert your name) ");
    Player = Console.ReadLine();
    Console.WriteLine("So your name is "+ Player + "? " + "What a stupid name. \nPRESS ANY BUTTON FOR A BATTLE!");

    NumberGenerator generator = new NumberGenerator();

    int enemyRoll = generator.generateEnemyRoll();
    int playerRoll = generator.generatePlayerRoll();
    string deathMessage = generator.generateDeathMessage();

    // etc

    Console.ReadKey();       
}
silver6454
  • 150
  • 1
  • 5
  • Man you are a saint. I have been looking things up for whole day and couldn't find nothing. If you have discord is it okay if I add you to ask a few things? I would truly appricate that! Again thanks a lot! – KomoGit Oct 11 '20 at 15:22
  • Hi, glad I could be of help. I don't have discord, but I'm happy to answer any questions you have in the comments – silver6454 Oct 11 '20 at 15:26
  • Hey, sorry if I am bothering you. I have recently implemented the gameplay system to my code. It works all fine and dandy but one major problem is that regardless of whether the player wins or loses the battle after the battle finishes the game concludes. I am trying to loop the game so the player can play until they roll lower than the enemy. https://github.com/KomoGit/Battle-Wizard/blob/main/Program.cs Check the improved code here. Thanks again! :) – KomoGit Oct 12 '20 at 09:22
  • Hi, I've updated your code to use a more object oriented approach that should be a bit clearer. Download the zip from here https://safenote.co/r/5f847ee9ca6489@64122419 – silver6454 Oct 12 '20 at 16:09
  • Compared to your code, it looks as if the code I wrote was made by cavemen banging two rocks so thank you. Can you tell me what does newtonsoft.json do? I read some documentation on Google but couldn't understand a thing. – KomoGit Oct 12 '20 at 16:28
  • It just reads and writes data in JSON format (JavaScript Object Notation), which is a simple text-based format for storing the data in an object (class instance). Newtonsoft.Json has a class called JsonConvert that can convert between any object (including user-defined objects) and JSON strings which can be stored in a text file. This format is often used for web APIs and storing settings and data from applications that are small enough that a full database is not required. Using an external file means that you can change the strings that your code uses without having to recompile the project. – silver6454 Oct 12 '20 at 16:57
  • JSON uses a key:value format, so you have a key, which is defined in the class, such as `myInt` -> `"myInt"` and a value, which is the `int` value converted to a `string`. So you might have `"myInt": 1`. Newtonsoft.Json reads the character `'1'` and converts it to the int `1` which is then assigned to the member `myInt` of whichever class you are serializing (converting to a string). It also has converters for all other types, such as `DateTime` and arrays, so really it's just a library that makes storing data externally easier. – silver6454 Oct 12 '20 at 17:03
  • I seriously can't thank you enough for taking the time to teach me something. I am really glad that I started asking questions, as before I never did and failed many times on learning programming. Again, thanks a lot! – KomoGit Oct 12 '20 at 17:10
  • Hey there, I am sorry for bothering you again, but I have been trying to reverse engineer your code and see how you were able to read files from the .json file and write it out on console and so far failed. bit.ly/3jZ1sk3 This is the JSON file I have been trying to parse. bit.ly/3j0bLDd and this is the code I have written that doesn't even work. Could you give me a tip on what should I do? – KomoGit Oct 14 '20 at 18:30
  • Hi, the link doesn't let me view your code, but I can see this JSON file `{"Player01":{"name":"O'Neill", "exp":5, "ban":false}` and it looks like you're missing a closing `}` character from the end of the file. You also need to make sure you have the same structure in your code, so instead of labelling as `"Player01"`, have an `int playerNumber` member of your player class so you can just serialize a list of players to the file, rather than retrieving them with a custom identifier from each one `{"players":[{"playerNum": 1, etc.. }]}`. If you show me any errors I can help more. – silver6454 Oct 17 '20 at 10:45
  • Hi, thanks a lot for the tip, sorry, for the late response and the fact that the link wasn't working. – KomoGit Oct 18 '20 at 14:14