What parts of these should i put in functions?
Im a beginner so i dont know where to begin
84 Replies
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
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
put all defense logic in a function? some of the attacking code needs the defense code
could you show an example
First, do u know how to make a class?
no
There are 2 easy rules:
1. Group related data into classes or structs
2. Make functions out of repeating code
Is that a school/uni project?
no
But im planning to add more to it
Also, send data as parameters, don't use globals
first you should learn what classes are
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
dont think fortnite that has 100 players declare their variables like this
they use the power of object orientation, and C# is an object oriented level
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
classes atleast should be beginner friendly
its not like theyre gonna use interface or abstract classes
Well that's how you structure data
public fields
As a beginner, that's pretty much what you should go for by default
you could start checking out the links below $helloworld to start learning the basics of C#
I did in codeacademy
it didnt teach you what
class
is?its teaching abstraction next
without teaching what a class is first?
learn that
it's like one of the most important things
what an algorithm is
and what an abstraction is
ngl I dont really understand algorithims at all
they're related in many ways
yes
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
it seems to be much more complex that that
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
so its just making steps to solve a problem and transfering the steps to code?
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
im going to put teh attack and defend in there own functions but i feel like its going to cause errors
how so?
this code 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
honestly I have no idea what you said
you need to structure your code in a way that the information the function needs is accessible
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
I dont know how to do that
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
so how do you do that
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
ok im going to tdo that and see if i get errors
Ok when do you use classes
whenever you have data and behavior that can be meaningfully treated as one "thing"
when you see related data
breaks code since i dont really know how to implement classes
though i know how to make them
in main you now do
and replace playerHealth for state.PlayerHealth
ok after 5 days i present this:https://github.com/TakenGit2/Turn-Based-C-Console-App/blob/master/turnbasedc%23/Program.cs
GitHub
Turn-Based-C-Console-App/turnbasedc#/Program.cs at master · TakenGi...
My turn based c# console app for practicing code and just having fun making new mechanics - TakenGit2/Turn-Based-C-Console-App
Nice but why make a class called Enemy if it behaves the exact same as the Player class
u could just do
Player Enemy = new Player();
so they can have diffrent special abilites but that dosent work because to make the abilty worj it has to be in both classes
u have methods in both classes that do the exact same thing
This is where you can create a parent class for both so you can inherit these methods from the parent class so you dont have to write them down twice
i just made one befroe
global stats
wait how
Angius
REPL Result: Success
Console Output
Compile: 438.858ms | Execution: 75.058ms | React with ❌ to remove this embed.
havent seen
Unga Bunga
for a while 😄My go-to lmao
look up
Inheritance
i tried but i just get errors like theseInvalid token '(' in class, record, struct, or interface member declaration
The name 'Enemy.Attack' does not exist in the current context.
The name 'Attack' does not exist in the current context.
Tuple must contain at least two elements.
Invalid token ';' in class, record, struct, or interface member declaration
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
There is no argument given that corresponds to the required parameter 'player' of 'GlobalStats.takeThrust(int, Player)'
There is no argument given that corresponds to the required parameter 'player' of 'GlobalStats.takeThrust(int, Player)'
There is no argument given that corresponds to the required parameter 'player' of 'GlobalStats.takeThrust(int, Player)'
There is no argument given that corresponds to the required parameter 'player' of 'GlobalStats.takeThrust(int, Player)'
There is no argument given that corresponds to the required parameter 'player' of 'GlobalStats.takeThrust(int, Player)'
Argument 1: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 1: cannot convert from 'Enemy' to 'ConsoleAppTurnBased.Player'
Argument 2: cannot convert from 'ConsoleAppTurnBased.Player' to 'Enemy'
'Player' does not contain a definition for 'youcantStab' and no accessible extension method 'youcantStab' accepting a first argument of type 'Player' could be found (are you missing a using directive or an assembly reference?)
Argument 1: cannot convert from 'ConsoleAppTurnBased.Player' to 'Enemy'
Argument 1: cannot convert from 'ConsoleAppTurnBased.Player' to 'Enemy'
GitHub
Turn-Based-C-Console-App/turnbasedc#/Enemy.cs at master · TakenGit2...
My turn based c# console app for practicing code and just having fun making new mechanics - TakenGit2/Turn-Based-C-Console-App
why?
why d instead of Defense?
why do you have private fields?
use named arguments in the constructor, or use
required
and public fieldswym
there's no point to inheritance if the enemy doesn't add its own fields
to the shared set of fields
and even then, inheritance is bad, which you will learn eventually after you've screwed yourself with it a couple of times
i made class globalstats and i put all stats in there and then i made player and enemy inherit
doesn't make sense, just give them each a globalstats field
i did
do some simple procedural programming
specific static functions for enemy, specific ones for the player
I did before i tried to do the inheritance stuff
ok stick to that
it's better
especially when you're a beginner
yeah i did that in github
well maybe you didn't push or something
there's nothing of sorts in the repo you linked
I just see duplicated code
well i already did that before i started trying to use oop
ok, keep doing procedural, don't do oop
why
because you don't need it yet
and it's going to confuse you
well when am i going to need it
when you understand why you would want to encapsulate data
when you're able to see good abstractions
when you need polymorphism
When there's lots of public data in a single place, it's going to get difficult to ensure you only change it in some well defined way
encapsulation will help with that
put bits of data in containers that provide a well-defined set of ways to access it or modify it
encapsulation basically means that the data may only be changed through those methods and not directly
and the type that houses the methods and the data is referred to as an abstraction
polymorphism is for when you want to do the strategy pattern
think an effect that triggers on pickup
or a function that decides what action to take
it can be a multitude of things
like on pickup it might add a stat, might create a passive effect, might apply a curse
you can model this with a switch
but the benefit to using an interface for this is that the implementation could have its owen data
in a switch, you'd have to store and pass along that data
for either all implementations, or the current one
but most of you code should be procedural
now, it could be instance methods instead of static methods, but it's mostly going to be just lists of steps of stuff to do with some context
While im begginer
in general
I'm not a beginner
95-ish percent of my code is procedural
isnt c# whole thing is oo?
well c# being oop just means it has syntax for these things
it doesn't mean you have to use it when it's not going to be useful
you can do oop without the syntax
it's just less convenient
people do oop in C
the only thing that doesn't have an exact equivalent is just private data I think
you have to jump through hoops for that in C
sym von nbys bupy xczzlyhn julugynylm mi nbun qihn qile
where?
the Attack method for example
the parameters can just have the type of the parent class
the only other difference i see in its body is if (paraChance == 1 & paraTurns > 0) and if (paraChance == 4 & paraTurns > 0)
this can be replaced with for example
if (paraChance == GetParaChance() && paraTurns > 0)
, the GetParaChance can be an abstract or a virtual method that can get overridden in the subclasses, returning a different value for each
you can even divide your code in that method into multiple methods
for instance extracting the whole switch statement of switch (AttackNumbers)
into a method called PrintAttackNumbers()
so even if you want to keep the method in both subclasses, atleast extract a piece of code like the switch (AttackNumbers) into a method, and call it on the Attack method of both classes&&
cmhn w# qbify nbcha cm ii?
hgag ;ok; 'asoliu 'klksf&* sd!
F alkq rkabopqxka elt jxkv ibqqbop afa vlr bkzlab? rpb 3 pl f zxk rkabopqxka
wym?
Ouqx rhe... ixkj kh ijkfyt qii veh Y huqbb owuj ed oekh tkcr qiydzyzyzyzkdd
i will just make an class for abilites
what did i just read
since Player and Enemy inherit from class Character, u can pass instances of Player or Enemy to the Parameter of type Character