2017-07-20 3 views
-3

期待される:char 'H'はデフォルトの 'X'で作られた5x5配列内を移動します。C#で配列内の文字を移動する

結果1:一致するインデックス付き 'X'を置き換える代わりに、 'H'と 'P'を追加するとグリッドが展開されます。

結果2:char 'H'は移動しません。

ここでのコードは、これまでだ。このような何かが

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座標を渡す

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

....これは、コードのサービスサイトではありません......しかし、あなたが特定の問題をしようと読んでた後で行うので管理していないかを説明するための場所。 ...あなたの配列にある文字については、代わりに 'char [、]'を使用してください。 –

+0

@Nunoこのサイトでは敬語は必要ありません。私はおそらく、ダウンマーク警察が来る前にそれらを削除するだろう。 –

+0

こんにちは@GiladGreen!申し訳ありませんが、私はここでメタが欠けています。 char可変型に関するヒントをありがとう。私の目標に役立つと思われる参考資料も大歓迎です。 –

答えて

-2

それはコードするのが楽しいです。あまりにも多すぎるかもしれません。ごめんなさい。 「私はXYZが必要」

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

だから私は最終的にあなたのコードを理解し、あなたは高さの値と幅の値のyを決定xに気付いた:D –

0

あなたがダウンしたい場合は+1を、アップダウンのために上に移動したい場合は-1を、左矢印の場合も同様に移動します。

関連する問題