I am a newbie and I am stuck in this problem. I can the players stats, scores and names but I can't make the scoreboard work properly. I have worked 2 days trying to figure it out now I am asking you guys.
I have top 10 scoreboard but I cant make the placement. Higher score should have higher placement.
This is my code:
int PlayerCount = PlayerSystem.Players.Count;
if(PlayerCount == 1)
{
Score[0].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
}
if (PlayerCount == 2)
{
if(PlayerSystem.Players[0].Score > PlayerSystem.Players[1].Score)
{
Score[0].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
Score[1].text = PlayerSystem.Players[1].Name + ": " + PlayerSystem.Players[1].Score.ToString();
}
else if(PlayerSystem.Players[1].Score > PlayerSystem.Players[0].Score)
{
Score[1].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
Score[0].text = PlayerSystem.Players[1].Name + ": " + PlayerSystem.Players[1].Score.ToString();
}
}
I commented over 200 lines of code because it didnt worked. But I hope you get the idea. Thank you if you readed my post. I really apreciate it if you help me how to do it. Thank you.
First sort the player score.
using System.Linq;
....
List<Player> Players = PlayerSystem.Players.OrderByDescending(p=>p.Score).ToList();
Then assign the scores in a loop to your GUI.
for(int i=0; i<10; ++i)
{
var player = PlayerSystem.Players[i];
Score[i].text = player.Name + ": " + player.Score;
}
Related
Hi guys I need help solving a c# problem. This is the code being run as you can see it repeats 1bottles and 1 bottle on the picture I would like to know how to remove the "1 bottles on the wall take one down" and keep "1 bottle on the wall" please help
class Program
{
static void Main(string[] args)
{
string strBottles = "bottles";
int bottles = 100;
while (bottles <= 100)
{
Console.WriteLine(bottles + strBottles + " on the wall" + " take one down");
if(bottles == 1)
{
strBottles = "bottle";
Console.WriteLine(bottles + strBottles + " on the wall");
break;
}
bottles--;
}
}
}
You need to check the value before the first message printing, add an if check like so,
if(bottles>1)
Console.WriteLine(bottles + strBottles + " on the wall" + " take one down");
Choosing a possible infinite do/while with a break condition that can or cannot be fullfilled is a bad idea
I will try this aproach:
string strBottle = "bottle";
int iBottles = 100;
for (int bottles = iBottles; bottles >=1; bottles--)
{
Console.WriteLine(bottles + strBottle + (bottles>1?"s":"") +" on the wall" + (bottles > 1 ? " take one down":""));
}
Think about what the code is doing when bottles == 1.
First it hits this line
Console.WriteLine(bottles + strBottles + " on the wall" + " take one down");
then it enters the if statement:
if(bottles == 1)
{
strBottles = "bottle";
Console.WriteLine(bottles + strBottles + " on the wall");
break;
}
If you want only the "1bottle on the wall", then the if statement needs to come first:
while (bottles > 0)
{
if(bottles == 1)
{
Console.WriteLine($"{bottles} bottle on the wall");
break;
}
Console.WriteLine($"{bottles} bottles on the wall take one down");
bottles--;
}
Other improvements:
while (bottles <= 100) - bottles starts at 100 and decreases so this statement will always be true. If you changed the initial number of bottles to int bottles = 101 the code wouldn't execute the while loop in your version. You never want it to go below 1 so the condition should be while (bottles > 0)
You don't really need the string strBottles, it's redundant and can be removed
If you're using C# 6.0 or above take a look at string interpolation. It provides a more readable way to format strings with expressions: $"{bottles} bottles on the wall"
I'm new to c# and don't completely understand for loops. I am working on a text-based wizard game recommended by brackeys on youtube. At the start of the game, you are asked to chose your spell, let's say "bugs(costs 13mana, deals 2dmg)" you have 3 spell slots and 10hp. When it's time to attack calculations are done etc to reduce the enemy's(frog(10hp)) hp. So after the frog attacks, I want to start over the battle(loop) for me to attack again and the frog to attack again. And the game will be over when either I or the enemy has 0hp. I know for sure I'm coding right but the way I'm using classes and functions are too much and I bet there are simpler ways to do it but I just don't know.
I'm not sure this project is too big for a beginner but yea lol. I was thinking of using a for loop but I don't completely understand how it works.
When I run it only loops the part that asked which spell you want to use and minuses your mana and spell slots. It only loops that and doest attack Also, I'm kinda new to this site so I don't know the proper way of laying out my work.
class Wizard
{
//Wiz Profile
public string name;
public int myHp = 10;
public int myMana = 20;
public string userAttack;
public string favoriteSpell;
private int spellSlots;
private float xp;
public static int Count;
/////////////////////////
//Spells Profile
public int bugsDmg = 2;
public int bugsMana = 13;
public int rottingFodderDmg = 3;
public int rottingFodderMana = 10;
public int owlDmg = 5;
public int owlMana = 10;
//////////////////////////////////////////
//Enemies Profile
public int frogHp = 10;
//Here is where I attempted to make the loop
public void Gameover()
{
while(myHp > 0){
Gameactive();
}
}
//I don't use this I just got this in brackets tutorial I will use it when
getting user name and spell not really sure of to use functions taking stuff
in the brackets
public Wizard(string _name, string _favoriteSpell)
{
name = _name;
favoriteSpell = _favoriteSpell;
spellSlots = 3;
xp = 0f;
Count++;
}
//This is done after the spell calculation is done
public void Battle()
{
userAttack = Console.ReadLine();
if((userAttack == "Bugs") || (userAttack == "bugs"))
{
CastSpell();
frogHp =frogHp - bugsDmg;
Console.WriteLine("You casted " + userAttack + ". Dealth " + bugsDmg + "
damage!");
Thread.Sleep(5000);
Console.WriteLine("----------------------------------------");
Console.WriteLine("Frog hp: " + frogHp +"hp" + " Your hp: " + myHp + "hp Your
mana: " + myMana + "mana");
Console.WriteLine("Spell Slots: " + spellSlots + " slots left");
Console.WriteLine("----------------------------------------");
Thread.Sleep(3000);
}
if( (userAttack == " Rotting Fodder") || (userAttack == "rottingfodder") ||
(userAttack == "rotting fodder"))
{
CastSpell();
frogHp =frogHp - rottingFodderDmg;
Console.WriteLine("You casted " + userAttack + ". Dealth " + rottingFodderDmg +
" damage!");
Thread.Sleep(5000);
Console.WriteLine("----------------------------------------");
Console.WriteLine("Frog hp: " + frogHp +"hp" + " Your hp: " + myHp + "hp Your
mana: " + myMana + "mana");
Console.WriteLine("Spell Slots: " + spellSlots + " slots left");
Console.WriteLine("----------------------------------------");
Thread.Sleep(3000);
}
}
//This is called when an enemy(the frog) attacks
public void Enemyattack()
{
myHp = myHp - rottingFodderDmg;
Console.WriteLine("You got hit with rotting fodder");
Thread.Sleep(2000);
Console.WriteLine("Dealth" + rottingFodderDmg + " damage!");
Thread.Sleep(3000);
Console.WriteLine("----------------------------------------");
Console.WriteLine("Frog hp: " + frogHp +"hp Your hp: " + myHp + "hp Your
mana: " + myMana + "mana");
Console.WriteLine("Spell Slots: " + spellSlots + " slots left");
Console.WriteLine("----------------------------------------");
}
//This is the calculation that happens when a player chooses a spell
public void CastSpell(){
if((userAttack == "Bugs") || (userAttack == "bugs"))
{
myMana = myMana - bugsMana;
spellSlots--;
Console.WriteLine("-15 mana, -1 Spell Slot");
Thread.Sleep(5000);
}
if( (userAttack == " Rotting Fodder") || (userAttack == "rottingfodder") ||
(userAttack == "rotting fodder"))
{
myMana = myMana - rottingFodderMana;
spellSlots--;
Console.WriteLine("-15 mana, -1 Spell Slot");
Thread.Sleep(5000);
}
Gameover();
}
public void Meditate(){
Console.WriteLine(name + " meditates to regain spell slots.");
spellSlots = 2;
}
////////////////I assumed by making this into a func. I will be able to loop the part
///that takes user input then to attack again
public void Gameactive()
{
Console.WriteLine("Select Your Spell to attack: \nBugs(costs 15 mana) Rotting
Fodder(costs 4 mana)");
Battle();
Thread.Sleep(5000);
Console.WriteLine("Standby Phase...");
Thread.Sleep(2);
Console.WriteLine("Frog is attacking...");
Thread.Sleep(2000);
Enemyattack();
}
}
class Program
{
static void Main(string[] args)
{
Console.Title = "WizardMadness";
//Making a new wizard
string userName;
string userGender;
string userFavoriteSpell;
Wizard player1 = new Wizard(/*userName*/"Aaron", "Bugs" /*userFavoriteSpell*/);
Console.WriteLine(Wizard.Count + " wizard created.");
Console.WriteLine("Time to go into battle");
Thread.Sleep(3000);
Console.WriteLine("You ran into a Frog!");
Thread.Sleep(2000);
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("Frog hp: 10hp Your hp: 10hp Your mana: 20mana\nSpell Slots:
3 slots available");
Console.WriteLine("--------------------------------------------------");
Thread.Sleep(5000);
player1.Gameactive();
Welcome to programming! There are a few things you can do to up your code and clean it up a little.
Use .ToLower() for your if statements
Consider using a DO WHILE loop for your game state
You can NEST your if statements
if
{
if
{
}
else if
{
}
else
{
}
}
Using libraries will clean up your code, separating your methods and functions can really help you trouble shoot and debug as well. Making your "main" 30 lines instead of 300
Learn what an if statement actually does. Think of it like this, your program runs top to bottom, so once it goes through that statement it is done, unless there is a loop to reprompt and make it go through it again, but once that statement is cleared, it will not be seen again ( you can nest your if statements in your do while loops)
Hope this helps! Not complete answers, but programming is about logic, and once you figure it out it will click, good luck!
so I currently have a program that adds an item to a list in a format such as
username,Index it then adds one to the index in this code below however. It is only adding one to the item that has been added most recently.
Console.WriteLine("There are currently: " + AntiSpam.Count);
int Index = 0;
foreach (string s in AntiSpam)
{
Console.WriteLine("Found User: " + s.Split(',')[0]);
AntiSpam[Index] = s.Split(',')[0] + "," + (int.Parse(s.Split(',')[1]) + 1).ToString();
Index++;
}
Basically this returns the data There are currently: 10
Found User: someone. It then goes again for another loop of this code and shows the same result again.
EDIT
I have managed to make my code work by using this code
for (var i = 0; i < AntiSpam.Count; i++)
{
AntiSpam[i] = AntiSpam[i].Split(',')[0] + "," + (int.Parse(AntiSpam[i].Split(',')[1]) + 1).ToString();
Console.WriteLine("Text is {0}", AntiSpam[i]);
}
However if possible I would like to know why this works and the first doesn't
If you're going to be indexing a list, just do for rather than foreach. This avoids the need (and possible confusion) of using a separate variable to keep track of the AntiSpam.IndexOf(s) which is basically what you were trying to do with Index:
Console.WriteLine("There are currently: " + AntiSpam.Count);
int index;
string s;
for(int i=0; i < AntiSpam.Count, i++)
{
string[] parts = AntiSpam[i].Split(',');
username = parts[0];
Console.WriteLine("Found User: " + username);
if (parts.Length > 1)
{
index = int.Parse(parts[1])
AntiSpam[i] = username + "," + (index + 1).ToString();
}
}
I'm trying to read out the contents off a Setting inside my Application. Below is the code i'm having troubles with:
private bool checkGrid()
{
string playlists = Spotify_Extender.Properties.Settings.Default.Playlists;
MessageBox.Show(playlists);
string[] split1;
if (playlists.Contains(";"))
{
MessageBox.Show("Multiple Links");
split1 = playlists.Split(';');
}
else
{
MessageBox.Show("One Link");
split1 = new string[1];
split1[0] = playlists;
}
MessageBox.Show("Array Length: " + split1.Length);
int lines = 0;
for (int i = 0; i < split1.Length; i++)
{
MessageBox.Show("Check #" + i + " - " + split1[i] + " - Length: " + split1[i].Length);
if (split1[i].Length >= 22)
{
MessageBox.Show(i + " - " + split1[1]);
lines++;
}
}
int rows = this.playlistGrid.Rows.Count;
MessageBox.Show(lines + "");
if (rows == lines)
return true;
return false;
}
The code should be easy to understand and it should work as far as i am aware, but it doesn't. I entered this in my Setting:
If i run the program now, my first MessageBox prints out exactly what i entered, the second one prints out "One Link" and the third prints "Array Length: 1". Now we get to the part i'm having troubles with. The next Message is this:
So the length of the text is 22 as displayed in the MessageBox, but down below this statement isn't true:
if (split1[i].Length >= 22)
I'm really confused by this and it also does this when i check this:
if (split1[i] != "")
Any help is appreciated, because i don't know what to do, since my code should be fine. Thanks for your time!
You should have split[i] and not split[1]
I am cycling through the contents of a two-dimensional array containing the result of a Punnett Square calculation for gene crosses. I need to summarize the result so that the user can readily see the unique instances. I can accomplish this by putting the result into a text box, but when I try and use a ListBox to display the data, part of the information is getting lost, namely a translation of the AaBBCc type data to something that directly relates to the traits that the user initially selected.
This is the main block of code for the operation:
foreach (string strCombination in arrUniqueCombinations)
{
int intUniqueCount = 0;
decimal decPercentage;
foreach (string strCrossResult in arrPunnettSQ)
{
if (strCrossResult == strCombination)
{
intUniqueCount++;
}
}
decPercentage = Convert.ToDecimal((intUniqueCount*100)) / Convert.ToDecimal(intPossibleCombinations);
txtReport.AppendText(strCombination + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%."+ Environment.NewLine);
lstCrossResult.Items.Add(DecodeGenome(strCombination) + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%.");
}
For appending the data to the textbox I use this code and it works perfectly:
txtReport.AppendText(DecodeGenome(strCombination) + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%."+ Environment.NewLine);
Giving the result:
Trait 1 Het.,Trait 3 appears 16 times or 25%.
For adding the result to a list box, this works:
lstCrossResult.Items.Add(strCombination + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%.");
Giving the result:
AaBBCc appears 16 times or 25%.
But the contents of strCombination is AaBBCc and I need it translated to "Trait 1 Het.,Trait 3", which I accomplish with this bit of code:
private string DecodeGenome(string strGenome)
{
string strTranslation = "";
int intLength = strGenome.Length;
int intCounter = intLength / 2;
string[] arrPairs = new string[intLength / 2];
//Break out trait pairs and load into array
for (int i = 1; i <= intLength; i++)
{
arrPairs[i / 2] = strGenome.Substring((i-1),2);
i++;
}
foreach (string strPair in arrPairs)
{
char chFirstLetter = strPair[0];
char chSecondLetter = strPair[1];
intCounter = intCounter - 1;
if (Char.IsUpper(chFirstLetter))
{
if (!Char.IsUpper(chSecondLetter))
{
if (intCounter > 0)
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)) + " Het.,");
}
else
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)));
}
}
}
else
{
if (!Char.IsUpper(chSecondLetter))
{
if (intCounter > 0)
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)) + ",");
}
else
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)));
}
}
}
}
return strTranslation;
}
That has no problem displaying in a text box, but when I try and put it as an item into a list box it turns it into null. Instead of:
"Trait 1 Het.,Trait 3 appears 16 times or 25%."
I get:
" appears 16 times or 25%."
I have tried adding the results to an ArrayList, then populating the listbox after everything is processed, but the result is the same.
Any clues as to why the list box is not accepting the translated AaBBCc information would be greatly appreciated.
strTranslation is never set. Everything is pushed to txtReport.AppendText