从其他方法获取bool值以结束while循环

本文关键字:结束 while 循环 bool 其他 方法 获取 | 更新日期: 2023-09-27 18:04:29

我很难从EndLoop()获得bool完成值并将其移动到Start()方法以结束我的while循环。我做错了什么?

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;
                    }
                    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");
                EndLoop(move, geniusMove, done);

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

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

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

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

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

            }
            else
            {
                EndLoop(move, geniusMove, done);
            }
        }
        public static bool EndLoop(string move, string geniusMove, bool done)
        {
            if (move == "s" && geniusMove == "f")
            {
                done = false;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "s" && geniusMove == "r")
            {
                done = false;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "s" && geniusMove == "s")
            {
                done = false;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "r" && geniusMove == "f")
            {
                done = true;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "r" && geniusMove == "r")
            {
                done = false;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "r" && geniusMove == "s")
            {
                done = false;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "f" && geniusMove == "f")
            {
                done = true;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "f" && geniusMove == "r")
            {
                done = true;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else if (move == "f" && geniusMove == "s")
            {
                done = false;
                Console.Write(move + geniusMove + done);
                return done;
            }
            else
            {
                done = false;
                Console.Write(move + geniusMove + done);
                return done;
            }
        }
    }
}

从其他方法获取bool值以结束while循环

我知道我不能直接回答你的问题,但我想我会试着给你一个执行游戏的替代方法,使用对象和LINQ,并尝试让代码更加模块化。真的是为了兴趣。

在这里:

void Main()
{
    var moves = new Dictionary<string, Move>()
    {
        { "r", new Move(1, "Reloaded") },
        { "s", new Move(0, "Shielded") },
        { "f", new Move(-1, "Fired") },
    };
    var messages = new Dictionary<string, string>()
    {
        { "Shielded-Fired", "Nice shield, no one has died yet" },
        { "Fired-Fired", "You both died!  Good game!" },
        { "Reloaded-Fired", "No shield!?  You died!  Good game!" },
        { "Shielded-Shielded", "Keep playing it safe." },
        { "Fired-Shielded", "Genius is too good, no one has died yet" },
        { "Reloaded-Shielded", "No-one fired" },
        { "Shielded-Reloaded", "No-one fired" },
        { "Fired-Reloaded", "Genius let his guard down!  Good game!" },
        { "Reloaded-Reloaded", "No-one fired" },
    };
    var isDone = new Dictionary<string, bool>()
    {
        { "Shielded-Fired", false },
        { "Fired-Fired", true },
        { "Reloaded-Fired", true },
        { "Shielded-Shielded", false },
        { "Fired-Shielded", false },
        { "Reloaded-Shielded", false },
        { "Shielded-Reloaded", false },
        { "Fired-Reloaded", true },
        { "Reloaded-Reloaded", false },
    };
    var rnd = new Random();
    var choices = new [] { "r", "s", "f", };
    var human = new Player("You", () => Console.ReadLine(), m => Console.WriteLine(m));
    var genius = new Player("Genius", () => choices[rnd.Next(0, 3)], m => { });
    var allMoves = GetPlayerMoves(moves, human).Zip(GetPlayerMoves(moves, genius), (h, g) =>
    {
        human.Play(h);
        genius.Play(g);
        var hg = String.Format("{0}-{1}", h.Name, g.Name);
        Console.WriteLine(messages[hg]);
        return isDone[hg];
    });
    foreach (var done in allMoves)
        if (done)
            break;
}
private static IEnumerable<Move> GetPlayerMoves(Dictionary<string, Move> moves, Player player)
{
    while (true)
    {
        player.WriteMessage("'nEnter your move: ");
        var choice = player.GetChoice();
        if (moves.ContainsKey(choice))
        {
            var move = moves[choice];
            if (move.Play(player.Ammo) < 0)
            {
                player.WriteMessage("'nYou don't have enough ammo, try again.'n");
            }
            else
            {
                yield return move;
            }
        }
        else
        {
            player.WriteMessage("'nInvalid move, try again.'n");
        }
    }
}
public class Player
{
    public Player(string name, Func<string> getChoice, Action<string> writeMessage)
    {
        this.Name = name;
        this.Ammo = 1;
        _getChoice = getChoice;
        _writeMessage = writeMessage;
    }
    private readonly Func<string> _getChoice;
    private readonly Action<string> _writeMessage;
    public string GetChoice()
    {
        return _getChoice();
    }
    public void WriteMessage(string message)
    {
        _writeMessage(message);
    }
    public string Name { get; private set; }
    public int Ammo { get; private set; }
    public void Play(Move move)
    {
        this.Ammo = move.Play(this.Ammo);
        Console.Write(String.Format("{0} {1} (ammo is {2}.)'n", this.Name, move.Name.ToLowerInvariant(), this.Ammo));
    }
}
public class Move
{
    public Move(int ammoChange, string name)
    {
        this.AmmoChange = ammoChange;
        this.Name = name;
    }
    public string Name { get; private set; }
    private int AmmoChange { get; set; }
    public int Play(int ammo)
    {
        return ammo + AmmoChange;
    }
}

在类的作用域中声明static bool done = false;,并使用相同的方法而不是通过不同的方法传递state。如果您将状态传递给调用函数Start,则可以这样做。

您有两个名为done的局部变量。它们是独立的变量,所以Start中的done和TestMoves中的done不是同一个变量。这两个参数都和EndLoop中的参数不一样。

最佳实践是实际创建一个Program实例,并使用像这样的公共变量作为类成员。

如果你想让所有的东西都是静态的,那么就用一个静态类变量来代替所有的局部变量和参数。

这段代码还有很多其他的错误——例如,你改变了其他的局部变量和/或参数的值,但这种改变不会影响调用方法。if/else语句的每个分支中都有大量重复的代码,这些代码可以移到最后的单个调用中。

你需要将bool设置为静态和全局,这样当你调用EndLoop时,循环中的done值也会被设置。

class Program
{
    static bool done = false;
    // rest of code
}

你也应该只检查你想把done设置为true的条件,而忽略其余的(因为把它从false更改为false没有任何作用)。