【问题标题】:Issue with Updating Array更新数组的问题
【发布时间】:2013-06-21 17:48:27
【问题描述】:

我有一个控制台应用程序,它显示一个数组并根据用户的输入将数字移动到“0”的左侧、右侧、顶部或底部

这是我的代码:

int[,] myNos = new int[3, 3] { { 3, 0, 7 }, { 9, 4, 8 }, { 2, 1, 5 } };

public void WriteData()
{
   Console.SetCursorPosition(0, 4);
   for (int col = 0; col < 3; col++)
   {
      for (int row = 0; row < 3; row++)
      {
         Console.Write("{0}\t", myNos[col, row]);
      }
      Console.WriteLine();
   }
}

public void Level1()
{
   Console.Clear();
   Console.SetCursorPosition(0, 0);
   Console.Write("Score : {0}", score);
   Console.SetCursorPosition(30, 0);
   Console.Write("Moves : {0}", moves);

   WriteData();

   string move = "a";

   while (move != "E")
   {
      Console.SetCursorPosition(0, 8);
      Console.Write("\nEnter your Move [L,R,T,B,E] : ");
      move = Console.ReadLine();
      move = move.ToUpper();

      while (move == "L")
      {
         if (myNos[0, 0] == 0)
         {
            WriteData();
         }
         else if (myNos[0, 1] == 0)
         {
            myNos[0, 1] = myNos[0, 0];
            myNos[0, 0] = 0;
            WriteData();
         }
         else if (myNos[0, 2] == 0)
         {
            myNos[0, 2] = myNos[0, 1];
            myNos[0, 1] = 0;
            WriteData();
         }
         else
         {
            WriteData();
         }
      }
   }
}

一切都按我的意愿工作,但唯一的问题是在数组更新并重新显示后,光标不会回到它将读取输入的位置。我试过使用:

Console.SetCursorPosition(0, 8);

和:

Console.ReadKey();

但似乎两者都不起作用。

