使用StackOverflow轻松控制台目标游戏问题 [英] Simply console objective game PROBLEM with StackOverflow

查看:68
本文介绍了使用StackOverflow轻松控制台目标游戏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:)

这是我的第一条帖子,然后为每一个错误感到抱歉.

我正在使用Visual Studio 2012和C#编写简单的控制台目标游戏应用程序,但在StackOverflow异常方面遇到了一些麻烦.

在这个游戏中,我们有2个类别:
级士兵和班级计划(主班)

而且我声明了2个具有适当性质的对象.

其z1和z2对象具有以下属性:Sila(强度),Zrecznosc(敏锐度),Wytrzymalosc(类似于击中点)和ksywa(意味着昵称)

好的,所以这2个对象使用Walcz方法一起战斗,其中包含计算统计信息的方程式.

此方法应是递归的,并且她仍应修改生命值的价值.

我的代码有什么问题?

请帮助我:)

这是我的代码:

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用使用System.Threading.Tasks;

命名空间 Gra_Zolnierze
{
     class  Zolnierz /// //////////////////////////////////klasa Zolnierz//////////////////////////////////
 {
        公共 字符串 Ksywa { get ; 设置; }
        公共  double  Sila { get ; 设置; }
        公共  double  Zrecznosc { get ; 设置; }
        公共  double  Wytrzymalosc { get ; 设置; }

        公共 Zolnierz(字符串 Ksywa, double  Sila , double  Zrecznosc, double  Wytrzymalosc)//  konstruktor 
        {
            .Ksywa= Ksywa;
            .Sila= Sila;
            .Zrecznosc= Zrecznosc;
            .Wytrzymalosc= Wytrzymalosc;

        }

        公共 无效 PrzedstawSie()//  metoda wyswietlajaca notifyacje o obiektach 
        {
            Console.WriteLine("  this  .Ksywa);
            Console.WriteLine("  this  .Sila);
            Console.WriteLine("  this  .Zrecznosc);
            Console.WriteLine("  this  .Wytrzymalosc);
        }

        公共 无效 Walcz(Zolnierz Z)//  metoda walki pomiedzy obiektami 
        {
            随机R =  Random();
             double  sA1 =  0 ,sA2 =  0  ;
            
            
            {
                sA1 =( 0 . 1  * Z.Sila)+( 0 . 6  * Z.Zrecznosc)+ R.Next( 10 );
                sA2 =( 0 . 1  *  .Sila)+( 0 . 6  *  span> .Zrecznosc)+ R.Next( 10 );

                如果(sA1 >  sA2)
                {
                    Z.Wytrzymalosc-=(sA1 *  0 . 2 );
                    Z.Walcz(Z);
                }
                其他
                {
                    Z.Wytrzymalosc-=(sA2 *  0 . 2 );
                    Z.Walcz(Z);
                }  break ;

                如果(Z.Wytrzymalosc ==  0 )
                {
                    Console.WriteLine(" );
                }
                其他
                {
                    Console.WriteLine(" );
                }  break ;

            } 同时(.Wytrzymalosc>  =  0  || Z.Wytrzymalosc >  =  0 );
        }


    }


     class 程序/// /////////////////////////////////////////////glowna klasa///////////////////////////////////////////////
 {
        静态 无效 Main(字符串 []参数)
        {
            Zolnierz z1 =  Zolnierz(" ,20d,18d,90d);
            Zolnierz z2 =  Zolnierz(" ,18d,23d,90d);

            Console.WriteLine(" );
            Console.WriteLine(" );
            Console.WriteLine(" );
            Console.WriteLine();

            Console.WriteLine(" );
            z1.PrzedstawSie();
            Console.WriteLine(" );
            z2.PrzedstawSie();
            Console.WriteLine(" );
            Console.WriteLine();
            
            z1.Walcz(z2);

            Console.ReadLine();
        }
    }
} 

解决方案

您的代码有点...奇怪-由于您的Walcz方法中存在无法接收的代码,因此我感到惊讶:

 公共  void  Walcz(Zolnierz Z) metoda walki pomiedzy obiektami 
{
...
    
    {
        ...
        如果(...)
        {
            ...
        }
        其他
        {
            ...
        }  break ;
// 如何执行此代码?
        如果(Z.Wytrzymalosc ==  0 )
        {
            Console.WriteLine(" );
        }
        其他
        {
            Console.WriteLine(" );
        }  break ;

    } 同时(.Wytrzymalosc>  =  0  || Z.Wytrzymalosc >  =  0 );
} 

break是无条件的,因此循环将只执行一次.
这意味着问题出在其上方某处的if条件中.
在不知道您的代码应该做什么的情况下,这看起来也很奇怪:有条件的双方都执行相同的代码,仅禁止变量SA1或SA2,并且它们都使用与实例相同的实例执行Walcz方法的Z实例.参数-因此,当它开始执行时,this和Z是相同的实例...这感觉很不对劲,即使不知道发生了什么.

我认为无论如何这都没有递归的充分理由,如果您要做的只是删除随机的命中点(正如我猜的那样),那么循环将是一种更自然的方法. /blockquote>

您知道什么是堆栈溢出吗?这意味着您的递归函数永远不会消失,它会自行调用,直到计算机没有足够的资源来存储调用.我看不到您的递归调用在哪里,并且因为您的方法名不是英语,所以我真的看不到您的代码做什么.

好的,Walcz方法创建了一个循环,不断调用自身.这不是递归的工作方式.最终发生的是,该方法一遍又一遍地调用,直到堆栈已满.


对波兰的变量名表示抱歉.我会解释给你的.

Walcz是英文Fight.

而且此方法包含计算每个士兵的生命值所需的方程参数.

好吧,请告诉我,为什么我应该正确编写此递归函数?

 公共  void 战斗(士兵Z)
        {
            随机R =  Random();
             double  sA1 =  0 ,sA2 =  0  ;
            
            
            {
                sA1 =( 0 . 1  * Z.Strength)+( 0 . 6  * Z.Aguillity)+ R.Next( 10 );
                sA2 =( 0 . 1  *  .Strength)+( 0 . 6  *  .Aquillity)+ R.Next( 10 );
 
                如果(sA1 >  sA2)
                {
                    Z.Vitallity-=(sA1 *  0 . 2 );
                    Z.Fight(Z);
                }
                其他
                {
                    Z.Vitallity-=(sA2 *  0 . 2 );
                    Z.Fight(Z);
                }  break ;
 
                如果(Z.Vitallity ==  0 )
                {
                    Console.WriteLine(" );
                }
                其他
                {
                    Console.WriteLine(" );
                }  break ;
 
            } 同时(.活力>  =  0  || Z.Vallallity >  =  0 );
        } 



只要生命力(wytrzymałość)为> = 0,并且此方法停止工作并显示出战斗获胜者,此方法就应该起作用.


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:

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();
        }
    }
}

解决方案

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.


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.


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.


这篇关于使用StackOverflow轻松控制台目标游戏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