2017-07-20 19 views
-3

預期:字符'H'在由默認'X'構成的5x5數組中移動。在C#中的數組中移動一個字符

結果1:通過添加'H'和'P'而不是替換匹配的索引'X'來擴展網格。

結果2:char'H'不動。

這裏是到目前爲止的代碼:

class Mainclass 
{ 
    public static void Main(string[] args) 
    { 

     Console.WriteLine("The Board Game"); 
     Console.WriteLine(); 
     Console.WriteLine(">>Press any key to begin<<"); 

     Console.ReadKey(); 
     Console.Clear(); 


     Console.WriteLine("The Board Game"); 
     Console.WriteLine(); 

     CreateGrid(); 

     Console.WriteLine(); 

     Console.WriteLine("H = your hero"); 
     Console.WriteLine(); 
     Console.WriteLine("X = floor tiles"); 
     Console.WriteLine(); 
     Console.WriteLine("P = enemy pawn"); 

     Console.WriteLine(); 
     Console.WriteLine(); 
     Console.WriteLine("Press the arrows in your keyboard to move in that direction"); 

    } 

    public static void CreateGrid() 
    { 

     int width = 5; 
     int height = 5; 

     char[,] grid = new int[width, height]; 
     grid[2, 2] = 'H'; 
     grid[4, 4] = 'P'; 

     for (int x = 0; x < width; x++) 
     { 
      for (int y = 0; y < height; y++) 
      { 
       Console.Write(grid[x, y] + " "); 
      } 
      Console.WriteLine(); 

     } 

     if (ConsoleKeydown(ConsoleKey.up)){ 
     get hero.index[,]; 
     newHeroPosition = Hero.index +1, +0; 
     char hero.index = H; 
     char newHeroPosition = x; 
     } 

     /*I want the tile to be identified and swap the char value 
     of it with the one above it on the grid */ 


    } 
} 

}

+2

「我需要XYZ」 ......這不是一個代碼服務站點......但一個地方來形容你是不是嘗試管理和閱讀,並具有特定的問題後做了什麼。 ...至於在你的數組中有字母......使用'char [,]'而不是 –

+0

@Nuno本網站不需要Salutations。我可能會在警方降臨之前將他們移走。 –

+0

Hi @GiladGreen!對不起,如果我在這裏錯過了元。感謝關於char變量類型的提示。您可能會發現對我的目標有用的任何參考也非常受歡迎。 –

回答

-2

好吧。編碼很有趣。可能做得太多了。抱歉。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApp3 
{ 
class Program 
{ 


    static void Main(string[] args) 
    { 
     Console.WriteLine("The Board Game"); 
     Console.WriteLine(); 
     Console.WriteLine(">>Press any key to begin<<"); 

     Console.ReadKey(); 
     Console.Clear(); 


     Console.WriteLine("The Board Game"); 
     Console.WriteLine(); 

     CreateGrid(); 

    } 


    static void CreateGrid() 
    { 

     int heroX = 2; 
     int heroY = 2; 
     int width = 5; 
     int height = 5; 
     char[,] grid = new char[width, height]; 


     for (int i = 0; i < width; i++) 
     { 
      for (int j = 0; j < height; j++) 
      { 
       grid[j, i] = 'x'; 
      } 
     } 
     grid[heroX, heroY] = 'H'; 
     grid[4, 4] = 'P'; 


     Console.WriteLine(); 

     Console.WriteLine("H = your hero"); 
     Console.WriteLine(); 
     Console.WriteLine("X = floor tiles"); 
     Console.WriteLine(); 
     Console.WriteLine("P = enemy pawn"); 

     Console.WriteLine(); 
     Console.WriteLine(); 
     Console.WriteLine("Press the arrows in your keyboard to move in that direction"); 


     for (int x = 0; x < width; x++) 
     { 
      for (int y = 0; y < height; y++) 
      { 
       Console.Write(grid[x, y] + " "); 
      } 
      Console.WriteLine(); 

     } 

     while (true) 
     { 
      var key = Console.ReadKey(); 
      char temp; 

      switch (key.Key) 
      { 
       case ConsoleKey.UpArrow: 
        if (heroX == 0) 
         break; 

        temp = grid[heroX - 1, heroY]; 
        grid[heroX - 1, heroY] = 'H'; 
        grid[heroX, heroY] = temp; 
        heroX--; 
        Console.Clear(); 
        for (int x = 0; x < width; x++) 
        { 
         for (int y = 0; y < height; y++) 
         { 
          Console.Write(grid[x, y] + " "); 
         } 
         Console.WriteLine(); 

        } 
        break; 

       case ConsoleKey.DownArrow: 
        if (heroX == height - 1) 
         break; 

        temp = grid[heroX + 1, heroY]; 
        grid[heroX + 1, heroY] = 'H'; 
        grid[heroX, heroY] = temp; 
        heroX++; 
        Console.Clear(); 
        for (int x = 0; x < width; x++) 
        { 
         for (int y = 0; y < height; y++) 
         { 
          Console.Write(grid[x, y] + " "); 
         } 
         Console.WriteLine(); 

        } 
        break; 

       case ConsoleKey.LeftArrow: 
        if (heroY == 0) 
         break; 

        temp = grid[heroX, heroY - 1]; 
        grid[heroX, heroY - 1] = 'H'; 
        grid[heroX, heroY] = temp; 
        heroY--; 
        Console.Clear(); 
        for (int x = 0; x < width; x++) 
        { 
         for (int y = 0; y < height; y++) 
         { 
          Console.Write(grid[x, y] + " "); 
         } 
         Console.WriteLine(); 

        } 
        break; 

       case ConsoleKey.RightArrow: 
        if (heroY == width - 1) 
         break; 

        temp = grid[heroX, heroY + 1]; 
        grid[heroX, heroY + 1] = 'H'; 
        grid[heroX, heroY] = temp; 
        heroY++; 
        Console.Clear(); 
        for (int x = 0; x < width; x++) 
        { 
         for (int y = 0; y < height; y++) 
         { 
          Console.Write(grid[x, y] + " "); 
         } 
         Console.WriteLine(); 

        } 
        break; 

       default: 
        break; 
      } 
     } 
    } 


} 
} 
+0

它起作用了,現在我正在研究原因。非常感謝,@AdamKingsley! –

+0

@NunoNeves沒問題。 :-) –

+0

所以我終於明白了你的代碼,並且注意到你確定x爲高度值,y爲寬度值:D –

0

像這樣的東西應該幫助你

public static void move(ref int[][] grid, ref int heroX, ref int heroY, int upDown, int leftRight) { 
    if ((heroX + upDown >= 0) && (heroX + upDown < grid.Length) && 
     (heroY + leftRight >= 0) && (heroY + leftRight < grid[heroX + upDown].Length)) { 
     int aux = grid[heroX][heroY]; 
     grid[heroX][heroY] = grid[heroX += upDown][heroY += leftRight]; 
     grid[heroX][heroY] = aux; //heroX and heroY were changed in the previous operation 
    } 
} 

只需通過電網,當前的X,英雄的y位置+1如果你想讓他下降,-1如果你想讓他升起upDown和類似leftRight。