How do I find the highest integer in a list of values?
Im making a card game with C# and each card is 4 values which can change based on other cards played and I want to find the highest value of the 4 then compare the highest one against another varying integer but have no clue how to do any of it.
Answer by 5c4r3cr0w · Jul 29, 2016 at 04:49 AM
Here is simplest code to find highest number from list
public List<int> list = new List<int> ();
int GetMax(){
list.Sort ();
int max = list [list.Count - 1];
return max;
}
Could you explain the code in further detail, I'm not sure if I understand it.
Is it like
public List<int> Card = new List ( );
int Get$$anonymous$$ax ( ) {
Card.sort ( );
int max = Card [Card.Count - 1];
return max;
}
or something different?
Yes. you got this right. But let me still explain. You can add any number of cards in your list. Here card is your list.
Now sort() function arranges numbers in ascending order so the after calling Card.sort() largest value would be at last index of list. so Card[ Card.Count - 1] will be the largest number.
Now you need to call Get$$anonymous$$ax() in order to get this value like :
int $$anonymous$$axNumber = Get$$anonymous$$ax();
Thanks!
Also I have questions about other parts of this card game and I'm wondering if you can answer them?
First off is that in my game thiers field card and effect cards that affect the integers in the list by adding and subtracting from them depending on which one is used and I was wondering what the is the easiest way to incorporate that into this script
public int fire;
public int water;
public int air;
public int earth;
private int f;
private int w;
private int a;
private int e;
void start ( ) {
f = fire;
w = water;
a = air;
e = earth;
void restart ( ) {
fire = f;
water = w;
air = a;
earth = e;
And also I have no clue how to make a customizable deck and also how to make it so that it deals cards in random order but never deals the same card twice into the hand at any given time.
Your answer
Follow this Question
Related Questions
Money-system for decillionare game 1 Answer
Add "1" to score text? Not "11111"? 1 Answer
how can i check if two numbers are close to each other? 1 Answer
Using very big Numbers 0 Answers