2017-02-28 85 views
-6

嗨我有二维数组类型的int我有数字,例如从1到100.如何将char('X')设置为int数组?

这是一个游戏 - 类似Tic-Tac-Toe - 但我使用更大的数组。所以我需要把用户的字符'X'或'O'放到这个数组中。但问题是我不能(不知道)如何把这些字符放在那个int数组中。我只想使用控制台。

我试过让数组类型的字符,但然后我不能用数字填充数组。

我知道该怎么做,如果用户将有希望把一些数字,但然后,它看起来并不好......

我会很高兴的任何建议如何做到这一点。

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

只要考虑一下你的问题,我可以想象两种方式,我会做到这一点,而不会太花哨。

第一个是使用两个数组。一个用于保存数字(和int数组),一个用于保存玩家输入(一个保存“x”和“o”的字符数组)。这可能是这样的:

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] + "\""); 
    } 
} 

第二种解决方案是创建一个对象,以满足您的需求,并有这一个阵列。好处是你只有一个数组,每个单元格都包含与这个单元格有关的所有信息。考虑以下几点:

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 S和class是因为他们让你做一些非常强大的事情,就像在这种情况下捆绑在一起的相关信息一样。

+0

非常感谢。有用!!! –