简体   繁体   English

漫游者和标本C#

[英]Rover And specimen C#

i am making a simple game of rover, in which i am having a simple problem which i can't figured out 我正在做一个简单的漫游车游戏,其中有一个我不知道的简单问题

This is my Main Function which is making One rover object in which rover can handle multiple device, there is one device which extract the specimen 这是我的主要功能,它使一个流动站对象能够在其中流动站处理多个设备,有一个设备可以提取标本

      Rover rov = new Rover();
        string input;
        //Device d = new Device();
        //Specimen spec = new Specimen();
        Console.WriteLine("Welcome to Planetary Rover");
        Console.WriteLine("Enter y to start the game or n to exit");
        string selection = Console.ReadLine();
        if (selection == "y")
        {
            Console.WriteLine("Enter the rover Default Location X: ");
            rov.X = 

            do
            {

                Console.WriteLine("Write the device nname to operate ");
                input = Console.ReadLine();
                rov.Handle(input);
                //d.Operate();
            } while (input != "end");
        }
        if (selection == "n")
        { 
            Console.Clear();
        }
        else
        {
            Console.WriteLine("Wrong Input!");
        }

THe drill is the device which extraxt the specimen whenever it is operated, it only extract if the specimen x and y is equals to rover x and y I have done these thing but in my drill class there is operate functionwhich is doing the above thing, but problems occurs whenever drill is operating it is again making a new rover, so the rover x and y get null that time, so any can give me a potential fixes for that how can i use the existing rover in the drill function 演习是一种在每次操作时都会引出标本的设备,只有当标本x和y等于流动站x和y时,它才会提取。我已经做了这些事情,但是在我的演习课上有执行上述操作的操作功能,但是只要钻头在运行,它就会再次制造新的流动站,因此会出现问题,因此流动站x和y在那段时间变为空,因此任何方法都可以为我提供潜在的解决方法,让我可以在钻取功能中使用现有流动站

    public override void Operate(string ids)
    {
        Rover rov = new Rover();




        if (specim.X == rov.X && specim.Y == rov.Y)
        {
            _wearfactor += 5;
            Console.WriteLine("specimen extracted and wearfaction of drill is incresed by 5%");
            Console.WriteLine(_wearfactor);
            _spec. = 0;

        }

        if(specim.X != rov.X && specim.Y != rov.Y)
        {
            _wearfactor += 10;
            Console.WriteLine("wear factor increased by 10%");
            Console.WriteLine(_wearfactor);
        }
        if (_wearfactor == 100)
        {
            Console.WriteLine("Drill Destroyed");
            Console.WriteLine("syart your program again");
            Console.WriteLine("You can not drill specimen now");
            this.Name = "";

        }


    }

You could change your Operate method's signature to: 您可以将Operate方法的签名更改为:

    public override void Operate(Rover rov, string ids)

then when you create Rover rov = new Rover(); 然后当您创建Rover rov = new Rover(); you can pass it through for Operate to use. 您可以将其传递给Operate使用。

    Operate(rov, "ids");

Another option would be to make a public Rover object and directly reference it through Operate : 另一个选择是制作一个公共Rover对象并通过Operate直接引用它:

// assuming your main class is MainFunction
public class MainFunction()
{
    public Rover rov { get; private set; }

    public static void Main(string[] args)
    {
        // Establish the reusable rover
        rov = new Rover();
        // ...
    }

}

Then in Operate , change all rov to MainFunction.rov 然后在Operate ,更改所有rovMainFunction.rov

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM