lundi 5 décembre 2016

Creating a Random number(in Class) for later extensive use in program C#

I have to create a program where the player has 6 choices, between 2 pets, he must decide either to feed, talk or play with each one, however he can only do one at a time and each time he chooses an option the option chosen (except when talking) makes the other options increase while the former one decreases.

Im having trouble in all aspects, inside the program and inside the class. I can't seem to define the random numbers inside the Class, and that makes the program not work.

This is the Program so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tamagochi
{
class Program
{
    static void Main(string[] args)
    {
        string userString;
        bool keepPlaying = true;
        Tamagochi tamagochi1 = new Tamagochi ("Blubi", 4.0, 4.0);
        Tamagochi tamagochi2 = new Tamagochi ("Flugin", 4.0, 4.0);
        while (keepPlaying)
        {

            Console.WriteLine("Welcome, You will now take care of a tamagochi");
            Console.WriteLine("To make that you will have to feed him and play with him.");
            Console.WriteLine("If you don't take care of him he will become unhappy and eventually run away! So be Careful");
            Console.WriteLine("When hunger level or boredom goes over 20, it will run away.");

            int choice;

            while (tamagochi1.GetHungerLvl() < 20.0 & tamagochi1.GetBoredomLvl() < 20.0 & tamagochi2.GetHungerLvl() < 20.0 & tamagochi2.GetBoredomLvl() < 20)
            {
                Console.WriteLine($"\nNow, this is your tamagiochi's Hunger level: {tamagochi1.GetHungerLvl()}  and this his Boredom Level: {tamagochi1.GetBoredomLvl()}");
                Console.WriteLine("Now you have 6 Options chosen by entering numbers 1 to 6: 1.Feed Blubi 2.Talk to Blubi 3.Play with Blubi ");
                Console.WriteLine("4.Feed Flugin 5.Talk to Flugin 6.Play with Flugin");
                do
                {
                    userString = Console.ReadLine();


                    //Here, convert anything else that isn't a number into a 0 making it invalid
                    if (!(Int32.TryParse(userString, out choice)) || (choice <= 0) || (choice > 6))
                    {
                        Console.WriteLine("It seems that input is wrong try again");
                    }
                } while ((choice <= 0) || (choice > 6));
                if (choice == 1)
                {
                    tamagochi1.IncreaseBoredomLvl();
                    tamagochi1.IncreaseHungerLvl();
                    tamagochi2.IncreaseBoredomLvl();
                    tamagochi2.IncreaseHungerLvl();
                }
                if (choice == 3)
                {
                    tamagochi1.IncreaseBoredomLvl();
                    tamagochi1.IncreaseHungerLvl();
                    tamagochi2.IncreaseBoredomLvl();
                    tamagochi2.IncreaseHungerLvl();
                }
                if (choice == 4)
                {
                    tamagochi1.IncreaseBoredomLvl();
                    tamagochi1.IncreaseHungerLvl();
                    tamagochi2.IncreaseBoredomLvl();
                    tamagochi2.IncreaseHungerLvl();
                }
                if (choice == 6)
                {
                    tamagochi1.IncreaseBoredomLvl();
                    tamagochi1.IncreaseHungerLvl();
                    tamagochi2.IncreaseBoredomLvl();
                    tamagochi2.IncreaseHungerLvl();
                }

            }

            Console.WriteLine("New game? y/n");
            ConsoleKeyInfo cki = Console.ReadKey();
            keepPlaying = cki.KeyChar == 'y';
        }
    }
}
}

And this is the code for the Class so far, here is where the problem must be happening:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tamagochi
{
class Tamagochi
{
    private string tamagochi;
    private double hungerLvl;
    private double boredomLvl;
    private double totalLevels;
    private static int time;
    private static int RandomNumberGenerator;
    private static int ranNum1;
    private static int ranNum2;
    private static int ranNum3;
    private static int ranNum4;
    public Tamagochi (string tamagochi)
        {
        this.tamagochi = tamagochi;
        }

    public Tamagochi (string tamagochi, double hungerLvl, double boredomLvl, double randomNumbers, int RandomNumberGenerator)
    {
        this.tamagochi = tamagochi;
        this.hungerLvl = hungerLvl;
        this.boredomLvl = boredomLvl;
        this.RandomNumberGenerator = RandomNumberGenerator;

    }
    public static void IncreaseTime()
    {
        ++time;
    }
    public static void IncreaseBoredomLvl(double boredomLvl, int random)
    {
        boredomLvl += // randomnumber
    }
    public static void IncreaseHungerLvl(double hungerLvl)
    {
        hungerLvl += //randomnumber
    }
    public static void randomNumber(int min, int max)
    {
        Random RandomNumberGenerator = new Random();
        ranNum1 = RandomNumberGenerator.Next(1, 6);
        ranNum2 = RandomNumberGenerator.Next(1, 6);
        ranNum3 = RandomNumberGenerator.Next(1, 6);
        ranNum4 = RandomNumberGenerator.Next(1, 6);

    }


    //return section

    public string GetTamagochi()
    {
        return tamagochi;
    }
    public double GetBoredomLvl()
    {
        return boredomLvl;
    }
    public double GetHungerLvl()
    {
        return hungerLvl;
    }
    public static int GetTime()
    {
        return time;

    }
    public static int GetRandomNum()
    {
        return ranNum1;
    }
}
}

Any help on trying to make this work?




Aucun commentaire:

Enregistrer un commentaire