首页 > 解决方案 > Only state once

问题描述

I am making an RPG sort of text adventure in Visual Studio C# console. I am trying to create a piece of code that will detect your location. I want it to then respond in the necessary format. I know you can probably do this more effectively with arrays but I'm happy with how it is now. What I want to do is stop it from constantly repeating the message "It worked.". If it only states it once then it has worked. I have tried multiple methods. Please offer a solution to my issues.
Code Here or Underneath

using System;

namespace RPG
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            //Done(); will check if Cords are at end of game


            //variables
            string response = "";
            string name = "";
            bool hastyped = false;

            //Coordinates
            int xCord = 0; int yCord = 0;


            void Done()
            {
                if (yCord == 5 && xCord == 7)
                {
                    Console.WriteLine("Thanks for playing! You did it!");
                }
            }

            void Move()
            {
                if (hastyped == true)
                {
                    if (response == "E")
                    {
                        xCord = xCord + 1;
                    }
                    else if (response == "W")
                    {
                        xCord = xCord - 1;
                    }
                    else if (response == "N")
                    {
                        xCord = xCord + 1;
                    }
                    else if (response == "S")
                    {
                        xCord = xCord - 1;
                    }
                    else
                    {
                        Console.WriteLine("That movement is invalid.");
                    }

                    hastyped = false;
                    response = "";
                    Done();
                }
            }


            //FANCY START LOGO
            Console.WriteLine(@"
____________ _____
| ___ \ ___ \  __ \
| |_/ / |_/ / |  \/
|    /|  __/| | __ 
| |\ \| |   | |_\ \
\_| \_\_|    \____/
"
            );
            System.Threading.Thread.Sleep(2500);

            while (response != "Y")
            {
                Console.WriteLine("Please answer with Y for yes and N for no. Please try to answer all questions as simply as possible.\nPlease type N for north, E for east, S for south and W for west.");
                Console.WriteLine("Ok?");
                response = Console.ReadLine();

                if (response == "Y")
                {
                    Console.WriteLine("Well lets start:");
                }
                else
                {
                    Console.WriteLine("Please try again.");

                }
            }
            //Instructions



            Console.WriteLine("LOADING...");

            Console.WriteLine("7%");
            System.Threading.Thread.Sleep(200);
            Console.WriteLine("26 %");
            System.Threading.Thread.Sleep(100);
            Console.WriteLine("48%");
            System.Threading.Thread.Sleep(200);
            Console.WriteLine("76%");
            System.Threading.Thread.Sleep(600);
            Console.WriteLine("87%");
            System.Threading.Thread.Sleep(400);
            Console.WriteLine("99%");
            System.Threading.Thread.Sleep(20);
            Console.WriteLine("100%");
            //Loading...

            Console.WriteLine("Hey buddy, you took that hit pretty hard. Are you alright?");
            Console.ReadLine();
            Console.WriteLine("Well we need to get you to a hospital.\nSorry, what was your name?");
            name = Console.ReadLine();
            Console.WriteLine("Well " + name + ", I wish we were meeting under better circumstances.");
            Console.WriteLine("Anyway, I'm Dave. Off to the East is a path that looks pretty safe, the only other direction is back to the west where I just found you. So we won't go there.");
            Console.WriteLine("YOU MOVED EAST!!!");
            xCord = xCord + 1;
            //Intro


            //GAMEPLAY
            while (true)
            {
                //1,0
                if (xCord == 1 && yCord == 0)
                {
                    Console.WriteLine("Hit E then enter for testing.");
                    response = Console.ReadLine();
                    hastyped = !string.IsNullOrWhiteSpace(response);
                    if (hastyped == true)
                    {
                        Move();
                    }
                }

                //2,0
                if (xCord == 2 && yCord == 0)
                {
                    Console.WriteLine("It worked.");
                }

                //3,0
                if (xCord == 3 && yCord == 0)
                {

                }
                //-1,0
                if (xCord == -1 && yCord == 0)
                {

                }

                //-2,0
                if (xCord == -2 && yCord == 0)
                {

                }

                //-3,0
                if (xCord == -3 && yCord == 0)
                {

                }

                //1,1
                if (xCord == 1 && yCord == 1)
                {

                }

                //2,1
                if (xCord == 2 && yCord == 1)
                {

                }

                //3,1
                if (xCord == 3 && yCord == 1)
                {

                }

                //-1,1
                if (xCord == -1 && yCord == 1)
                {

                }

                //-2,1
                if (xCord == -2 && yCord == 1)
                {

                }

                //-3,1
                if (xCord == -3 && yCord == 1)
                {

                }

                //1,2
                if (xCord == 1 && yCord == 2)
                {

                }

                //2,2
                if (xCord == 2 && yCord == 2)
                {

                }

                //3,2
                if (xCord == 3 && yCord == 2)
                {

                }

                //-1,2
                if (xCord == -1 && yCord == 2)
                {

                }

                //-2,2
                if (xCord == -2 && yCord == 2)
                {

                }

                //-3,2
                if (xCord == -3 && yCord == 2)
                {

                }

                //1,3
                if (xCord == 1 && yCord == 3)
                {

                }

                //2,3
                if (xCord == 2 && yCord == 3)
                {

                }

                //3,3
                if (xCord == 3 && yCord == 3)
                {

                }

                //-1,3
                if (xCord == -1 && yCord == 3)
                {

                }

                //-2,3
                if (xCord == -2 && yCord == 3)
                {

                }

                //-3,3
                if (xCord == -3 && yCord == 3)
                {

                }

                //1,-1
                if(xCord == 1 && yCord == -1)
                {

                }

                //2,-1
                if (xCord == 2 && yCord == -1)
                {

                }

                //3,-1
                if (xCord == 3 && yCord == -1)
                {

                }

                //-1,-1
                if (xCord == -1 && yCord == -1)
                {

                }

                //-2,-1
                if (xCord == -2 && yCord == -1)
                {

                }

                //-3,-1
                if (xCord == -3 && yCord == -1)
                {

                }

                //1,-2
                if (xCord == 1 && yCord == -2)
                {

                }

                //2,-2
                if (xCord == 2 && yCord == -2)
                {

                }

                //3,-2
                if (xCord == 3 && yCord == -2)
                {

                }

                //-1,-2
                if (xCord == -1 && yCord == -2)
                {

                }

                //-2,-2
                if (xCord == -2 && yCord == -2)
                {

                }

                //-3,-2
                if (xCord == -3 && yCord == -2)
                {

                }

                //1,-3
                if (xCord == 1 && yCord == -3)
                {

                }

                //2,-3
                if (xCord == 2 && yCord == -3)
                {

                }

                //3,-3
                if (xCord == 3 && yCord == -3)
                {

                }

                //-1,-3
                if (xCord == -1 && yCord == -3)
                {

                }

                //-2,-3
                if (xCord == -2 && yCord == -3)
                {

                }

                //-3,-3
                if (xCord == -3 && yCord == -3)
                {

                }

                //0,3
                if (xCord == 0 && yCord == 3)
                {

                }

                //0,2
                if (xCord == 0 && yCord == 2)
                {

                }

                //0,1
                if (xCord == 0 && yCord == 1)
                {

                }

                //0,0
                if (xCord == 0 && yCord == 0)
                {

                }

                //0,-1
                if (xCord == 0 && yCord == -1)
                {

                }

                //0,-2
                if (xCord == 0 && yCord == -2)
                {

                }

                //0,-3
                if (xCord == 0 && yCord == -3)
                {

                }



                //BOUNDARIES:


                //RIGHT
                if (xCord == 4)
                {
                    Console.WriteLine("This area doesn't exist.");
                    xCord = xCord - 1;
                }

                //LEFT
                if (xCord == -4)
                {
                    Console.WriteLine("This area doesn't exist.");
                    xCord = xCord + 1;
                }

                //TOP
                if (yCord == 4)
                {
                    Console.WriteLine("This area doesn't exist.");
                    yCord = yCord - 1;
                }

                //BOTTOM
                if (yCord == -4)
                {
                    Console.WriteLine("This area doesn't exist.");
                    yCord = yCord + 1;
                }

            }

        }
    }
}

标签: c#user-inputconsole.writeline

解决方案


这是一个主循环中的大量代码,我建议将功能拆分为更多功能。

回答你的问题;您的控制台发送垃圾邮件“它成功了!”的原因。是因为您的“if”语句位于连续的 while 循环中。您可以尝试在 if 语句中添加一个额外的布尔值,如下所示:

//GAMEPLAY
boolean hasMoved = false;
while (true)
{
    //2,0
    if (xCord == 2 && yCord == 0 && !hasMoved)
    {
        Console.WriteLine("It worked.");
        hasMoved = true;
    } 
    else{
        hasMoved = false;
    }

推荐阅读