2017-02-28 3 views
-6

こんにちは私は1から100int配列にchar( 'X')を入れる方法は?チックタックつま先のようなもの - - しかし、私は大きな配列を使用

に、たとえばそれはゲームだ、私は数字を持っているint型の2次元配列型を持っています。だから、私は文字 'X'または 'O'をこの配列に配置する必要があります。しかし、問題は、int配列にこれらの文字を配置する方法がわからないことです。私はコンソールだけを使いたいと思う。

私はchar型の配列型を作成しようとしましたが、配列で数値を埋めることができません。

ユーザーが持っているならば、私はそれを行う方法を知っているが、私はそれを行うにはどのように何かアドバイスのために幸せになるいくつかの数字を入れてますが、それは良い見ていない...

たい。

public void Napln() { //filling the array 
     int poc = 1; 

     for (int i = 0; i < pole.GetLength(1); i++) 
     { 
      Console.Write(" "); 
      for (int j = 0; j < pole.GetLength(0); j++) 
      { 
       if (poc < 10) 
        Console.Write(" " + (pole[j, i] = poc++) + " | "); 
       else if (poc < 100)    
        Console.Write((pole[j,i] = poC++) + " | "); 
       else      
        Console.Write((pole[j, i] = poc++) + " | ");      
      } 
      Console.WriteLine(); 
      for (int v = 0; v < roz1; v ++) 
       Console.Write("_____|"); 
      Console.WriteLine(); 
     } 
     Console.WriteLine(); 

public void Pozice (int vyber) //find the user choice 
    { 
     for (int i = 0; i < pole.GetLength(1); i ++) 
     { 
      for (int j = 0; j < pole.GetLength(0); j ++) 
      { 
       if (pole[i, j] == vyber) 
       { 
        pole[i, j] = 'X';   
        hraci.Vypis(); 
       } 
      } 
     } 
    } 
public void Vypis() //print the same with change of user choice 
    { 
     for (int i = 0; i < pole.GetLength(1); i ++) 
     { 
      Console.Write(" "); 
      for (int j = 0; j < pole.GetLength(0); j ++) 
      { 
       if (pole[j,i] < 10) 
        Console.Write(" " + pole[j, i] + " | "); 
       else if (pole[j,i] < 100) 
        Console.Write(pole[j, i] + " | "); 
       else 
        Console.Write(pole[j, i] + " | "); 
      } 
      Console.WriteLine(); 
      for (int v = 0; v < roz1; v++) 
       Console.Write("_____|"); 
      Console.WriteLine(); 
     } 
    } 

私はC#で特に新しいOOPです。だからあなたはもう助言があれば、私は幸せになるでしょう。

+5

***コードを表示*** – abelenky

+0

なぜボードに数字を入力する必要がありますか? – Lee

+0

でも数字を文字列に入れることはできますか? –

答えて

0

ちょうどあなたの疑問を抱いて、私はあまりにも幻想的でなくてもそれをやり遂げる2つの方法を想像することができます。

最初に2つの配列を使用します。数字を保持するためのもの(およびint配列)、プレーヤーの入力を保持するもの( "x"と "o"を保持するchar配列)これは次のようになります。

public class Program 
{ 
    public static void Main() 
    { 
     int width = 10; 
     int height = 10; 

     char[,] playerBoard = new char[width, height]; 
     int[,] numberedBoard = new int[width, height]; 

     for (int x = 0; x < width; x++) { 
      for (int y = 0; y < height; y++) { 
       // Fill the numbered board with 1 to 100 
       numberedBoard[x, y] = x * width + y + 1; 

       // And the player board with emptyness 
       playerBoard[x, y] = ' '; 
      } 
     } 

     System.Console.WriteLine("Number at x = 3/y = 5: " + numberedBoard[3, 5]); 
     System.Console.WriteLine("Current owner of x = 3/y = 5: \"" + playerBoard[3, 5] + "\""); 

     // Let's change the owner of x = 3 and y = 5 
     playerBoard[3, 5] = 'X'; 

     System.Console.WriteLine("New owner of x = 3/y = 5: \"" + playerBoard[3, 5] + "\""); 
    } 
} 

2番目の解決策は、ニーズに合わせてオブジェクトを作成し、この配列を作成することです。利点は、配列が1つしかなく、各セルがこのセルに関連するすべての情報を保持していることです。考えてみましょう以下:

using System; 

public class Program 
{ 
    struct BoardEntry { 
     public int number; 
     public char owner; 
    } 

    public static void Main() 
    { 
     int width = 10; 
     int height = 10; 

     BoardEntry[,] board = new BoardEntry[width, height]; 

     for (int x = 0; x < width; x++) { 
      for (int y = 0; y < height; y++) { 
       // For each "cell" of the board, we create a new instance of 
       // BoardEntry, holding the number for this cell and a possible ownage. 
       board[x, y] = new BoardEntry() { 
        number = x * width + y + 1, 
        owner = ' ' 
       }; 
      } 
     } 

     // You can access each of those instances with `board[x,y].number` and `board[x,y].owner` 

     System.Console.WriteLine("Number at x = 3/y = 5: " + board[3, 5].number); 
     System.Console.WriteLine("Current owner of x = 3/y = 5: \"" + board[3, 5].owner + "\""); 

     // Let's change the owner of x = 3 and y = 5 
     board[3, 5].owner = 'X'; 
     System.Console.WriteLine("New owner at x = 3/y = 5: \"" + board[3, 5].owner + "\""); 
    } 
} 

それはあなたがプログラミングに新しいもののように思えるので、最初のソリューションを理解し、今使用する方が簡単かもしれませんが、私はあなたには、いくつかの時点で何struct秒に少し読むことをお勧めし、 classは、この場合は一緒に属している関連情報をバンドルするような、本当に強力なものをやらせるためです。

+0

ありがとうございます。できます!!! –

関連する問題