Click here to Skip to main content
14,880,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So,I have written code for a basic Hero vs Monster program.The problem is when I run the code,the value of hero attack,hero health,monster attack and monster health (that means EVERYTHING) doesn't change.
C#
namespace ConsoleApplication1
{
    class Battle
    {
        public static int heroat, monsterat, herohealth, monsterhealth;
        
        public Battle()
        {
            heroat = 0;
           monsterat=0;
            
            herohealth = 100;
            monsterhealth = 100;
            do
            {
                Console.WriteLine(@" 
            {2} Health={0}               Monster Health={1}
                Hero's Attack={3}        Monster's Attack={4}", herohealth, monsterhealth, Main.heroname,heroat,monsterat);
            
            
                HeroAtk(heroat, herohealth, monsterat, monsterhealth);
                MonsAtk(heroat, herohealth, monsterat, monsterhealth);
               
                Console.ReadLine();
            }
            while (herohealth > 0 && monsterhealth > 0);



        }
        public static void HeroAtk(int heroat,int herohealth,int monsterat,int monsterhealth)
        {Random rand;
            rand=new Random();
            heroat = rand.Next(10, 21);
            monsterhealth=monsterhealth - heroat;
           
        }
        public static void MonsAtk(int heroat, int herohealth, int monsterat, int monsterhealth )
        {
            Random rand;
            rand = new Random();
            monsterat = rand.Next(10, 20);
            herohealth = herohealth - monsterat;
           
        }
        
        }
    }

What am I doing wrong?And how do I correct it?
Posted
Comments
[no name] 10-Aug-14 5:36am
   
The most likely reason that this would happen would be that you are never calling this code.

First off, stop creating new Random instance when you want them: create one at class level:
C#
private Random rand = new Random();
Because Random starts it's generation using the system clock as a starter, if you create them repeatedly you are likely to generate the same sequence each time!

Secondly, you need to understand what happens when you pass a parameter to a method: the variable you pass is passed by value, not by reference. What that means is that a copy of the variable is taken and passes to the method, which can modify it all it likes, because it will not affect the world outside the method! (There is a more detailed explanation of what happens in this: Using struct and class - what's that all about?[^] but it might be a bit too much for you as a beginner).

Think about it:
C#
void add(int a)
   {
   a = a + 1;
   }
Is fine and dandy when you do this:
C#
int A = 6;
add(A);
But what happens when you do this:
C#
add(6);
You can't start changing constants! :laugh:

You could do it by passing a reference to the value - and that's very easy - but a better solution is to use the current class values, and not pass anything:

C#
namespace ConsoleApplication1
{
    class Battle
    {
        public int heroat, monsterat, herohealth, monsterhealth;
        private Random rand = new Random();
        public Battle()
        {
            heroat = 0;
            monsterat=0;
            
            herohealth = 100;
            monsterhealth = 100;
            do
            {
                Console.WriteLine(@" 
            {2} Health={0}               Monster Health={1}
                Hero's Attack={3}        Monster's Attack={4}", herohealth, monsterhealth, Main.heroname,heroat,monsterat);
            
            
                HeroAtk();
                MonsAtk();
               
                Console.ReadLine();
            }
            while (herohealth > 0 && monsterhealth > 0);
        }
        public void HeroAtk()
        {
            heroat = rand.Next(10, 21);
            monsterhealth=monsterhealth - heroat;
           
        }
        public void MonsAtk()
        {
            monsterat = rand.Next(10, 20);
            herohealth = herohealth - monsterat;
           
        }
        
    }
}
There are a couple of other changes it'd be worth doing, but they can wait until you have this working.
   
Comments
Andreas Gieriet 10-Aug-14 8:25am
   
My 5!
Cheers
Andi
OriginalGriff 10-Aug-14 8:38am
   
You're welcome!
Andreas Gieriet 10-Aug-14 8:43am
   
PS: I gave you a 5 but CP seems to ignore that... - Do you see my upvote (I don't)?
Cheers
Andi
Hi,


I think you have mixed up with Deep Copy and Shallow Copy

Definition:

Quote:
The terms "Shallow Copy " and "Deep Copy " which refer to the way the objects are copied, for example, during the invocation of a copy constructor or assignment operator. The deep copy can also be called as member wise copy and the copy operation respects object semantics. For example, copying an object that has a member of type standard string ensures that the corresponding standard string in the target object is copy-constructed by the copy constructor of class string.

Shallow copy: This is nothing but creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type then a bit-by-bit copy of the field is performed. If it is a reference type then the reference is copied but not the referred object. Therefore, the original object and its clone refer to the same object.

Deep copy: Deep copy is partially same as shallow copy, but the difference is deep copy copies the whole object and makes a different object, it means it do not refer to the original object while in case of shallow copy the target object always refer to the original object and changes in target object also make changes in original object. It serializes the objects and deserializes the output.


Well explained : http://seesharpconcepts.blogspot.in/2012/05/shallow-copy-and-deep-copy-in-c.html[^]

You have used Int which is structure so when you assign an integer from another integer it will deep copy the values which mean it will not hold the reference so if you want achive the same you can use REF[^] but mind dont use OUT[^] because you need to assign it with in the function but you need to continue its value within the function and after manipulation you will get the manipulated value.

Enjoy..
   
Comments
Suvabrata Roy 11-Aug-14 1:00am
   
Please provide a reason of down vote

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900