Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have this grid containing 5x5 buttons where I want to randomly generate one number(1-5) to each button, and randomly place one (of 5) color to each button.

Ex

[RED 1][BLUE 5][GREEN 2][RED 4][YELLOW 3]
[GREEN 1][YELLOW 5][Orange 3] and so on.....

Ok, so the question is, in which steps should I think? And are there some good methods to use in this case? I'm starting from scratch....

I appreciate any help!

---
EDIT

So there will only exist five buttons of one color, and to each color there are numbered 1-5.
Posted
Updated 4-Feb-13 8:12am
v2
Comments
BC @ CV 4-Feb-13 15:13pm    
sounds like a homework assignment.
Member 9808042 4-Feb-13 15:33pm    
It's not. I'm not studying programming at all, just doing it in my free time =)) A game actually....I just want a push, cause right now I got no clue where to start.
BC @ CV 4-Feb-13 15:41pm    
Well....use a random number generation function such as: this[^] to generate the number.

Then set button colors thusly: button1.BackColor = myArrayOfPossibleColors[num];

and text values like so: button2.Content = num;
Member 9808042 4-Feb-13 15:55pm    
Thanks alot! As a beginner, I tend to overthink which makes it hard to see the whole picture. I just learned to break down the problems into minor parts.
Sergey Alexandrovich Kryukov 4-Feb-13 15:44pm    
What have you done so far?
—SA

1 solution

What you need is to generate a random permutation of 0-24, assigning one number for each of the buttons. Then just use number % 5 to assign the number and number / 5 to index into the color list.
To efficiently generate the random permutation try this:
C#
using System.Collections.Generic;
using System.Linq;

private Random Rand = new Random();  // OR use a fixed "seed" here for reproducability when testing
private List<int> RandPerm(int N)
{
  List<int> p = new List<int>(Enumerable.Range(0, N));
  for (int k = 2; k <= N; k++)
  {
    int r = (int)Math.Ceiling(k * Rand.NextDouble());
    --r;
    p[k - 1] = p[r];
    p[r] = k - 1;
  }
  return p;
}
 
Share this answer
 
Comments
Member 9808042 4-Feb-13 16:04pm    
Permutation! I learned that from earlier years (discrete)...Great idea! But the code you wrote....will take me some time to understand haha..=D

I really appreciate your time. Thanks!
Sergey Alexandrovich Kryukov 4-Feb-13 16:26pm    
My 5, but important note: the buttons themselves should also be created in the loop in C# code, not in XAML. Only this way, OP could have an array Button[,] to work with.
—SA
Matt T Heffron 4-Feb-13 16:38pm    
But in the code behind, OP could populate an array by referencing the buttons named in the XAML...
Sergey Alexandrovich Kryukov 4-Feb-13 16:41pm    
Yes, of course, but it would be ugly, unnatural. XAML is often abused, as well as designer. And attempt to replace code with "visual programming" often introduces a lot of manual repetitive work...
—SA
Matt T Heffron 4-Feb-13 16:50pm    
As is usually the case, the "correct" way is "it depends" :)
Are the colors and numbers being set through WPF Data Binding (changing often)?
Data Binding in code is much messier than in XAML.
Will the layout be complicated? (OP already stated a "simple" Grid layout to start with.)
Etc...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900