结束 while 循环

发布于 2024-11-28 03:58:16 字数 6223 浏览 0 评论 0原文

我编写了一个小型 Shotgun 应用程序,但是,当 AI(称为 Genius)、用户或两者都被射击时需要停止游戏的代码部分,我无法开始工作。我做错了什么?我觉得我通过添加大量具有不同布尔值的返回值使我的代码变得过于复杂,其中一些被传递,另一些则没有。

在现在的测试中,无论用户移动(称为字符串移动)等于“f”,循环都会结束。在任何其他情况下,我都无法结束循环。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {


        public static void Main(string[] args)
        {
            Start("r");
        }

        public static string Start(string move)
        {

            Console.Write("Welcome to the Shotgun App\nEnter s for single player and m for multiplayer: ");
            string gameType = Console.ReadLine();

            if (gameType == "s")
            {

                Console.Write("Single Player Controls:\n r = reload\n s = shield\n f = fire\nYou start with 1 ammo\nReady to play?");
                Console.ReadLine();

                int ammo = 1;
                int geniusAmmo = 1;
                string geniusMove = "";
                bool done = false;
                while (!done)
                {
                    Console.Write("\nEnter your move: ");
                    move = Console.ReadLine();


                    switch (move)
                    {
                        case "r":
                            Console.Write("\nYou have reloaded, press enter for Genius\n");

                            ammo++;
                            Console.Write("Your ammo is " + ammo);

                            Console.ReadLine();
                            Genius(geniusMove, move, geniusAmmo, done);


                            break;
                        case "s":
                            Console.Write("\nYou have shielded, press enter for Genius\n");

                            Console.Write("Your ammo is " + ammo);

                            Console.ReadLine();
                            Genius(geniusMove, move, geniusAmmo, done);


                            break;
                        case "f":
                            if (ammo != 0)
                            {
                                Console.Write("\nYou have fired, press enter for Genius\n");

                                ammo--;
                                Console.Write("Your ammo is " + ammo);

                                Console.ReadLine();

                                Genius(geniusMove, move, geniusAmmo, done);

                            }
                            else
                            {
                                Console.Write("You don't have enough ammo, try again");
                                done = false;
                            }
                            break;
                        default:
                            Console.Write("\nInvalid move, try again\n");
                            done = false;
                            break;
                    }
                    done = EndLoop(move, geniusMove, done);
                    Console.ReadLine();

                }
                return move;
            }
            else
            {
                return move;
            }
        }

        public static string Genius(string geniusMove, string move, int geniusAmmo, bool done)
        {
            Random RandomNumber = new Random();
            int x = RandomNumber.Next(0,3);
            if (x == 0)
            {
                geniusMove = "f";
                geniusAmmo--;
                Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n");
                TestMoves(move, geniusMove);
            }
            else if (x == 1)
            {
                geniusMove = "r";
                geniusAmmo++;
                Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n");
                TestMoves(move, geniusMove);
            }
            else if (x == 2)
            {
                geniusMove = "s";  
                Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n");
                TestMoves(move, geniusMove);
            }

            return geniusMove;

        }


        public static void TestMoves(string move, string geniusMove)
        {
            bool done = false;
            if (move == "s" && geniusMove == "f")
            {
                Console.Write("Nice shield, no one has died yet");


            }
            else if (move == "f" && geniusMove == "f")
            {
                Console.Write("You both died!  Good game!");


            }
            else if (move == "r" && geniusMove == "f")
            {
                Console.Write("No shield!?  You died!  Good game!");


            }
            else if (move == "f" && geniusMove == "s")
            {
                Console.Write("Genius is too good, no one has died yet");


            }
            else if (move == "f" && geniusMove != "s")
            {
                Console.Write("Genius let his guard down!  Good game!");


            }
            else if (move != "f" && geniusMove != "f")
            {
                Console.Write("Keep playing it safe.");


            }
            else
            {


            }

        }

        static bool EndLoop(string move, string geniusMove, bool done)
        {
            done = false;
            if (move == "s" && geniusMove == "f")
            {
                return false;
            }
            else if (move == "f" && geniusMove == "f")
            {
                return true;
            }
            else if (move != "s" && geniusMove == "f")
            {
                return true;
            }
            else if (move == "f" && geniusMove == "s")
            {
                return false;
            }
            else if (move == "f" && geniusMove != "s")
            {
                return true;
            }
            else if (move != "f" && geniusMove != "f")
            {
                return false;
            }
            else
            {
                return done;
            }
        }
    }
}

I wrote a small Shotgun app, however, the section of code that needs to stop the game when either the AI (called Genius), the user, or both, are shot, I can't get to work. What am I doing wrong? I feel like I over-complicated my code a ton by adding lots of returns with different booleans, in which some are being passed and others aren't.

