user3456893
user3456893

Reputation: 19

How can i fill a matrix with random chars in C# , if for every char that apperas it must appear twice (memory game)

i need to create a simple memory game in c# (console) ,a board with hidden cards must be generated , behind the cards i need to have randomly selected chars from A-Z. i have created a class called Table , containing a matrix of chars. ive also created a method which suppose to randomly fill the matrix with chars

  public void set_table_values()
    {
        int i=0,randchar=0;
        Random board_filler = new Random() ;
        while(i< Height)
        {
            int j = 0;
            while (j<Width)
            {
                randchar=board_filler.Next(0, 26);
                Table_matrix[i, j] = (char)('A'+randchar);//table matrix is a private member inside the class
                j++;
            }
            i++;
        }

the problem is that in order for the game to work i need that every random char will be created twice , and in random locations. im kinda stuck and the only idea i had is too much complicatied, any advice?

Upvotes: 1

Views: 2633

Answers (4)

erik.dreyer
erik.dreyer

Reputation: 17

I would use a list or an array. Then just print the random index of the array or list.

Random rnd = new Random();
int myRandomNumber;

List<char> letters = new List<char>();
letters.Add('a');
letters.Add('a');
letters.Add('b');
letters.Add('b');
letters.Add('c');
letters.Add('c');

for (int i = 0; i< letters.Length; i++)
{
     myRandomNumber = rnd.Next(0, letters.Length);
     Console.Write(letters[myRandomNumber]);
}

Upvotes: 0

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

You should first generate all the couples of letters beforehand, shuffle them and then assign them to the matrix.

Assuming you have a squared matrix and you can have duplicates of couples:

int dimensionOfMatrix=n;
Random rnd=new Random();
char[] arrayOfCouplesOfLetters=new char[dimensionOfMatrix*dimensionOfMatrix];
for(int i=0; i < arrayOfCouplesOfLetters.Count(); i=i+2){
   char letter=(char)rnd.Next(65,91);
   arrayOfCouplesOfLetters[i]=letter;
   arrayOfCouplesOfLetters[i+1]=letter;
}
arrayOfCouplesOfLetters=ShuffleArray(arrayOfCouplesOfLetters); //ShuffleArray should return a permutation of the original array
int currPosition=0;
for(int i=0; i < dimensionOfMatrix; i++)
    for(int j=0; j < dimensionOfMatrix; j++){
        matrix[i,j]=arrayOfCouplesOfLetters[currPosition];
        currPosition++;
    }

Upvotes: 3

Nicholas Carey
Nicholas Carey

Reputation: 74317

Not much more complex than

public static char[,] CreateCharArray( int rows , int cols )
{
  const string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; // whatever character set you want here
  if ( rows < 1 || cols < 1 ) throw new ArgumentException();

  // create and populate the initial set of pairs
  char[] chars = new char[rows*cols];
  for ( int i = 0 ; i < chars.Length ; )
  {
    char ch = charset[ rng.Next(charset.Length) ] ;
    chars[i++] = ch ;
    chars[i++] = ch ;
  }

  // shuffle it up
  Shuffle(chars) ;

  // construct the 2-D grid
  char[,] grid = new char[rows,cols];
  for ( int i = 0 , k = 0 ; i < rows ; ++i )
  {
    for ( int j = 0 ; j < cols ; ++j )
    {
      grid[i,j] = chars[k++] ;
    }
  }

  return grid ;
}
private static Random rng = new Random() ;

private static void Shuffle( char[] chars)
{
  for ( int i = chars.Length ; --i >= 0 ; )
  {
    int j    = rng.Next(i) ;
    char t   = chars[j] ;
    chars[j] = chars[i] ;
    chars[i] = t        ;
  }
  return ;
}

Upvotes: 0

Simon
Simon

Reputation: 2960

Expanding on @Saverio's idea from above, use:

Randomize a List<T>

To shuffle the list. Ensure the list is the same size as the matrix.

Upvotes: -1

Related Questions