Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
HelloSmile | :)
 
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 Smile | :)
 
This is my code:
 
 
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 13-Nov-12 5:37am
Odyn262
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Your code is a little...odd - I'm surprised it compiles, given that there is unreacable code in you Walcz method:
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
 
 
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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 :
 
 
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
 

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?
  Permalink  
Comments
Nelek at 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).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Ok i am at good way:
 
 
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();
        }
    }
}
 

  Permalink  
Comments
Nelek at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,935
1 OriginalGriff 6,987
2 CPallini 5,845
3 George Jonsson 4,015
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100