【问题讨论】:

    标签: c# arrays console-application


    【解决方案1】:

    我想到了答案,我将 move 变量声明为全局变量,然后修改了我的 WriteData() 方法和

          public void WriteData()
          {
               Console.SetCursorPosition(0, 4);
               for (int col = 0; col < 3; col++)
               {
                    for (int row = 0; row < 3; row++)
                    {
                        Console.Write("{0}\t", myNos[col, row]);
                    }
                    Console.WriteLine();
                }
                //This was the modification
                menu = "a";
         }
    

    【讨论】:

      【解决方案2】:

      嗯,已经完成了气味问题的作业。

      从概念上来说,光标显示位置与输入流无关,与数组更新无关。

      代码分为部分类。虽然我没有,但我相信它也可以在 MVC 模式中实现,即使它是一个控制台应用程序。

      需要 [puzzle solver]?

      p.s.:我不知道你会如何计算分数,因此它总是为零。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      class Program {
          static void Main(String[] args) {
              (new SlidingPuzzle(new[,] { 
                  { 3, 0, 8 }, 
                  { 6, 4, 7 }, 
                  { 2, 1, 5 }, 
                  })).Run();
          }
      }
      
      public partial struct Position {
          public Position(int left, int top) {
              Left=left;
              Top=top;
          }
      
          public int Left, Top;
      }
      

      对于这部分,是控制器的候选:

      public partial class SlidingPuzzle {
          public void Run() {
              for(bool initialized=false, quit=false; ; ) {
                  if(!initialized) {
                      Reset();
                      Console.Clear();
                      ShowPrompt();
                      initialized=true;
                  }
      
                  ShowStatics();
                  ShowMatrix();
      
                  if(quit||IsGameOver||IsFinished) {
                      SetCursorToBottom();
      
                      if(!quit)
                          ShowOnEnd();
      
                      if(!(quit=IsExit)) {
                          initialized=false;
                          continue;
                      }
      
                      break;
                  }
      
                  var keyInput=Console.ReadKey(true).Key;
      
                  if(ConsoleKey.Escape==keyInput)
                      quit=true;
                  else if(ConsoleKey.LeftArrow==keyInput)
                      Move(new[] { +0, +1 });
                  else if(ConsoleKey.UpArrow==keyInput)
                      Move(new[] { +1, +0 });
                  else if(ConsoleKey.RightArrow==keyInput)
                      Move(new[] { +0, -1 });
                  else if(ConsoleKey.DownArrow==keyInput)
                      Move(new[] { -1, +0 });
              }
          }
      
          void ReadOnExit() {
              Console.WriteLine("Press any key to exit ... ");
              Console.ReadKey(true);
          }
      
          bool IsExit {
              get {
                  Console.WriteLine("Try again? (Y/N)");
                  var quit=ConsoleKey.Y!=Console.ReadKey(true).Key;
      
                  if(quit)
                      ReadOnExit();
      
                  return quit;
              }
          }
      }
      

      而这个,是给观点的候选人的:

      partial class SlidingPuzzle {
          void SetCursorToBottom() {
              var pos=posBottom;
              Console.SetCursorPosition(pos.Left, pos.Top);
          }
      
          void ShowOnEnd() {
              if(IsGameOver) {
                  Console.WriteLine("- Game Over -");
                  Console.WriteLine("You've reached the limitation of movement. ");
              }
              else {
                  Console.WriteLine("- Game Completed -");
                  Console.WriteLine("Congratulations! You've solved the puzzle. ");
              }
          }
      
          void ShowStatics() {
              var pos=posStatics;
              Console.SetCursorPosition(pos.Left, pos.Top);
      
              Console.Write("Score: {0}", score);
              Console.Write(SlidingPuzzle.indentation);
              Console.WriteLine("Moves: {0}", moves);
          }
      
          void ShowPrompt() {
              var pos=posPrompt;
              Console.SetCursorPosition(pos.Left, pos.Top);
      
              Console.WriteLine("Enter your move [←, ↑, →, ↓, Esc] ");
          }
      
          void ShowMatrix() {
              var pos=posMatrix;
              Console.SetCursorPosition(pos.Left, pos.Top);
      
              for(int m=lengths[0], i=0, n, j; m-->0; ++i)
                  for(n=lengths[1], j=0; n-->0; ++j) {
                      var value=this[i, j].ToString();
      
                      if("0"==value) {
                          value="\x20";
                          var left=indentation.Length+Console.CursorLeft;
                          pos=new Position(left, Console.CursorTop);
                      }
      
                      if(SlidingPuzzle.linesPerRow>0)
                          Console.Write(SlidingPuzzle.indentation+value);
      
                      if(0==n)
                          for(var count=SlidingPuzzle.linesPerRow; count-->0; )
                              Console.WriteLine("");
                  }
      
              Console.SetCursorPosition(pos.Left, pos.Top);
          }
      
          void SetLayoutPosition() {
              posMatrix=new Position(0, 2);
              posPrompt=new Position(0, posMatrix.Top+lengths[0]*linesPerRow);
              posBottom=new Position(0, 1+posPrompt.Top);
              posStatics=new Position(0, 0);
          }
      
          Position posBottom, posMatrix, posStatics, posPrompt;
      
          static readonly int linesPerRow=2;
          static readonly String indentation=new String('\x20', 4);
      }
      

      以及模型的候选者:

      partial class SlidingPuzzle {
          void Reset() {
              Array.Copy(initial, mutable, initial.Length);
              current=IndexOf(0);
              moves=0;
              score=0;
          }
      
          void Move(int[] offset) {
              var indices=new int[lengths.Length];
              var i=0;
      
              for(i=indices.Length; i-->0; indices[i]=current[i]+offset[i])
                  ;
      
              for(i=indices.Length; i-->0; )
                  if(0>indices[i]||indices[i]>lengths[i]-1)
                      break;
      
              if(i<0) {
                  var value=this[current];
                  this[current]=this[indices];
                  this[indices]=value;
                  current=indices;
                  ++moves;
              }
          }
      
          int[] IndexOf(object value) {
              for(int m=lengths[0], i=0, n, j; m-->0; ++i)
                  for(n=lengths[1], j=0; n-->0; ++j)
                      if(this[i, j].ToString()==value.ToString())
                          return new[] { i, j };
      
              return SlidingPuzzle.emptyArray;
          }
      
          object this[params int[] indices] {
              set {
                  mutable.SetValue(value, indices);
              }
      
              get {
                  return mutable.GetValue(indices);
              }
          }
      
          bool IsFinished {
              get {
                  for(int m=lengths[0], i=0, n, j; m-->0; ++i)
                      for(n=lengths[1], j=0; n-->0; ++j)
                          if(this[i, j].ToString()!=(1+i*lengths[1]+j).ToString())
                              if(0!=m||0!=n)
                                  return false;
      
                  return true;
              }
          }
      
          bool IsGameOver {
              get {
                  return !(maxMoves!=moves||IsFinished);
              }
          }
      
          public SlidingPuzzle(Array original) {
              var elementType=original.GetType().GetElementType();
              lengths=new int[original.Rank];
      
              for(var i=original.Rank; i-->0; lengths[i]=original.GetLength(i))
                  ;
      
              initial=Array.CreateInstance(elementType, lengths);
              mutable=Array.CreateInstance(elementType, lengths);
              Array.Copy(original, initial, original.Length);
              SetLayoutPosition();
          }
      
          int[] current;
          Array mutable;
          readonly int[] lengths;
          readonly Array initial;
          int score, moves, maxMoves=25;
      
          static readonly int[] emptyArray=new int[] { };
      }
      

      【讨论】:

      • 感谢您的回答,但我只修改了一行代码,它按我的意愿工作
      • @Precious1tj:我写这个只是为了好玩。代码可编译可运行,你可以试试游戏,我玩得很开心。
      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 2022-06-15
      • 2015-11-25
      相关资源
      最近更新 更多