Checks if the pressed keys correspond to the background colors of the console application [on hold]
I'm making a finger trainer for a rhythm game with 10 keys.
But it only works after multiple presses.
Either the performance is bad, or you have to press the keys too perfectly.
static void Main(string args)
{
//assigning of the variables
bool keysNotPressed;
string input = String.Empty;
Random rnd = new Random();
ConsoleKeyInfo key1 = new ConsoleKeyInfo();
ConsoleKeyInfo key2 = new ConsoleKeyInfo();
ConsoleKeyInfo key3 = new ConsoleKeyInfo();
...
ConsoleKeyInfo key10 = new ConsoleKeyInfo();
int oldRndNumber1 = 0;
int rndNumber1 = 0;
int rndNumber2 = 0;
int rndNumber3 = 0;
...
int rndNumber9 = 0;
//assigns the ConsoleKeyInfo to the users prefered keys
Console.WriteLine("Give your prefered 10 keybinds");
Console.WriteLine("nKey1:");
key1 = Console.ReadKey();
Console.WriteLine(key1.Key);
Console.WriteLine("nKey2:");
key2 = Console.ReadKey();
Console.WriteLine(key2.Key);
Console.WriteLine("nKey3:");
key3 = Console.ReadKey();
...
Console.WriteLine("nKey10:");
key10 = Console.ReadKey();
Console.WriteLine(key10.Key);
//lets the user choose how many notes (spaces with background color) should be randomly placed
Console.WriteLine("How many notes should come on the screen simultaneously? 1-10");
input = Console.ReadLine().ToLower();
switch (input)
{
case "1":
...
break;
case "2":
...
break;
case "3":
while (true)
{
//generates random numbers representing the notes
oldRndNumber1 = rndNumber1;
do
{
rndNumber1 = rnd.Next(10) + 1;
} while (rndNumber1 == oldRndNumber1); //makes sure no note position will be the same
notePlacer(rndNumber1); //places the note equal to the random generated number
do
{
rndNumber2 = rnd.Next(10) + 1;
} while (rndNumber2 == rndNumber1);
notePlacer(rndNumber2);
do
{
rndNumber3 = rnd.Next(10) + 1;
} while (rndNumber3 == rndNumber1 || rndNumber3 == rndNumber2);
notePlacer(rndNumber3);
//checks if the keys equal to the random generated numbers are all pressed at the same time, until it happens
while (keysNotPressed)
{
if (keyCheck(rndNumber1, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber2, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber3, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10))
{
keysNotPressed = false;
}
}
keysNotPressed = true;
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
}
...
case "10":
...
break;
}
}
static void notePlacer (int rndNumber)
{
switch (rndNumber)
{
case 1:
Console.SetCursorPosition(0, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
case 2:
Console.SetCursorPosition(5, 10);
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine(" ");
break;
case 3:
Console.SetCursorPosition(10, 10);
Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine(" ");
break;
...
case 10:
Console.SetCursorPosition(50, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
}
}
static bool keyCheck(int rndNumber, ConsoleKeyInfo key1, ConsoleKeyInfo key2, ConsoleKeyInfo key3, ConsoleKeyInfo key4, ConsoleKeyInfo key5, ConsoleKeyInfo key6, ConsoleKeyInfo key7, ConsoleKeyInfo key8, ConsoleKeyInfo key9, ConsoleKeyInfo key10)
{
bool isPressed = false;
switch (rndNumber)
{
case 1:
if(Console.ReadKey() == key1)
{
isPressed = true;
}
break;
case 2:
if (Console.ReadKey() == key2)
{
isPressed = true;
}
break;
case 3:
if (Console.ReadKey() == key3)
{
isPressed = true;
}
break;
...
case 10:
if (Console.ReadKey() == key10)
{
isPressed = true;
}
break;
}
return isPressed;
}
Is there a better way to do this?
Thanks in advance!
Emanuel
c# game console
put on hold as off-topic by t3chb0t, Heslacher, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
I'm making a finger trainer for a rhythm game with 10 keys.
But it only works after multiple presses.
Either the performance is bad, or you have to press the keys too perfectly.
static void Main(string args)
{
//assigning of the variables
bool keysNotPressed;
string input = String.Empty;
Random rnd = new Random();
ConsoleKeyInfo key1 = new ConsoleKeyInfo();
ConsoleKeyInfo key2 = new ConsoleKeyInfo();
ConsoleKeyInfo key3 = new ConsoleKeyInfo();
...
ConsoleKeyInfo key10 = new ConsoleKeyInfo();
int oldRndNumber1 = 0;
int rndNumber1 = 0;
int rndNumber2 = 0;
int rndNumber3 = 0;
...
int rndNumber9 = 0;
//assigns the ConsoleKeyInfo to the users prefered keys
Console.WriteLine("Give your prefered 10 keybinds");
Console.WriteLine("nKey1:");
key1 = Console.ReadKey();
Console.WriteLine(key1.Key);
Console.WriteLine("nKey2:");
key2 = Console.ReadKey();
Console.WriteLine(key2.Key);
Console.WriteLine("nKey3:");
key3 = Console.ReadKey();
...
Console.WriteLine("nKey10:");
key10 = Console.ReadKey();
Console.WriteLine(key10.Key);
//lets the user choose how many notes (spaces with background color) should be randomly placed
Console.WriteLine("How many notes should come on the screen simultaneously? 1-10");
input = Console.ReadLine().ToLower();
switch (input)
{
case "1":
...
break;
case "2":
...
break;
case "3":
while (true)
{
//generates random numbers representing the notes
oldRndNumber1 = rndNumber1;
do
{
rndNumber1 = rnd.Next(10) + 1;
} while (rndNumber1 == oldRndNumber1); //makes sure no note position will be the same
notePlacer(rndNumber1); //places the note equal to the random generated number
do
{
rndNumber2 = rnd.Next(10) + 1;
} while (rndNumber2 == rndNumber1);
notePlacer(rndNumber2);
do
{
rndNumber3 = rnd.Next(10) + 1;
} while (rndNumber3 == rndNumber1 || rndNumber3 == rndNumber2);
notePlacer(rndNumber3);
//checks if the keys equal to the random generated numbers are all pressed at the same time, until it happens
while (keysNotPressed)
{
if (keyCheck(rndNumber1, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber2, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber3, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10))
{
keysNotPressed = false;
}
}
keysNotPressed = true;
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
}
...
case "10":
...
break;
}
}
static void notePlacer (int rndNumber)
{
switch (rndNumber)
{
case 1:
Console.SetCursorPosition(0, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
case 2:
Console.SetCursorPosition(5, 10);
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine(" ");
break;
case 3:
Console.SetCursorPosition(10, 10);
Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine(" ");
break;
...
case 10:
Console.SetCursorPosition(50, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
}
}
static bool keyCheck(int rndNumber, ConsoleKeyInfo key1, ConsoleKeyInfo key2, ConsoleKeyInfo key3, ConsoleKeyInfo key4, ConsoleKeyInfo key5, ConsoleKeyInfo key6, ConsoleKeyInfo key7, ConsoleKeyInfo key8, ConsoleKeyInfo key9, ConsoleKeyInfo key10)
{
bool isPressed = false;
switch (rndNumber)
{
case 1:
if(Console.ReadKey() == key1)
{
isPressed = true;
}
break;
case 2:
if (Console.ReadKey() == key2)
{
isPressed = true;
}
break;
case 3:
if (Console.ReadKey() == key3)
{
isPressed = true;
}
break;
...
case 10:
if (Console.ReadKey() == key10)
{
isPressed = true;
}
break;
}
return isPressed;
}
Is there a better way to do this?
Thanks in advance!
Emanuel
c# game console
put on hold as off-topic by t3chb0t, Heslacher, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
But it only works after multiple presses. - Is this a bug or a feature?
– t3chb0t
yesterday
1
It doesn't look like this is your complete code. Stub code is off-topic.
– Heslacher
yesterday
@t3chb0t It's the main problem.
– Emanuel de Jong
yesterday
@Heslacher The rest of the code is only personalization and making it fancy.
– Emanuel de Jong
yesterday
ok, then this is really a problem because we don't fix bugs...
– t3chb0t
yesterday
|
show 1 more comment
I'm making a finger trainer for a rhythm game with 10 keys.
But it only works after multiple presses.
Either the performance is bad, or you have to press the keys too perfectly.
static void Main(string args)
{
//assigning of the variables
bool keysNotPressed;
string input = String.Empty;
Random rnd = new Random();
ConsoleKeyInfo key1 = new ConsoleKeyInfo();
ConsoleKeyInfo key2 = new ConsoleKeyInfo();
ConsoleKeyInfo key3 = new ConsoleKeyInfo();
...
ConsoleKeyInfo key10 = new ConsoleKeyInfo();
int oldRndNumber1 = 0;
int rndNumber1 = 0;
int rndNumber2 = 0;
int rndNumber3 = 0;
...
int rndNumber9 = 0;
//assigns the ConsoleKeyInfo to the users prefered keys
Console.WriteLine("Give your prefered 10 keybinds");
Console.WriteLine("nKey1:");
key1 = Console.ReadKey();
Console.WriteLine(key1.Key);
Console.WriteLine("nKey2:");
key2 = Console.ReadKey();
Console.WriteLine(key2.Key);
Console.WriteLine("nKey3:");
key3 = Console.ReadKey();
...
Console.WriteLine("nKey10:");
key10 = Console.ReadKey();
Console.WriteLine(key10.Key);
//lets the user choose how many notes (spaces with background color) should be randomly placed
Console.WriteLine("How many notes should come on the screen simultaneously? 1-10");
input = Console.ReadLine().ToLower();
switch (input)
{
case "1":
...
break;
case "2":
...
break;
case "3":
while (true)
{
//generates random numbers representing the notes
oldRndNumber1 = rndNumber1;
do
{
rndNumber1 = rnd.Next(10) + 1;
} while (rndNumber1 == oldRndNumber1); //makes sure no note position will be the same
notePlacer(rndNumber1); //places the note equal to the random generated number
do
{
rndNumber2 = rnd.Next(10) + 1;
} while (rndNumber2 == rndNumber1);
notePlacer(rndNumber2);
do
{
rndNumber3 = rnd.Next(10) + 1;
} while (rndNumber3 == rndNumber1 || rndNumber3 == rndNumber2);
notePlacer(rndNumber3);
//checks if the keys equal to the random generated numbers are all pressed at the same time, until it happens
while (keysNotPressed)
{
if (keyCheck(rndNumber1, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber2, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber3, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10))
{
keysNotPressed = false;
}
}
keysNotPressed = true;
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
}
...
case "10":
...
break;
}
}
static void notePlacer (int rndNumber)
{
switch (rndNumber)
{
case 1:
Console.SetCursorPosition(0, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
case 2:
Console.SetCursorPosition(5, 10);
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine(" ");
break;
case 3:
Console.SetCursorPosition(10, 10);
Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine(" ");
break;
...
case 10:
Console.SetCursorPosition(50, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
}
}
static bool keyCheck(int rndNumber, ConsoleKeyInfo key1, ConsoleKeyInfo key2, ConsoleKeyInfo key3, ConsoleKeyInfo key4, ConsoleKeyInfo key5, ConsoleKeyInfo key6, ConsoleKeyInfo key7, ConsoleKeyInfo key8, ConsoleKeyInfo key9, ConsoleKeyInfo key10)
{
bool isPressed = false;
switch (rndNumber)
{
case 1:
if(Console.ReadKey() == key1)
{
isPressed = true;
}
break;
case 2:
if (Console.ReadKey() == key2)
{
isPressed = true;
}
break;
case 3:
if (Console.ReadKey() == key3)
{
isPressed = true;
}
break;
...
case 10:
if (Console.ReadKey() == key10)
{
isPressed = true;
}
break;
}
return isPressed;
}
Is there a better way to do this?
Thanks in advance!
Emanuel
c# game console
I'm making a finger trainer for a rhythm game with 10 keys.
But it only works after multiple presses.
Either the performance is bad, or you have to press the keys too perfectly.
static void Main(string args)
{
//assigning of the variables
bool keysNotPressed;
string input = String.Empty;
Random rnd = new Random();
ConsoleKeyInfo key1 = new ConsoleKeyInfo();
ConsoleKeyInfo key2 = new ConsoleKeyInfo();
ConsoleKeyInfo key3 = new ConsoleKeyInfo();
...
ConsoleKeyInfo key10 = new ConsoleKeyInfo();
int oldRndNumber1 = 0;
int rndNumber1 = 0;
int rndNumber2 = 0;
int rndNumber3 = 0;
...
int rndNumber9 = 0;
//assigns the ConsoleKeyInfo to the users prefered keys
Console.WriteLine("Give your prefered 10 keybinds");
Console.WriteLine("nKey1:");
key1 = Console.ReadKey();
Console.WriteLine(key1.Key);
Console.WriteLine("nKey2:");
key2 = Console.ReadKey();
Console.WriteLine(key2.Key);
Console.WriteLine("nKey3:");
key3 = Console.ReadKey();
...
Console.WriteLine("nKey10:");
key10 = Console.ReadKey();
Console.WriteLine(key10.Key);
//lets the user choose how many notes (spaces with background color) should be randomly placed
Console.WriteLine("How many notes should come on the screen simultaneously? 1-10");
input = Console.ReadLine().ToLower();
switch (input)
{
case "1":
...
break;
case "2":
...
break;
case "3":
while (true)
{
//generates random numbers representing the notes
oldRndNumber1 = rndNumber1;
do
{
rndNumber1 = rnd.Next(10) + 1;
} while (rndNumber1 == oldRndNumber1); //makes sure no note position will be the same
notePlacer(rndNumber1); //places the note equal to the random generated number
do
{
rndNumber2 = rnd.Next(10) + 1;
} while (rndNumber2 == rndNumber1);
notePlacer(rndNumber2);
do
{
rndNumber3 = rnd.Next(10) + 1;
} while (rndNumber3 == rndNumber1 || rndNumber3 == rndNumber2);
notePlacer(rndNumber3);
//checks if the keys equal to the random generated numbers are all pressed at the same time, until it happens
while (keysNotPressed)
{
if (keyCheck(rndNumber1, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber2, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10) && keyCheck(rndNumber3, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10))
{
keysNotPressed = false;
}
}
keysNotPressed = true;
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
}
...
case "10":
...
break;
}
}
static void notePlacer (int rndNumber)
{
switch (rndNumber)
{
case 1:
Console.SetCursorPosition(0, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
case 2:
Console.SetCursorPosition(5, 10);
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine(" ");
break;
case 3:
Console.SetCursorPosition(10, 10);
Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine(" ");
break;
...
case 10:
Console.SetCursorPosition(50, 10);
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine(" ");
break;
}
}
static bool keyCheck(int rndNumber, ConsoleKeyInfo key1, ConsoleKeyInfo key2, ConsoleKeyInfo key3, ConsoleKeyInfo key4, ConsoleKeyInfo key5, ConsoleKeyInfo key6, ConsoleKeyInfo key7, ConsoleKeyInfo key8, ConsoleKeyInfo key9, ConsoleKeyInfo key10)
{
bool isPressed = false;
switch (rndNumber)
{
case 1:
if(Console.ReadKey() == key1)
{
isPressed = true;
}
break;
case 2:
if (Console.ReadKey() == key2)
{
isPressed = true;
}
break;
case 3:
if (Console.ReadKey() == key3)
{
isPressed = true;
}
break;
...
case 10:
if (Console.ReadKey() == key10)
{
isPressed = true;
}
break;
}
return isPressed;
}
Is there a better way to do this?
Thanks in advance!
Emanuel
c# game console
c# game console
asked yesterday
Emanuel de Jong
174
174
put on hold as off-topic by t3chb0t, Heslacher, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by t3chb0t, Heslacher, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, tinstaafl, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
But it only works after multiple presses. - Is this a bug or a feature?
– t3chb0t
yesterday
1
It doesn't look like this is your complete code. Stub code is off-topic.
– Heslacher
yesterday
@t3chb0t It's the main problem.
– Emanuel de Jong
yesterday
@Heslacher The rest of the code is only personalization and making it fancy.
– Emanuel de Jong
yesterday
ok, then this is really a problem because we don't fix bugs...
– t3chb0t
yesterday
|
show 1 more comment
But it only works after multiple presses. - Is this a bug or a feature?
– t3chb0t
yesterday
1
It doesn't look like this is your complete code. Stub code is off-topic.
– Heslacher
yesterday
@t3chb0t It's the main problem.
– Emanuel de Jong
yesterday
@Heslacher The rest of the code is only personalization and making it fancy.
– Emanuel de Jong
yesterday
ok, then this is really a problem because we don't fix bugs...
– t3chb0t
yesterday
But it only works after multiple presses. - Is this a bug or a feature?
– t3chb0t
yesterday
But it only works after multiple presses. - Is this a bug or a feature?
– t3chb0t
yesterday
1
1
It doesn't look like this is your complete code. Stub code is off-topic.
– Heslacher
yesterday
It doesn't look like this is your complete code. Stub code is off-topic.
– Heslacher
yesterday
@t3chb0t It's the main problem.
– Emanuel de Jong
yesterday
@t3chb0t It's the main problem.
– Emanuel de Jong
yesterday
@Heslacher The rest of the code is only personalization and making it fancy.
– Emanuel de Jong
yesterday
@Heslacher The rest of the code is only personalization and making it fancy.
– Emanuel de Jong
yesterday
ok, then this is really a problem because we don't fix bugs...
– t3chb0t
yesterday
ok, then this is really a problem because we don't fix bugs...
– t3chb0t
yesterday
|
show 1 more comment
1 Answer
1
active
oldest
votes
Some guidelines:
Your code is not easily extensible. If you want to add a key 11, you have to change code throughout.
The key binding can be done in a loop.
I assume that each of your switch cases where you generate the random numbers has the same code copy pasted. Move that to a method.
In fact, you do not need the switch case with a count, you can create a method that accepts the number of numbers you want.
Your random number generation is not very performant. You create numbers until you have one you haven't found yet. This can be done more efficiently by 'shuffling' an array.
Here's some pseudo code with some improvements:
int keyCount = 10;
List<ConsoleKeyInfo> keys = new List<ConsoleKeyInfo>();
for(int i=0; i<keyCount; i++)
{
keys.Add(Console.ReadKey());
}
var numbers = Enumerable.Range(0,keyCount);//Numbers 0->9
numbers = numbers.Shuffle();//Shuffle example: https://www.dotnetperls.com/fisher-yates-shuffle
foreach(var number in numbers)
{
//Check if the key keys[number] is pressed.
}
2
You should avoid answerting off-topic questions and this one is off-topic even for two reasons...
– t3chb0t
yesterday
Thanks a lot! I can't seem to get the shuffle to work though. Should I make a new methode or is there a library for it? Also, do I now loop the for each until every key is pressed simultaneously?
– Emanuel de Jong
yesterday
I added a link in the comment.
– Carra
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Some guidelines:
Your code is not easily extensible. If you want to add a key 11, you have to change code throughout.
The key binding can be done in a loop.
I assume that each of your switch cases where you generate the random numbers has the same code copy pasted. Move that to a method.
In fact, you do not need the switch case with a count, you can create a method that accepts the number of numbers you want.
Your random number generation is not very performant. You create numbers until you have one you haven't found yet. This can be done more efficiently by 'shuffling' an array.
Here's some pseudo code with some improvements:
int keyCount = 10;
List<ConsoleKeyInfo> keys = new List<ConsoleKeyInfo>();
for(int i=0; i<keyCount; i++)
{
keys.Add(Console.ReadKey());
}
var numbers = Enumerable.Range(0,keyCount);//Numbers 0->9
numbers = numbers.Shuffle();//Shuffle example: https://www.dotnetperls.com/fisher-yates-shuffle
foreach(var number in numbers)
{
//Check if the key keys[number] is pressed.
}
2
You should avoid answerting off-topic questions and this one is off-topic even for two reasons...
– t3chb0t
yesterday
Thanks a lot! I can't seem to get the shuffle to work though. Should I make a new methode or is there a library for it? Also, do I now loop the for each until every key is pressed simultaneously?
– Emanuel de Jong
yesterday
I added a link in the comment.
– Carra
yesterday
add a comment |
Some guidelines:
Your code is not easily extensible. If you want to add a key 11, you have to change code throughout.
The key binding can be done in a loop.
I assume that each of your switch cases where you generate the random numbers has the same code copy pasted. Move that to a method.
In fact, you do not need the switch case with a count, you can create a method that accepts the number of numbers you want.
Your random number generation is not very performant. You create numbers until you have one you haven't found yet. This can be done more efficiently by 'shuffling' an array.
Here's some pseudo code with some improvements:
int keyCount = 10;
List<ConsoleKeyInfo> keys = new List<ConsoleKeyInfo>();
for(int i=0; i<keyCount; i++)
{
keys.Add(Console.ReadKey());
}
var numbers = Enumerable.Range(0,keyCount);//Numbers 0->9
numbers = numbers.Shuffle();//Shuffle example: https://www.dotnetperls.com/fisher-yates-shuffle
foreach(var number in numbers)
{
//Check if the key keys[number] is pressed.
}
2
You should avoid answerting off-topic questions and this one is off-topic even for two reasons...
– t3chb0t
yesterday
Thanks a lot! I can't seem to get the shuffle to work though. Should I make a new methode or is there a library for it? Also, do I now loop the for each until every key is pressed simultaneously?
– Emanuel de Jong
yesterday
I added a link in the comment.
– Carra
yesterday
add a comment |
Some guidelines:
Your code is not easily extensible. If you want to add a key 11, you have to change code throughout.
The key binding can be done in a loop.
I assume that each of your switch cases where you generate the random numbers has the same code copy pasted. Move that to a method.
In fact, you do not need the switch case with a count, you can create a method that accepts the number of numbers you want.
Your random number generation is not very performant. You create numbers until you have one you haven't found yet. This can be done more efficiently by 'shuffling' an array.
Here's some pseudo code with some improvements:
int keyCount = 10;
List<ConsoleKeyInfo> keys = new List<ConsoleKeyInfo>();
for(int i=0; i<keyCount; i++)
{
keys.Add(Console.ReadKey());
}
var numbers = Enumerable.Range(0,keyCount);//Numbers 0->9
numbers = numbers.Shuffle();//Shuffle example: https://www.dotnetperls.com/fisher-yates-shuffle
foreach(var number in numbers)
{
//Check if the key keys[number] is pressed.
}
Some guidelines:
Your code is not easily extensible. If you want to add a key 11, you have to change code throughout.
The key binding can be done in a loop.
I assume that each of your switch cases where you generate the random numbers has the same code copy pasted. Move that to a method.
In fact, you do not need the switch case with a count, you can create a method that accepts the number of numbers you want.
Your random number generation is not very performant. You create numbers until you have one you haven't found yet. This can be done more efficiently by 'shuffling' an array.
Here's some pseudo code with some improvements:
int keyCount = 10;
List<ConsoleKeyInfo> keys = new List<ConsoleKeyInfo>();
for(int i=0; i<keyCount; i++)
{
keys.Add(Console.ReadKey());
}
var numbers = Enumerable.Range(0,keyCount);//Numbers 0->9
numbers = numbers.Shuffle();//Shuffle example: https://www.dotnetperls.com/fisher-yates-shuffle
foreach(var number in numbers)
{
//Check if the key keys[number] is pressed.
}
answered yesterday
Carra
21415
21415
2
You should avoid answerting off-topic questions and this one is off-topic even for two reasons...
– t3chb0t
yesterday
Thanks a lot! I can't seem to get the shuffle to work though. Should I make a new methode or is there a library for it? Also, do I now loop the for each until every key is pressed simultaneously?
– Emanuel de Jong
yesterday
I added a link in the comment.
– Carra
yesterday
add a comment |
2
You should avoid answerting off-topic questions and this one is off-topic even for two reasons...
– t3chb0t
yesterday
Thanks a lot! I can't seem to get the shuffle to work though. Should I make a new methode or is there a library for it? Also, do I now loop the for each until every key is pressed simultaneously?
– Emanuel de Jong
yesterday
I added a link in the comment.
– Carra
yesterday
2
2
You should avoid answerting off-topic questions and this one is off-topic even for two reasons...
– t3chb0t
yesterday
You should avoid answerting off-topic questions and this one is off-topic even for two reasons...
– t3chb0t
yesterday
Thanks a lot! I can't seem to get the shuffle to work though. Should I make a new methode or is there a library for it? Also, do I now loop the for each until every key is pressed simultaneously?
– Emanuel de Jong
yesterday
Thanks a lot! I can't seem to get the shuffle to work though. Should I make a new methode or is there a library for it? Also, do I now loop the for each until every key is pressed simultaneously?
– Emanuel de Jong
yesterday
I added a link in the comment.
– Carra
yesterday
I added a link in the comment.
– Carra
yesterday
add a comment |
But it only works after multiple presses. - Is this a bug or a feature?
– t3chb0t
yesterday
1
It doesn't look like this is your complete code. Stub code is off-topic.
– Heslacher
yesterday
@t3chb0t It's the main problem.
– Emanuel de Jong
yesterday
@Heslacher The rest of the code is only personalization and making it fancy.
– Emanuel de Jong
yesterday
ok, then this is really a problem because we don't fix bugs...
– t3chb0t
yesterday