Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a game that involves finding and clicking on a number (0-9) in a grid that randomizes each time you click on the correct one.
I want to get it so that when you click on the correct number, the grid randomizes again.
How would you do this?
Here's what it would kind of look like in the end:
I assume you're rendering an array of integers in order:
for (int i = 0; i < arrayOfNumbers.Length; i++ ) {
// rendering here
render(arrayOfNumbers[i]);
}
If thats the case.. just randomize the array after a successful click.. somewhat like this:
var rnd = new System.Random();
var arrayOfNumbers = Enumerable.Range(1, 9).OrderBy(r => rnd.Next()).ToArray();
Then you can just re-render (or let your game loop continue to render the array). Since the array has changed, your rendering will too.
Every time you detect a click on the correct number (I hope you know how to do this) you simply randomize the array of numbers you're displaying in your grid:
//Fisher-Yates algorithm
Random generator = new System.Random();
int len = array.Length;
while (len > 1)
{
len--;
int k = generator.Next(len + 1);
int temp = array[k];
array[k] = array[len];
array[len] = temp;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to find at what index a sine curve (which may start at any point along the curve) reaches its first maximum, and only the first. To do this, I am running a loop which compares one value to its previous. If one point is greater than its previous value, it is trending up, and similar for the opposite.
In c#, how do you detect when the variable has changed from trending up to trending down? In other words, how do you detect when the variable has changed. In LabVIEW, this can be done using a shift register. What is the equivalent in c#?
public static int FirstMaxIndex(int[] values)
{
bool up = false;
for (int i = 1; i < values.Length; i++)
if (values[i] < values[i - 1])
{
if (up) return i;
else up = false;
}
else if (values[i] > values[i - 1])
{
up = true;
}
return -1;
}
I didn't test this. This is only to give you an idea of how to solve this. (I wrote it as close as possible to what you wrote in a comment.)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Getting a byte array like this [0,0,0,0,0,1,1,1,1,3,3,3,3,0,0,0,0,0]
Does anyone know how to detect a change from 1 to 3 efficiently in linq?
Why Linq? you could achieve this with simple loop.
int previous = array[0];
for(int i=1;i< array.Length;i++)
{
if(Math.Abs(array[i]- previous) > 1) // use appropriate jump
{
//logic
}
previous = array[i];
}
If you are looking for Linq solution, you could do this.
int previous = array[0];
array.FirstOrDefault(x=>
{
var retValue = Math.Abs(x- previous) > 1;
previous = x;
return retValue;
});
If you want to find all change-indexes in the most efficient way:
List<int> changeIndexes = new List<int>();
for(int i = 1; i < array.Length; i++)
if(array[i] != array[i-1])
changeIndexes.Add(i);
Linq is not the best tool when it comes to indexes.
If you want to find only the index where 1 changes to 3 modify the condition accordingly:
if(array[i] == 3 && array[i-1] == 1) ... // break the loop if you only want to find the first
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am writing the beginning of my program which is to have the computer generate 3 random numbers on the console.
They need to be 1-9 (including both), no numbers can repeat, and I need to put this answer into an array. What's needed in the main method class vs the class.
You should try it out yourself first (site rules say so), but some hints may be provided:
1) integer random numbers can be generated using Random class. More details can be found here and an answer has been already provided about generation
2) to avoid duplicates each number should be tested against the existing list of numbers:
array.Contains(generatedNumber)
3) For your particular request, an elegant option is to generate all numbers between 1 and 9, shuffle the array and pick the first three elements:
var initArray = Enumerable.Range(1, 9).ToArray();
var randomArray = initArray.OrderBy(x => rnd.Next()).ToArray();
Get first three elements and those are random and distinct.
Generally, you can a subarray using the method specified here.
Try this:
Random rnd = new Random();
int[] arr = Enumerable.Range(0, 10).OrderBy(n => rnd.Next()).Take(3).ToArray();
foreach (var n in arr)
{
Console.WriteLine(n);
}
Try this code below :
//range set 0 to 9
int Min = 0;
int Max = 10;
//declare an array which store 3 random number
int[] arr = new int[3];
Random randNum = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = randNum.Next(Min, Max);
Console.WriteLine(arr[i]);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I wish to know if i can make a simple application in WFA that if you enter your name to generate a lucky number, I have an idea but I really don't know how to generate a number if you insert your name...Is it even posible ?
In C# a string is composed of single characters and those can be converted to int
string s = "Abc";
int i = (int)s[0]; // Get code of first character.
Yet another possibility is to get the hash code of the string:
int hash = s.GetHashCode();
This hash code should look random enough. If not, or if you need to create several random numbers from a name, you could use this hash code as a seed value for the random numbers.
var random = new Random(hash);
int n1 = random.Next();
int n2 = random.Next();
Try this:
var random = new Random();
var number = random.Next(0, 1000);
In this example, number is an int digit between 0 and 1000.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I enter the value in textbox:
1,2,3,4,5,6,7...
How to into to int array or list and show value display ???
And stop when a user enters the wrong consecutive 3 times or total error of 5
//textbox is your textbox controls' name
string[] content = textbox.Text.Split(',');
//use linq convert stringp[] to int[]
int[] nums = (from num in content
select int.Parse(num)).ToArray();
I don't understand the last part of your question (the bit about stopping when a user enters something wrong... I guess you'd have to define "wrong" in this context). Regardless, the following code should take values entered into a text box (let's call it txtNumbers) and put it in an array of ints.
string[] splitContent = txtNumbers.Text.Split(new char[] { ',' });
int[] nums = new int[splitContent.Length];
for (int i = 0; i < splitContent.Length; i++)
{
nums[i] = Int32.Parse(splitContent[i]);
}