Creating a known length array in C# - c#

I am currently following a tutorial on making a game in Unity using C#. In it, they showed us how to show the score in the center of the screen using
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.z.ToString("0");
}
}
However, I want to go further and figure out how to make a high score list in the top left corner. My only experience is with python so I know I would go about making an empty array of size 10, and then each time the game restarts, if the score is greater than any of the highest 10 scores in the mentioned list, it is inserted in the corresponding position (keeping the elements ordered), and the lowest value is dropped from the list (to keep a list of just 10 elements). However, I am confused about the syntax of C#.
Currently, my thought process for the code (which would go on my restart statement), if it were python is this
##this is how I would write it in python I believe
array = np.zeros[10]
for i in range(0, len(array)):
if player.position.z.ToString("0") < array[-i]
continue
else:
array[-i-1] = player.position.z.ToString("0")
Where obviously the player.position statement is from c#. I was wondering if anyone could help me translate the thought process over. It seems I need to declare an array of strings before I can use it but I am not sure.
Thanks for any help

As I understood you want an array with 10 elements where you store the top 10 scores. So each new score which is higher than the existing top 10 is placed in the correct position of that top 10.
Something like
current Top10 => 2, 3, 5, 7, 8, 9, 12, 15, 18, 20
newScore = 16
new Top10 => 3, 5, 7, 8, 9, 12, 15, 16, 18, 20
Option 1:
string[] scoreArray = new string[10];
//Initializing the score array
for (int i = 0; i < 10; i++)
{
scoreArray[i] = 0;
}
//Somewhere in your code a new score will be assigned to this variable
int newScore;
//Checking potentially higher score
boolean replaceFlag = false;
for (int i = 0; i < 10; i++)
{
if(newScore > scoreArray[i])
{
replaceFlag = true;
}else{
//newScore lower than lowest stored score
if(i == 0)
{
//Stop the loop
i = 11;
}else if(replaceFlag){
//The rest of the elements in the array move one position back
for(int j = 0 ; j < i-1; j++ )
{
scoreArray[j] = scoreArray[j+1];
}
//Finally we insert the new score in the correct place
scoreArray[i-1] = newScore;
}
}
}
Option 2: Using List
//Create and initialize list of scores
List<int> listScores = new List<int>{ 0,0,0,0,0,0,0,0,0,0};
// If later, at some point we have the following scores 2, 3, 5, 7, 8, 9, 12, 15, 18, 20
//When you get a new score (our list will have)
listScores.Add(newScore);
//After inserting 2, 3, 5, 7, 8, 9, 12, 15, 18, 20, 16
//Ordering the list in descending order
listScores.Sort()
//Now we have 2, 3, 5, 7, 8, 9, 12, 15, 16, 18, 20,
//Drop last element of the list to keep the fixed size of 10.
listScores.RemoveAt(0)
//Now we have 3, 5, 7, 8, 9, 12, 15, 16, 18, 20
//In case you want to have the list in descending order
listScores.Sort((a, b) => -1* a.CompareTo(b));

if Score is the array, then add the scores to this Score array variable and then sort and reverse this array whenever u want to update the high score.
private void UpdateScore(int[] ScoreArray)
{
Array.Sort(ScoreArray);
Array.Reverse(ScoreArray);
}

You would create a known array size of 10 like this:
string[] aStringArray = new string[10];
Also add this at top;
using System.Collections.Generic;
Arrays - C# Programming Guide

Array must have length, if you want unknown size then use List
List<int> player = new List<int>();
player.Add(1);

Related

Create array based on another arrays C#

I am trying to create an array based on the ones that I have. I have two arrays which look like this
sample array A: {AB01, AB01, AB01, AB02, AB02, AB02, AB03, AB01}
sample array B: {10, 10, 20, 10, 20, 20, 40, 10}
Both arrays are dynamic so provided data is just a sample data. Same indexes in both arrays creates a pair: so for example: pair 1: AB01 10 , pair 2: AB01 10 and so on. Based on those two arrays I want to create third array which will represent indexes but including duplicates. So I want my third array to look like this:
array (3) C: {10, 10, 20, 30, 40, 40, 50, 10}
FYI I cannot use LINQ 4+
I tried nested loop over the arrays but I do not get the expected result.
for(int i = 0; i < arrayA.Length; i++){
for(int j = i + 1; j < arrayA.Length; j++){
if(arrayA[i] == arrayA[j] && arrayB[i] == arrayB[j]){
arrayC.Insert(i, 10 * (i+1));
}
else
arrayC.Insert(i, 10 * (i+2));
}
}
As I mentioned I expext my array C to be indexes of each element but I want to include duplicates:
array (3) C:
{10, 10, 20, 30, 40, 40, 50, 10}
EDIT:
As the result is not clear.
If there was no duplicates in the arrays I would want my array C to look like this:
{10, 20, 30, 40, 50, 60, 70, 80}
It means that each pair eg(AB01, 10) will be given index 10 in array C.
But when it comes to duplicates array C will look different.
Pair AB01 - 10 appears as first so I want to give it index 10. And because it appears three times at position: 0, 1 and 7 I want number 10 to be at those positions in array C. Next on I have pair AB01 - 20 it appears only one time at position 2 and values are different than in pair to so I want to give it index 20. And so on.
As written in the comments, a re-formulation of the task is:
for each pair, if it has been encountered before, use the already
allocated "index", otherwise create a new one as (previous index) +
10.
You need to remember the already assigned indexes, e.g. in a Dictionary<(string, int), int>, mapping a pair to the index. In addition. you need to keep track of the highest index assigned so far.
var arrayA = new string[] { "AB01", "AB01", "AB01", "AB02", "AB02", "AB02", "AB03", "AB01"};
var arrayB = new int[] {10, 10, 20, 10, 20, 20, 40, 10};
var assignedIndexes = new Dictionary<(string, int), int>();
var highestIndex = 0;
var result = new int[arrayA.Length];
for (var i = 0; i < arrayA.Length; i++)
{
var tuple = (arrayA[i], arrayB[i]);
if (!assignedIndexes.TryGetValue(tuple, out var index))
{
index = highestIndex + 10;
assignedIndexes.Add(tuple, index);
highestIndex = index;
}
result[i] = index;
}
Console.WriteLine(string.Join(", ", result));
Prints: 10, 10, 20, 30, 40, 40, 50, 10
If you cannot use ValueTuples, too, replace them by Tuples:
var assignedIndexes = new Dictionary<Tuple<string, int>, int>();
var tuple = Tuple.Create(arrayA[i], arrayB[i]);

