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 5 years ago.
Improve this question
I'm new.
my problem:
class Battle
{
public void Fight(string charOne, string charTwo)
{
void CurrentHealth()
{
Console.WriteLine(charOne.name + "'s Health: " +
charOne.currentHealth + "/" + charOne.maxHealth +
" | " + charTwo.name + "'s Health: " + charTwo.currentHealth +
"/" + charTwo.maxHealth);
}
}
}
the ".name" and ".currentHealth" are not working.
charOne would be a class instance, and charTwo would be another class instance.
[UPDATE BELOW]
Below was the code that was originally working in the main program class, and I was trying to move it in to a class, where I could pass in some parameters to the class, so the Hero could fight other monsters in the future. This single time monster was "mosquito".
I just want to be able to replace a "mosquito" with a "wolf" class and maybe "hero2" instead of "hero". Hope this makes more sense.
void CurrentHealth()
{
Console.WriteLine(hero.name + "'s Health: " + hero.currentHealth + "/" + hero.maxHealth + " | " + mosquito.name + "'s Health: " + mosquito.currentHealth + "/" + mosquito.maxHealth);
}
void Attack(int dmg)
{
Console.WriteLine(hero.name + " Dealt " + dmg + " damage to " + mosquito.name + ".");
}
while (mosquito.currentHealth > 0 && hero.currentHealth > 0)
{
Console.Clear();
int damage = hero.AttackAction();
mosquito.currentHealth = mosquito.currentHealth - damage;
int newHealth = mosquito.currentHealth;
Attack(damage);
CurrentHealth();
Console.WriteLine();
Console.WriteLine("Press any key to continue.");
Console.Read();
}
if (mosquito.currentHealth <= 0)
{
Console.WriteLine("Right on! You are Victorious!!!");
}
else if (hero.currentHealth <= 0)
{
Console.WriteLine("Oh no.. you are dead...");
}
else
{
Console.WriteLine("You reached the end.");
}
public void Fight(MyCharacterClass charOne, MyCharacterClass charTwo)
Try it like this, of course change your own class with MyCharacterClass
Related
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!
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 6 years ago.
Improve this question
Is their a way to add each variable by colum instead of individually adding each one?
Like if I wanted to multiply a row the code won't just take every variable and multiply it. I want the code to keep them in the same place and print it to the screen in the same format.
Console.WriteLine(x + " " + y + " " + z);
Console.WriteLine(a + " " + b + " " + c);
Console.WriteLine(ab + " " + bb + " " + cb);
Something like this?
Console.WriteLine("{0,10}{1,10}{2,10}", x, y, z);
See here.
static void Main(string[] args)
{
int x = 1;
int y = 2;
int z = 3;
ConsoleWriteColumn(x,y,z);
Console.WriteLine("Multiplied by 2:");
ConsoleWriteColumn((x*2),(y*2),(z*2));
Console.ReadKey();
}
static void ConsoleWriteColumn(int value1, int value2, int value3)
{
Console.WriteLine($"{value1,5}{value2,5}{value3,5}");
}
Console.WriteLine($"{x}\t{y}\t{z}");
Console.WriteLine($"{a}\t{b}\t{c}");
Console.WriteLine($"{ab}\t{bb}\t{cb}");
Note: This format is supported as from C# 6 and up.
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;
}
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
I'm 100% new to programming, this is what I want:
(Name) No numbers
(Card Number) Limit to 16 digits and no letters
(Expiry Date) Numbers like this - "02/17" and no letters
(Security Code) Limit to 3 numbers and no letters.
My code:
string message =
"Name: " + nameTextBox.Text +
"\nCard Number: " + cardNumberTextBox.Text +
"\nExpiry Date: " + expiryDateTextBox.Text +
"\nSecurity Code: " + securityCodeTextBox.Text +
"\nOrder: Pizza " + pizzaType + ", " + pizzaSize;
if (TotalToppingQuantities() > 0)
{
for (int toppingIndex = 0; toppingIndex < toppingQuantities.Length; toppingIndex++)
{
if (toppingQuantities[toppingIndex] > 0)
{
message += ", " + toppingQuantities[toppingIndex] + " x " +
toppingNames[toppingIndex];
}
}
}
message +=
"\nPickup Spot: " + pickupSpot +
"\nDelivery Time: 30 minutes";
MessageBox.Show(message);
For your problem, regex is good solution.
And this should work for you
using System.Text.RegularExpressions;
//====================================
if (Regex.Match(nameTextBox.Text, "\\d").Success)
{
MessageBox.Show("(Name) must contain No numbers");
return ;
}
if (!Regex.Match(cardNumberTextBox.Text, "^\\d{16}$").Success)
{
MessageBox.Show("(Card Number) must be Limited to 16 digits and no letters");
return ;
}
if (!Regex.Match(expiryDateTextBox.Text, "^\\d{2}/\\d{2}$").Success)
{
MessageBox.Show("(Expiry Date) must be Numbers like this - 02/17 and no letters");
return ;
}
if (!Regex.Match(securityCodeTextBox.Text, "^\\d{3}$").Success)
{
MessageBox.Show("(Security Code) must be Limited to 3 numbers and no letters.");
return ;
}
This question already has answers here:
How to sort an array containing class objects by a property value of a class instance? [duplicate]
(2 answers)
Closed 7 years ago.
I Have an array like below code :
struct Book_Struct
{
public string Title;
public string Auther;
public int Date;
public int ID;
}
static void Print(Book_Struct[] a, int b)
{
for (int i = 0; i < b; i++)
{
Console.WriteLine(" Name of Book " + (i + 1) + " is : " + "\" " + a[i].Title + " \"");
Console.WriteLine("Auther of Book " + (i + 1) + " is : " + "\" " + a[i].Auther + " \"");
Console.WriteLine(" Date of Book " + (i + 1) + " is : " + "\" " + a[i].Date + " \"");
Console.WriteLine(" ID of Book " + (i + 1) + " is : " + "\" " + a[i].ID + " \"");
Console.WriteLine("\n---------------------------------\n");
}
}
I want sort this array based on for example Title of Books. How do i it?
You could use Array.Sort:
Array.Sort(a, (b1, b2) => b1.Title.CompareTo(b2.Title));
or LINQ:
a = a.OrderBy(book => book.Title).ToArray();
The latter needs to recreate the array.
As an aside, use a class instead of a mutable struct.
Use LINQ's OrderBy to sort the array:
a = a.OrderBy(x => x.Title).ToArray();
Reference