In testing it right now, the loop ends no matter what if the user move (called string move) equals "f". In any other scenario, I can not get the loop to end.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {


        public static void Main(string[] args)
        {
            Start("r");
        }

        public static string Start(string move)
        {

            Console.Write("Welcome to the Shotgun App\nEnter s for single player and m for multiplayer: ");
            string gameType = Console.ReadLine();

            if (gameType == "s")
            {

                Console.Write("Single Player Controls:\n r = reload\n s = shield\n f = fire\nYou start with 1 ammo\nReady to play?");
                Console.ReadLine();

                int ammo = 1;
                int geniusAmmo = 1;
                string geniusMove = "";
                bool done = false;
                while (!done)
                {
                    Console.Write("\nEnter your move: ");
                    move = Console.ReadLine();


                    switch (move)
                    {
                        case "r":
                            Console.Write("\nYou have reloaded, press enter for Genius\n");

                            ammo++;
                            Console.Write("Your ammo is " + ammo);

                            Console.ReadLine();
                            Genius(geniusMove, move, geniusAmmo, done);


                            break;
                        case "s":
                            Console.Write("\nYou have shielded, press enter for Genius\n");

                            Console.Write("Your ammo is " + ammo);

                            Console.ReadLine();
                            Genius(geniusMove, move, geniusAmmo, done);


                            break;
                        case "f":
                            if (ammo != 0)
                            {
                                Console.Write("\nYou have fired, press enter for Genius\n");

                                ammo--;
                                Console.Write("Your ammo is " + ammo);

                                Console.ReadLine();

                                Genius(geniusMove, move, geniusAmmo, done);

                            }
                            else
                            {
                                Console.Write("You don't have enough ammo, try again");
                                done = false;
                            }
                            break;
                        default:
                            Console.Write("\nInvalid move, try again\n");
                            done = false;
                            break;
                    }
                    done = EndLoop(move, geniusMove, done);
                    Console.ReadLine();

                }
                return move;
            }
            else
            {
                return move;
            }
        }

        public static string Genius(string geniusMove, string move, int geniusAmmo, bool done)
        {
            Random RandomNumber = new Random();
            int x = RandomNumber.Next(0,3);
            if (x == 0)
            {
                geniusMove = "f";
                geniusAmmo--;
                Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n");
                TestMoves(move, geniusMove);
            }
            else if (x == 1)
            {
                geniusMove = "r";
                geniusAmmo++;
                Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n");
                TestMoves(move, geniusMove);
            }
            else if (x == 2)
            {
                geniusMove = "s";  
                Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n");
                TestMoves(move, geniusMove);
            }

            return geniusMove;

        }


        public static void TestMoves(string move, string geniusMove)
        {
            bool done = false;
            if (move == "s" && geniusMove == "f")
            {
                Console.Write("Nice shield, no one has died yet");


            }
            else if (move == "f" && geniusMove == "f")
            {
                Console.Write("You both died!  Good game!");


            }
            else if (move == "r" && geniusMove == "f")
            {
                Console.Write("No shield!?  You died!  Good game!");


            }
            else if (move == "f" && geniusMove == "s")
            {
                Console.Write("Genius is too good, no one has died yet");


            }
            else if (move == "f" && geniusMove != "s")
            {
                Console.Write("Genius let his guard down!  Good game!");


            }
            else if (move != "f" && geniusMove != "f")
            {
                Console.Write("Keep playing it safe.");


            }
            else
            {


            }

        }

        static bool EndLoop(string move, string geniusMove, bool done)
        {
            done = false;
            if (move == "s" && geniusMove == "f")
            {
                return false;
            }
            else if (move == "f" && geniusMove == "f")
            {
                return true;
            }
            else if (move != "s" && geniusMove == "f")
            {
                return true;
            }
            else if (move == "f" && geniusMove == "s")
            {
                return false;
            }
            else if (move == "f" && geniusMove != "s")
            {
                return true;
            }
            else if (move != "f" && geniusMove != "f")
            {
                return false;
            }
            else
            {
                return done;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撕心裂肺的伤痛 2024-12-05 03:58:17

您在几个不同的地方设置 done ,包括 switch 情况下的某些执行分支以及调用 EndLoop 时。 EndLoop 中的作业将覆盖之前的任何作业,因此请让您设置的“One Place”完成

在 TestMoves 中使用 EndLoop 设置 done 不会产生任何效果,因为在调用 EndLoop 后立即返回一个硬编码值。

我建议您在调试器中完成 EndLoop。如果它可以让您更轻松地可视化正在发生的情况,您可以考虑将 EndLoop 的输入参数以及您最终选择的 if 条件打印到控制台。

You are setting done in a few different places, both in some execution branches in the switch cases, and when calling EndLoop. The assignment from EndLoop will overwrite any previous assignment, so make that The One Place You Set done.

Setting done with EndLoop in TestMoves does not have any effect since you immediately return a hard-coded value right after you call EndLoop.

I suggest you follow through EndLoop in a debugger. If it makes it easier for you to visualize what's happening, you might consider instead printing to the console the input parameters for EndLoop, and which if condition you end up selecting.

清风不识月 2024-12-05 03:58:17

GeniusMove 将始终为空字符串,因为您没有存储对 Genius 方法的调用结果。

将结果存储在 GeniusMove 变量中或通过引用传递

public static string Genius(ref string geniusMove, string move, int geniusAmmo, bool done)

geniusMove = Genius(geniusMove, move, geniusAmmo, done);

geniusMove will always be an empty string since you are not storing the result of the call to the Genius method.

Either store the result in the geniusMove variable or pass it in by reference

public static string Genius(ref string geniusMove, string move, int geniusAmmo, bool done)

or

geniusMove = Genius(geniusMove, move, geniusAmmo, done);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文