循环结束

时间:2011-08-05 01:28:15

标签: c# while-loop

我写了一个小的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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您正在几个不同的地方设置done,在交换机案例中的某些执行分支中,以及在调用EndLoop时。 EndLoop的作业将覆盖之前的任何作业,因此请设置您设置的一个地点done

在TestMoves中使用EndLoop设置done没有任何效果,因为您在调用EndLoop后立即返回硬编码值。

我建议你在调试器中关注EndLoop。如果它使您更容易可视化正在发生的事情,您可以考虑在控制台上打印EndLoop的输入参数,以及最终选择的条件。

答案 1 :(得分:1)

geniusMove将始终为空字符串,因为您没有将调用结果存储到Genius方法中。

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

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

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