C
C#2d ago
Silk&Song

What parts of these should i put in functions?

Im a beginner so i dont know where to begin
45 Replies
Jimmacle
Jimmacle2d ago
you should break code into functions based on some logical task that is getting done for example, attacking or defending could be an Attack or Defend function
TheRanger
TheRanger2d ago
they could make a single class called Human that has the stats defined as properties, and create 2 instances of that class for player and enemy follow the DRY principle
Silk&Song
Silk&SongOP2d ago
put all defense logic in a function? some of the attacking code needs the defense code could you show an example
TheRanger
TheRanger2d ago
First, do u know how to make a class?
Silk&Song
Silk&SongOP2d ago
no
Anton
Anton2d ago
There are 2 easy rules: 1. Group related data into classes or structs 2. Make functions out of repeating code
TheRanger
TheRanger2d ago
Is that a school/uni project?
Silk&Song
Silk&SongOP2d ago
no But im planning to add more to it
Anton
Anton2d ago
Also, send data as parameters, don't use globals
TheRanger
TheRanger2d ago
first you should learn what classes are
Anton
Anton2d ago
Then there are more considerations: 1. Conditions or checks getting too involved or too nested -> pull out a function 2. Same code done on multiple pieces of data -> use an array and a loop 3. Higher and lower level operations mixed up in a single function -> pull out more functions so it's easier to reason about These are the basic things you should master
TheRanger
TheRanger2d ago
dont think fortnite that has 100 players declare their variables like this
int player1Power = 100;
int player1Health = 100;

int player2Power = 100;
int player2Health = 100;

int player3Power = 100;
int player3Health = 100;
and so on
int player1Power = 100;
int player1Health = 100;

int player2Power = 100;
int player2Health = 100;

int player3Power = 100;
int player3Health = 100;
and so on
they use the power of object orientation, and C# is an object oriented level
Anton
Anton2d ago
Note: don't actually get too deep into OOP as a beginner, it's going to be overwhelming unless you know the basics I mentioned above
TheRanger
TheRanger2d ago
classes atleast should be beginner friendly its not like theyre gonna use interface or abstract classes
Anton
Anton2d ago
Well that's how you structure data public fields As a beginner, that's pretty much what you should go for by default
TheRanger
TheRanger2d ago
you could start checking out the links below $helloworld to start learning the basics of C#
Silk&Song
Silk&SongOP2d ago
I did in codeacademy
TheRanger
TheRanger2d ago
it didnt teach you what class is?
Silk&Song
Silk&SongOP2d ago
its teaching abstraction next
TheRanger
TheRanger2d ago
without teaching what a class is first?
Anton
Anton2d ago
learn that it's like one of the most important things what an algorithm is and what an abstraction is
Silk&Song
Silk&SongOP2d ago
ngl I dont really understand algorithims at all
Anton
Anton2d ago
they're related in many ways
Silk&Song
Silk&SongOP2d ago
yes
Anton
Anton2d ago
algorithm means input data, steps to transform it, and output data abstraction basically means giving names to a group of operations or data a variable is an abstraction for accessing a piece of memory it has a name, and it lets you do something indirectly — interact with computer memory through the name
Silk&Song
Silk&SongOP2d ago
it seems to be much more complex that that
Anton
Anton2d ago
it is just this interface + implementation = algorithm that's me rephrasing the same sentence people may start talking about classes and OOP when they hear "abstraction" but classes are not required for it
Silk&Song
Silk&SongOP2d ago
so its just making steps to solve a problem and transfering the steps to code?
Anton
Anton2d ago
That's called solving a problem by writing a program. Algorithm is more or less the product of this It's a bit more general Like, it doesn't have to exist in code It's more the idea of a sequence of steps than a concrete program so an algorithm is basically just the steps
Silk&Song
Silk&SongOP2d ago
im going to put teh attack and defend in there own functions but i feel like its going to cause errors
Anton
Anton2d ago
how so?
Silk&Song
Silk&SongOP2d ago
this code
else if (enemyChoice == 1)
{
if (enemyDefendTR <= 0 & enemyCooldown == 0)
{
enemyDefendTR += 2;
enemyDefense += 2;
Console.WriteLine("The enemy defended and gained 2 defense points!");
Console.ReadKey();
}
else if (enemyDefendTR >= 1 | enemyCooldown == 1)
{
Console.WriteLine($"The enemy attacked and dealt {enemyAttack} damage to you! ");
yourHealth -= enemyAttack - playerDefense;
Console.ReadKey();
if (playerDefenseTR > 0)
{
playerDefenseTR--;

}
else if (playerDefenseTR >= 0)
{

}
else if (enemyChoice == 1)
{
if (enemyDefendTR <= 0 & enemyCooldown == 0)
{
enemyDefendTR += 2;
enemyDefense += 2;
Console.WriteLine("The enemy defended and gained 2 defense points!");
Console.ReadKey();
}
else if (enemyDefendTR >= 1 | enemyCooldown == 1)
{
Console.WriteLine($"The enemy attacked and dealt {enemyAttack} damage to you! ");
yourHealth -= enemyAttack - playerDefense;
Console.ReadKey();
if (playerDefenseTR > 0)
{
playerDefenseTR--;

}
else if (playerDefenseTR >= 0)
{

}
attacks in else if because I gave them a defense cooldown so if it it selects defense again it will still attack so should i still put that in the defense fuction? then do I put the player attack and the enemy attack in the same function? how will the ennemy defend when its in antoher function and idk if the turns will work
Anton
Anton2d ago
honestly I have no idea what you said
Jimmacle
Jimmacle2d ago
you need to structure your code in a way that the information the function needs is accessible
Anton
Anton2d ago
Yes, start from pulling every variable that represents a piece of game state into a single class, this sounds fair public fields your functions can take in the game state as a parameter
Silk&Song
Silk&SongOP2d ago
I dont know how to do that
Jimmacle
Jimmacle2d ago
that's part of software development, taking a problem and turning it into smaller pieces that can connect together you just have to practice it
Silk&Song
Silk&SongOP2d ago
so how do you do that
Anton
Anton2d ago
you see related data, you group it. you see code that repeats, you make a function that takes in what it needs to do the thing. there's more, like recognizing when an abstraction does multiple separate things and restructuring it into multiple abstractions that communicate but start from those two
Silk&Song
Silk&SongOP17h ago
ok im going to tdo that and see if i get errors Ok when do you use classes
Jimmacle
Jimmacle17h ago
whenever you have data and behavior that can be meaningfully treated as one "thing"
Anton
Anton16h ago
when you see related data
Silk&Song
Silk&SongOP4h ago
breaks code since i dont really know how to implement classes though i know how to make them
Anton
Anton1h ago
public class GameState
{
public int PlayerHealth;
// ...
}
public class GameState
{
public int PlayerHealth;
// ...
}
in main you now do
var state = new GameState();
state.PlayerHealth = 100;
//...
var state = new GameState();
state.PlayerHealth = 100;
//...
and replace playerHealth for state.PlayerHealth

Did you find this page helpful?