C#: loop through array of numbers and put consecutive numbers in one array, and all other numbers else in other array

I have an unsorted array of numbers. Most numbers in this array are consecutive (1, 2, 3, 4, 5,...) but sometimes there's a number in it that's higher than the rest.
int highestNumber = int.MinValue;
int secondHighestNumber = int.MinValue;
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 1207, 7, 8, 9, 1803, 10, 11, , 9000, 9001 };
int counter = 0;
foreach (int number in arr) {
if (number > highestNumber) {
secondHighestNumber = highestNumber;
highestNumber = number;
}
else if (number > secondHighestNumber) secondHighestNumber = number;
}
When I run the code, it'll tell me 9001 is the highest number and 9000 the second highest. But I want to change it so that I get an array of all the numbers from 1 to 11, with all the consecutive numbers that only have a difference of one, and a second array with all the larger numbers: 1207, 1803, 9000 and 9001.
9000 and 9001 shouldn't be in the first array, because the difference between these numbers and the rest is too high. So the end result should look like this:
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int[] arr2 = new int[] { 1207, 1803, 9000, 9001 };
Some background info on what the purpose of this is:
Cient creates articles and each article has an article number that is created by looking up what the highest article number is in the "articles" table so far (e.g. 20), then the number of the new article is that number + 1 (e.g. 21); So an increment per number. However the client wishes to be able to sometimes add articles and input the article number he desires for the article, so let's say for the last article he created he inputted 7000 as article number. If he he then creates another article after that, I have to continue with that list of incrementing numbers. So unless the client changes the value of the next article again, the next new article should have 22 as article number (and not 7001).
Any help would be greatly appreciated as I've been stuck on trying to figure this out for a while now.
I'm not sure if you are just wanting to get the next number you should use or you want the 2 sets. If you want the two sets, I would do this:
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 1207, 7, 8, 9, 1803, 10, 11, , 9000, 9001 };
int counter = 0;
List<int> l1 = new List<int>();
List<int> l2 = new List<int>();
foreach (int number in arr) {
if(l1.Count==0 || l1[l1.Count-1] == (number -1))
l1.Add(number);
else
l2.Add(number);
}
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 1207, 7, 8, 9, 1803, 10, 11, 9000, 9001 };
// Sort the array
Array.Sort(arr);
// Start at the beginning and check if the index matches the actual number.
// Computer begins at 0, so fix that
for (int index=0;index<arr.Length; index++)
{
if (arr[index] != index+1)
{
Console.WriteLine("Next ID is " +(index+1));
break;
}
}

Round robin array in C#

