CS1
|

CS:GO Skins and Object Oriented Programming


Portuguese version available here: Skins de CS:GO e Programação Orientada a Objetos.

I once used the enormous esports skin market to explain basic economics (How CS:GO skin market explains basic economics). Now I’m going to use the very same phenomenon to show how this category of online games can relate to Object Oriented Programming.

For that, we’ll use Counter-Strike: Global Offensive, because it is the FPS (First-person shooter) that I’m most familiarized with.

For those who don’t know the game, we can say, in a very simplified way, that a match has players (who can be Terrorists or Counter-terrorists) and weapons (rifles, pistols, knives, grenades, etc.). Players choose their weapons, and with their team, the goal is to eliminate the players of the other team.

Thinking in OOP terms, in this scenario we can see both “Players” and “Firearms” as objects, given that they are “entities” existing in the world (in the game, in this case), endowed with characteristics (attributes) and performing actions (methods).

To better understand how the CS:GO and OOP realms are related, we will use the Java programming language.

CS2
Source: http://wall.alphacoders.com/big.php?i=570388

So let’s go to the game. When we start, all weapons with their respective skins are already given, as this depends on the player’s inventory. So, to represent this in Java, we can create the Skin class (with a showName() method) and three examples of subclasses (Redline, FuelInjector and Asiimov), which evidently inherit the superclass’s method.

public class Asiimov extends Skin {

                @Override

                public void showName() {

                                System.out.println("The skin of choice for this match was: Asiimov\n");

                }

}

However, skins do not stand on their own. For them to exist, they must be applied to a weapon. In this way, we can also create the Firearm class and its subclasses AK47, M4A4 and AWP, which inherit the attributes and methods of the parent class.

public abstract class Firearm {

               

                private double price;

                private String name;

                private Skin skin;

 

                public double getPrice() {

                                return price;

                }

 

                public String getName() {

                                return name;

                }

 

                public Skin getSkin() {

                                return skin;

                }

}
public class AK47 extends Firearm {

 

                private final int magazineAmount = 3;

                private int ammoPerMag = 30;

                private final double price = 2700;

                private final String name = "AK-47";

                private Skin skin;

 

                public AK47(Skin skin) {

                                this.skin = skin;

                }

                …

}

In this example, we have the AK47 class, which in its constructor takes a Skin as a parameter. In this way, we can force the creation of an AK47 object into a skin, so that the weapon, therefore, will always have a texture. Here we consider that even the default skin is considered a skin.

Now let’s deal with the player. To represent it, we create the Player class (with the attributes “nickname”, “cashAmount” and “firearm”), which contains the buyFirearm() method. Here we also have subclasses: Terrorist and CounterTerrorist, the former having the boolean attribute “isC4” and the latter the boolean attribute “isDefuseKit”.

public class Terrorist extends Player {

 

                private boolean isC4;

               

                public Terrorist(String nickname, double cashAmount, boolean isC4, Firearm firearm) {

                                super(nickname, cashAmount, firearm);

                                this.setC4(isC4);

                }

               

                @Override

                public void buyFirearm() {

                                Firearm firearm = this.getFirearm();

                                super.setCashAmount(super.getCashAmount() - firearm.getPrice());

                                System.out.print("The Terrorist " + super.getNickname() + " bought a(n) " + firearm.getName() + ". ");

                }

                …

}

We saw that to create a weapon we must first have created a skin. In the case of the creation of a Player, we now see a similar situation: to create it, we must first have created a weapon — given that every player, when entering a match, already has an inventory with all weapons and their respective skins. This relationship is shown in the Player class constructor, which requires a Firearm parameter.

Now that we have all the project’s classes implemented, we can start a match and see how everything works. For this we have the Match class, which contains the main method.

public class Match {

 

                public static void main(String[] args) {

                               

                                Skin redline = new Redline();

                                Skin asiimov = new Asiimov();

                                Skin fuelInjector = new FuelInjector();

                               

                                AK47 ak1 = new AK47(redline);

                                AK47 ak2 = new AK47(asiimov);

                                AK47 ak3 = new AK47(fuelInjector);

                                M4A4 m41 = new M4A4(asiimov);

                                AWP awp1 = new AWP(asiimov);

                                AWP awp2 = new AWP(redline);

                               

                                Terrorist t1 = new Terrorist("'FalleN'", 5000, true, ak1);

                                Terrorist t2 = new Terrorist("'coldzera'", 8600, false, ak2);

                                Terrorist t3 = new Terrorist("'gla1ve'", 6200, false, awp1);

                                Terrorist t4 = new Terrorist("'valde'", 7800, false, ak3);

                                CounterTerrorist ct1 = new CounterTerrorist("'s1mple'", 16000, false, awp2);

                                CounterTerrorist ct2 = new CounterTerrorist("'olofmeister'", 5000, true, m41);

                               

                                t1.buyFirearm();

                                t1.getFirearm().getSkin().showName();

                               

                                t2.buyFirearm();

                                t2.getFirearm().getSkin().showName();

 

                                t3.buyFirearm();

                                t3.getFirearm().getSkin().showName();

                               

                                t4.buyFirearm();

                                t4.getFirearm().getSkin().showName();

                               

                                ct1.buyFirearm();

                                ct1.getFirearm().getSkin().showName();

                               

                                ct2.buyFirearm();

                                ct2.getFirearm().getSkin().showName();

                               

                                System.out.println("Is Terrorist 1 carrying the C4? " + t1.isC4());

                                System.out.println("\nDoes Counter-Terrorist 1 have a Defuse Kit? " + ct1.isDefuseKit());

                               

                                System.out.println("\nHow many magazines does this AK-47 have? " + ak1.getMagazineAmount());

                                System.out.println("\nHow many ammo per magazine does an M4A4 have? " + m41.getAmmoPerMag());

                                System.out.println("\nWhat is the price of an AWP? " + awp1.getPrice());

                               

                                System.out.println("\nHow much money does Terrorist 2 have after buying his weapon? " + t2.getCashAmount());

                                System.out.println("\nWhat is the nickname of Counter-Terrorist 2? " + ct2.getNickname());

                }

}

When we run this Java Application, we get the following result:

The Terrorist ‘FalleN’ bought a(n) AK-47. The skin of choice for this match was: Redline

The Terrorist ‘coldzera’ bought a(n) AK-47. The skin of choice for this match was: Asiimov

The Terrorist ‘gla1ve’ bought a(n) AWP. The skin of choice for this match was: Asiimov

The Terrorist ‘valde’ bought a(n) AK-47. The skin of choice for this match was: Fuel Injector

The Conter-Terrorist ‘s1mple’ bought a(n) AWP. The skin of choice for this match was: Redline

The Conter-Terrorist ‘olofmeister’ bought a(n) M4A4. The skin of choice for this match was: Asiimov

Is Terrorist 1 carrying the C4? true

Does Counter-Terrorist 1 have a Defuse Kit? false

How many magazines does this AK-47 have? 3

How many ammo per magazine does an M4A4 have? 30

What is the price of an AWP? 4750.0

How much money does Terrorist 2 have after buying his weapon? 5900.0

What is the nickname of Counter-Terrorist 2? ‘olofmeister’

In this program we can notice a few things. As we said above, to create a weapon, we need a skin. In addition to this, for a player to buy a weapon, evidently that weapon must have already been created.

Additionally, we can notice that the same skin can be applied to different weapons — as is the case with Asiimov, which exists on all three rifles — and also that weapons of the same type can have different skins.

AK-47, M4A4 and AWP: Asiimov

Another noticeable thing is the fact that a weapon, after being created, will remain with its skin throughout the entire match, since it is not possible to make this kind of change (the definition of the skin is restricted to the constructor). Therefore, if a player enters a match with a weapon that has a certain skin, every purchase of that weapon made by him throughout the match will only have that skin until the end.

AK-47 and AWP: Redline

In this simple program, it was possible to present how the key concepts of OOP can be related to one of the most played esports in the world. We used encapsulation, polymorphism, and inheritance. With that, we were able to create our objects, respecting the rules of the game.


Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *