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 create simple memory game in c# (console) ,a board hidden cards must generated , behind cards need have randomly selected chars a-z. have created class called table , containing matrix of chars. ive created method suppose randomly fill matrix 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 private member inside class                 j++;             }             i++;         } 

the problem in order game work need every random char created twice , , in random locations. im kinda stuck , idea had complicatied, advice?

you should first generate couples of letters beforehand, shuffle them , assign them matrix.

assuming have squared matrix , can have duplicates of couples:

int dimensionofmatrix=n; random rnd=new random(); char[] arrayofcouplesofletters=new char[dimensionofmatrix*dimensionofmatrix]; for(int i=0; < 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 permutation of original array int currposition=0; for(int i=0; < dimensionofmatrix; i++)     for(int j=0; j < dimensionofmatrix; j++){         matrix[i,j]=arrayofcouplesofletters[currposition];         currposition++;     } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -