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

时间:2011-08-05 03:47:27

标签: c# while-loop boolean

我很难从EndLoop()获取bool done值并将其移动到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;
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

我知道我没有直接回答你的问题,但我想我会尝试为你提供另一种实现游戏的方法,使用对象和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;
    }
}

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您有两个名为done的局部变量。它们是单独的变量,因此Start中的完成与TestMoves中的完成不同。它们与EndLoop中的参数都不相同。

最佳做法是实际创建一个Program实例,并使用像class成员那样的常见变量。

如果你想要一切都是静态的,有一些压倒一切的理由,那么只需要一个名为done的静态类变量,而不是所有的局部变量和参数。

此代码还有很多其他问题 - 例如还有其他局部变量和/或参数可以更改其值,但该更改不会影响调用方法。在if / else if / else的每个分支中都有很多重复的代码,可以在最后移出一个调用。

答案 3 :(得分:0)

当你调用EndLoop时,你需要让bool完成静态和全局的方式设置循环中的done值。

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

您还应该只检查要将done设置为true的条件并忽略其余条件(因为将其从false更改为false不会执行任何操作)。