Class structure | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Class structure

HI all, I'm just starting to try to get the concept do the game in my head down into the simple heirechy so far I have Player class that I want to be able to attack () defend (), and move I want the player to be a fleet which is made up of ships also enemy's will be basically a player without the ability to move so do I make player the base class with fleets inheriting from that and in turn ships inheriting from fleets? and do I make enemies a child of Player even though it isn't really a player?

12th Sep 2017, 2:11 PM
Dan Blaine
Dan Blaine - avatar
21 Answers
+ 2
I would suggest making a Command class that has the attack and defend functions in it. then have a child Player class with the move func and any other attributes you would like to define this class. then make a child Enemy class that takes on any attributes you want for it specifically. This is just another way to think about it and keeps the Player and Enemy classes completely separate from each other. hope it helps some.
12th Sep 2017, 2:26 PM
sproutecks25
sproutecks25 - avatar
+ 2
I'm going to make 5 different kinds of ships with a variable that holds an int for attack and one for defense I want my players attack to be based on the number of ships and types of those ships so would ships be a child of fleet so then given your post I would have a command class with the children player, enemy, and fleet which has a child ships?
12th Sep 2017, 2:33 PM
Dan Blaine
Dan Blaine - avatar
+ 1
I'll make the ships a function of fleets and enemy and player children of fleets I think this will work for me thanks
12th Sep 2017, 2:41 PM
Dan Blaine
Dan Blaine - avatar
+ 1
i would make the fleet class separate and make each child ship class of fleet. you would then need the attack func to call the parent fleet and then child ship for each player/enemy. and each ship would have the attack number and defense number based on size, power, etc. im not gonna lie kinda passing my realm of expertise. im still learning this stuff, and im sure this is only one way to do it.
12th Sep 2017, 2:45 PM
sproutecks25
sproutecks25 - avatar
+ 1
what language are you writing in?
12th Sep 2017, 2:46 PM
sproutecks25
sproutecks25 - avatar
+ 1
c#
12th Sep 2017, 2:55 PM
Dan Blaine
Dan Blaine - avatar
+ 1
thats what i was gonna suggest. im gonna follow this cause im curious how this works out.
12th Sep 2017, 2:55 PM
sproutecks25
sproutecks25 - avatar
14th Sep 2017, 11:19 AM
Dan Blaine
Dan Blaine - avatar
+ 1
lol every day I look at this code and see a better way https://code.sololearn.com/cg80kRMVg2Uj/?ref=app
15th Sep 2017, 11:13 AM
Dan Blaine
Dan Blaine - avatar
+ 1
class Unit { attack() { console.log("Attack!"); } } //inherit this class from Unit class Gunner extends Unit{ attack() { //complete the function super.attack(); { console.log("Using gun!"); } } } //inherit this class from Unit class Sniper extends Unit { attack(){ //complete the function super.attack(); { console.log("Using sniper rifle!"); } } } let gunner = new Gunner(); let sniper = new Sniper(); //calls gunner.attack(); sniper.attack(); Good Luck
25th Jan 2022, 8:59 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
great answer ill work it out and hopefully post some code soon
12th Sep 2017, 2:56 PM
Dan Blaine
Dan Blaine - avatar
0
I'll be using my unity game engine but I don't think that will change my code drastically
12th Sep 2017, 3:00 PM
Dan Blaine
Dan Blaine - avatar
0
here is the beginning of my ships class, obviously I'll be adding a bunch more ship types and I have been starting to think that the fleet class will hold an array of ships () and then return the total defense or attack at the end
14th Sep 2017, 11:17 AM
Dan Blaine
Dan Blaine - avatar
0
I'm thinking now that I look closer.at that code I only need to pass in the number of ships because the other stuff is constant
14th Sep 2017, 11:24 AM
Dan Blaine
Dan Blaine - avatar
0
its looking good so far.
14th Sep 2017, 11:33 AM
sproutecks25
sproutecks25 - avatar
0
oh wow that link updates the code as I do, very nice solo learn. Anyway I'm just formatting that to run with my unity game engine so it may look weird from here on out
14th Sep 2017, 11:59 AM
Dan Blaine
Dan Blaine - avatar
0
ah, that feeling when you work the bugs
15th Sep 2017, 1:17 PM
Dan Blaine
Dan Blaine - avatar
0
you code is looking good. any chance you can comment it to help see your thought process
15th Sep 2017, 3:54 PM
sproutecks25
sproutecks25 - avatar
0
yes, sorry that's my bad habit
16th Sep 2017, 4:54 PM
Dan Blaine
Dan Blaine - avatar
0
my next project is setting up a battle system, I have already made the controls for flying the spaceship and I think the rest of the help I need falls out of the scope that can be done on this app
16th Sep 2017, 5:02 PM
Dan Blaine
Dan Blaine - avatar