Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello:)

It's my first post hiere then sorry for every mistakes.

I am writting simple console objective game application using Visual Studio 2012 and C# and i got some troubles with StackOverflow Exception.

In this game we have 2 classes:
-Class Soldier and Class Program(main class)

And i declare 2 object with properities.

Its z1 and z2 object with properties Sila(Strength), Zrecznosc(Aguillity), Wytrzymalosc(its like hitpoints) and ksywa(means nicname)

Ok so this 2 object are fighting together using Walcz method which contain equations which calculate stats.

This method should be recurentive, and she should still modificate the value of hitpoints.

What is wrong with my code?

Please help me :)

This is my code:

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

namespace Gra_Zolnierze
{
    class Zolnierz ////////////////////////////////////////////////// klasa Zolnierz /////////////////////////////////
    {
        public string Ksywa { get; set; }
        public double Sila { get; set; }
        public double Zrecznosc { get; set; }
        public double Wytrzymalosc { get; set; }

        public Zolnierz(string Ksywa, double Sila, double Zrecznosc, double Wytrzymalosc) // konstruktor
        {
            this.Ksywa = Ksywa;
            this.Sila = Sila;
            this.Zrecznosc = Zrecznosc;
            this.Wytrzymalosc = Wytrzymalosc;

        }

        public void PrzedstawSie() // metoda wyswietlajaca informacje o obiektach
        {
            Console.WriteLine("Jestem {0} ", this.Ksywa);
            Console.WriteLine("Moja sila to {0} ", this.Sila);
            Console.WriteLine("Moja zrecznosc to {0} ", this.Zrecznosc);
            Console.WriteLine("Moja wytrzymalosc to {0} ", this.Wytrzymalosc);
        }

        public void Walcz(Zolnierz Z) // metoda walki pomiedzy obiektami
        {
            Random R = new Random();
            double sA1 = 0, sA2 = 0;
            
            do
            {
                sA1 = (0.1 * Z.Sila) + (0.6 * Z.Zrecznosc) + R.Next(10);
                sA2 = (0.1 * this.Sila) + (0.6 * this.Zrecznosc) + R.Next(10);

                if (sA1 > sA2)
                {
                    Z.Wytrzymalosc -= (sA1 * 0.2);
                    Z.Walcz(Z);
                }
                else
                {
                    Z.Wytrzymalosc -= (sA2 * 0.2);
                    Z.Walcz(Z);
                } break;

                if (Z.Wytrzymalosc == 0)
                {
                    Console.WriteLine("Wygrywa zolnierz 1");
                }
                else
                {
                    Console.WriteLine("Wygrywa zolnierz 2");
                } break;

            } while (this.Wytrzymalosc >= 0 || Z.Wytrzymalosc >= 0);
        }


    }


    class Program //////////////////////////////////////////////// glowna klasa //////////////////////////////////////////////
    {
        static void Main(string[] args)
        {
            Zolnierz z1 = new Zolnierz("Odyn", 20d, 18d, 90d);
            Zolnierz z2 = new Zolnierz("Thor", 18d, 23d, 90d);

            Console.WriteLine("#############################");
            Console.WriteLine("###### BITWA ZOLNIERZY ######");
            Console.WriteLine("#############################");
            Console.WriteLine();

            Console.WriteLine("Przedstawienie zawodników!");
            z1.PrzedstawSie();
            Console.WriteLine("-------------------------");
            z2.PrzedstawSie();
            Console.WriteLine("-------------------------");
            Console.WriteLine();
            
            z1.Walcz(z2);

            Console.ReadLine();
        }
    }
}
Posted

Your code is a little...odd - I'm surprised it compiles, given that there is unreacable code in you Walcz method:
C#
public void Walcz(Zolnierz Z) // metoda walki pomiedzy obiektami
{
...
    do
    {
        ...
        if (...)
        {
            ...
        }
        else
        {
            ...
        } break;
// How does this code ever get executed?
        if (Z.Wytrzymalosc == 0)
        {
            Console.WriteLine("Wygrywa zolnierz 1");
        }
        else
        {
            Console.WriteLine("Wygrywa zolnierz 2");
        } break;

    } while (this.Wytrzymalosc >= 0 || Z.Wytrzymalosc >= 0);
}
The break is unconditional, so the loop will only ever execute once.
Which means that the problem is in the if condition above it somewhere.
Without knowing what your code is supposed to do, that looks odd as well: both sides of teh conditional execute the same code, baring only the variable SA1 or SA2 and they both execute the Z instance of the Walcz method with the same instance as a parameter - so when it starts to execute, this and Z are the same instance...this feels wrong, even without knowing what is going on.

I don't think there is any good reason for this to be recursive anyway, If all you are doing is removing random hit points (as I am guessing you are) then a loop would be a more natural way to do it.
 
Share this answer
 
Do you know what a stack overflow is ? It means your recursive function never unwinds, it calls itself until the computer is out of resources to store the calls. I don't see where your recursive call is, and because your method names are not English, I can't really see what your code does.

OK, the Walcz method creates a loop that keeps calling itself. That's not how recursion should work. What ends up happening is that the method is called over and over again, until the stack is full.
 
Share this answer
 
v2
Ok sorry for Polish names of variables. I will explain you.

Walcz is in english Fight.

And this method contain the parameters of equations needed to calculate the value of vitality of every soldier.

Ok the tell me please whay i should do to write this recurencive function correctly?