I am trying to make an round robin array. If I were to feed this array a bunch of values in a loop and I hit the end of the array (let's say it is 10 in length), how would I loop around to the first index?
When looping, use modular algebra to compute array's index:
myArray[index % myArray.Length]
Sample
int[] myArray = new int[10];
// Round robin feeding; trying to put 0, 1, .. 13 into int[10]:
for (int i = 0; i < 14; ++i) // note 14 > 10
myArray[i % myArray.Length] = i; // i % myArray.Length - modular arithmetics
// Test
// 10, 11, 12, 13, 4, 5, 6, 7, 8, 9
// note that first 4 values (10, 11, 12, 13) are overriden
Console.Write(String.Join(", ", myArray));

Array to Sort High Scores

I'm working on a project in C# that involves keeping track of the top five high scores for a rock paper scissors game. Right now I have an array to hold the top five scores (which are integers), I sort the array in descending order, and I use a for loop to compare the score just earned by the user to the scores currently in the array. If the new score is higher than one in the array, right now the new score just takes the space in the array that the lower one occupied.
For example, if the scores were 9, 8, 5, 3, 1 and the user scored a 6, the scores would then look like this: 9, 8, 6, 3, 1. I wondered if there's a way for me to shift the lower scores over and insert the new one, so the list would look like this: 9, 8, 6, 5, 3.
This is the code I currently have, where successPercent is the score, calculated as wins divided by losses and ties:
int[] scoreArray = { 84, 25, 36, 40, 50 };
Array.Sort(scoreArray);
Array.Reverse(scoreArray);
for (int x = 0; x <= scoreArray.Length; ++x)
{
if (successPercent > scoreArray[x])
{
scoreArray[x] = Convert.ToInt32(successPercent);
break;
}
}
Something like this can do the trick:
Create temporary list
Add new score
Sort it by descending order
Take top 5...
int[] scoreArray = { 84, 25, 36, 40, 50 };
var tempList = new List<int>(scoreArray );
int newScore = ...;//Get the new score
tempList.Add(newScore);
scoreArray = tempList.OrderByDescending(x=>x)
.Take(5)
.ToArray();
You can do this without creating a new list.
[Algo]: Replace the smallest number by the new number and then sort!
int[] scoreArray = { 5, 3, 9, 8, 1 };
int new_number = 6;
//Replaces smallest number by your new number
int min_index = Array.IndexOf(scoreArray, scoreArray.Min());
scoreArray[min_index] = new_number;
Array.Sort(scoreArray);
Array.Reverse(scoreArray);
I believe your way is correct and more effient than creating redundant lists, just you are calling the Reverse method unnecessarily.Instead, leave your elements sorted in ascending order, then loop through the array, and sort it in descending order.
int[] scoreArray = { 84, 25, 36, 40, 50 };
int userScore = 100;
Array.Sort(scoreArray);
for (int x = 0; x <= scoreArray.Length; ++x)
{
if (userScore > scoreArray[x])
{
scoreArray[x] = Convert.ToInt32(userScore);
break;
}
}
Array.Sort(scoreArray,(x,y) => y.CompareTo(x));
Note: My first solution was throwing away the second highest score so I have deleted it.

Take a random item from one list and add it to another list

So...
I have two lists. One is created by adding numbers 1-48 into it. The other one is empty at the beginning:
int max = 35;
int[] Numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48};
List<int> allNumbers = new List<int>();
List<int> newNumbers = new List<int>();
I use this method to fill up the allNumbers list:
for (int i = 0; i < Numbers.Length - 1; i++)
{
allNumbers.Add(Numbers[i]);
}
My application is supposed to select a random item in the allNumbers list and add it to the newNumbers list:
Random r = new Random();
int now;
for (int i = 1; i < max; i++)
{
now = r.Next (1, allNumbers.Count);
allNumbers.Remove(now);
newNumbers.Add(now);
This works (as in it doesn't crash or anything) but it doesn't remove the random number, i.e. I usually get the same number in my newNumbers list more than once (e.g. I could get 11, 4, 5, 5, 11, 27, 33, 4 etc.).
This is supposed to be a game somewhat similar to lottery so there can't be any double numbers. How would I go about to select a random list item from the allNumbers list, add it to the newNumbers list and then remove it from the allNumbers list? So that when the for loop goes through it again, the allNumbers.Count would be smaller by 1 and the number I previously selected is not in the allNumbers list any more.
Any help appreciated!
That is because you are selecting a random index, removing the value at that index and then adding that index as a value to the newNumbers list.
Using this snippet should fix it:
for (int i = 0; i < max; i++)
{
now = r.Next(0, allNumbers.Count);
newNumbers.Add(allNumbers[now]);
allNumbers.RemoveAt(now);
}
The problem is that you are adding the number generated by the Random.Next method, instead of the content of the list on the random index.
replace your code with:
newNumbers.Add(allnumbers[now]);
That should do the trick.
First of all the method you use to fill the allNumbers list is to much effort for something that can be done using the ToList() method on the array and assign it to allNumbers, like this:
List<int> allNumbers = Numbers.ToList();
The use the random to take the index from the list and add the value of the item at that index and insert it into the newNumbers list. Then you can use the index to remove it from the allNumbers list.
int index;
for (int i = 0; i < max; i++)
{
index = r.Next (0, allNumbers.Count);
newNumbers.Add(allNumbers[index]);
allNumbers.RemoveAt(index);
}
You're randomizing an index with now = r.Next(allNumbers.Count);, then you try removing this index as a value. Change the above line (the index starts at 0!) and use the RemoveAt method instead of Remove.
the now variable represents an index (a position) inside the list of numbers.
So you need to retrieve the number corresponding to this index, using the [] operator.
Then you can add this number to newNumbers list, and after you can remove the same number from allNumbers using the RemoveAt method, passing the index as parameter.
for (int i = 0; i < max; i++)
{
now = r.Next(0, allNumbers.Count);
newNumbers.Add(allNumbers[now]);
allNumbers.RemoveAt(now);
}
If you prefer, you can also use the Remove method, passing the number to remove as parameter, and not the index.
allNumbers.Remove(allNumbers[now]);
be careful with the indexes, in C# they start from 0.

Categories