C#
public void Fight(Soldier Z) 
        {
            Random R = new Random();
            double sA1 = 0, sA2 = 0;
            
            do
            {
                sA1 = (0.1 * Z.Strength) + (0.6 * Z.Aguillity) + R.Next(10);
                sA2 = (0.1 * this.Strength) + (0.6 * this.Aquillity) + R.Next(10);
 
                if (sA1 > sA2)
                {
                    Z.Vitallity -= (sA1 * 0.2);
                    Z.Fight(Z);
                }
                else
                {
                    Z.Vitallity -= (sA2 * 0.2);
                    Z.Fight(Z);
                } break;
 
                if (Z.Vitallity == 0)
                {
                    Console.WriteLine("The winner is soldier 1");
                }
                else
                {
                    Console.WriteLine("The winner is soldier 2");
                } break;
 
            } while (this.Vitallity >= 0 || Z.Vitallity >= 0);
        }


This method should work as long that the vitallity (wytrzymałość) will be >= 0 and after this stop working and show the winner of fight.
 
Share this answer
 
v3
Yes you have true, i removed this breake; but it dont solve the problem with stack.
Its hard to explain in English but i try :

C#
sA1 = (0.1 * Z.Strength) + (0.6 * Z.Aguillity) + R.Next(10);
sA2 = (0.1 * this.Strength) + (0.6 * this.Aquillity) + R.Next(10);


This is the equations which calculate the damage doned by every Soldier


C#
if (sA1 > sA2)
                {
                    Z.Vitallity -= (sA1 * 0.2);
                    Z.Fight(Z);
                }
                else
                {
                    Z.Vitallity -= (sA2 * 0.2);
                    Z.Fight(Z);
                } 


This is only checking the status of every soldier vitallity

So how i should solve this problem with diffrent way?
 
Share this answer
 
Comments
Nelek 13-Nov-12 19:03pm    
Please don't post solutions to add information or to answer a user.
- To add information to your message, you can use the widget "Improve question" at the bottom of your text.
- To answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you).
Ok i am at good way:

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

namespace Gra_Zolnierze
{
    class Zolnierz ////////////////////////////////////////////////// klasa Zolnierz /////////////////////////////////
    {
        public string Ksywa { get; set; }
        public double Sila { get; set; }
        public double Zrecznosc { get; set; }
        public double Wytrzymalosc { get; set; }

        public Zolnierz(string Ksywa, double Sila, double Zrecznosc, double Wytrzymalosc) // konstruktor
        {
            this.Ksywa = Ksywa;
            this.Sila = Sila;
            this.Zrecznosc = Zrecznosc;
            this.Wytrzymalosc = Wytrzymalosc;

        }

        public void PrzedstawSie() // metoda wyswietlajaca informacje o obiektach
        {
            Console.WriteLine("Jestem {0} ", this.Ksywa);
            Console.WriteLine("Moja sila to {0} ", this.Sila);
            Console.WriteLine("Moja zrecznosc to {0} ", this.Zrecznosc);
            Console.WriteLine("Moja wytrzymalosc to {0} ", this.Wytrzymalosc);
        }

        public void Walcz(Zolnierz Z) // metoda walki pomiedzy obiektami
        {
            Random R = new Random();
            double sA1 = 0, sA2 = 0;
            

            do
            {
                bool uderzyl_Thor = false;
                bool uderzyl_Odyn = true;
                sA1 = (0.1 * Z.Sila) + (0.6 * Z.Zrecznosc) + R.Next(10);
                sA2 = (0.1 * this.Sila) + (0.6 * this.Zrecznosc) + R.Next(10);

                if (sA1 > sA2)
                {
                    Z.Wytrzymalosc -= (sA1 * 0.2);
                    
                }
                else
                {
                    Z.Wytrzymalosc -= (sA2 * 0.2);
                    
                }

                if (uderzyl_Odyn)
                {
                    Console.WriteLine("Odyn zadaje obrazenia w wysokosci "+sA1+" odbierajac "+Z.Wytrzymalosc+" wytrzymalosci");
                    uderzyl_Odyn = false;
                }
                if(uderzyl_Thor)
                {
                    Console.WriteLine("Thor zadaje obrazenia w wysokosci " + sA2 + " odbierajac " + Z.Wytrzymalosc + " wytrzymalosci");
                    uderzyl_Thor = false;
                }

               
            } while (this.Wytrzymalosc > 0 && Z.Wytrzymalosc > 0);

             if (this.Wytrzymalosc <= 0)
                {
                    Console.WriteLine("Wygrywa zolnierz 1");
                }
                else
                {
                    Console.WriteLine("Wygrywa zolnierz 2");
                } 
        }


    }


    class Program //////////////////////////////////////////////// glowna klasa //////////////////////////////////////////////
    {
        static void Main(string[] args)
        {
            Zolnierz z1 = new Zolnierz("Odyn", 20d, 18d, 90d);
            Zolnierz z2 = new Zolnierz("Thor", 18d, 23d, 90d);

            Console.WriteLine("#############################");
            Console.WriteLine("###### BITWA ZOLNIERZY ######");
            Console.WriteLine("#############################");
            Console.WriteLine();

            Console.WriteLine("Przedstawienie zawodników!");
            z1.PrzedstawSie();
            Console.WriteLine("-------------------------");
            z2.PrzedstawSie();
            Console.WriteLine("-------------------------");
            Console.WriteLine();
            
            z1.Walcz(z2);

            Console.ReadLine();
        }
    }
}
 
Share this answer
 
Comments
Nelek 13-Nov-12 19:03pm    
Please don't post solutions to add information or to answer a user.
- To add information to your message, you can use the widget "Improve question" at the bottom of your text.
- To answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you).

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