War game on console

Nelson_EU Posted messages 3 Status Membre -  
KX Posted messages 19031 Status Modérateur -
Hello,

I had to create a game for school that consists of a war between two teams of three warriors. Each team selects a warrior from its three, and then they fight until one has no health points left. The game stops when one of the two teams has no warriors left alive.

My game is mostly finished, I added a few things like each warrior having a privilege (attack bonus, healing, etc.). All warriors start with one privilege that they can use during the game. If it is used, the warrior no longer has it anymore. If the warrior dies without using their privilege, the opposing warrior receives that privilege (if they don't already have it).

So far, everything works. I played a game and it runs smoothly.

The problem comes from the fact that when I restart a game, for example, the warrior who finished with 2 privileges in the previous game comes back with those two privileges, whereas they should only have one (since it's the beginning of a new game).

The privileges of a warrior are represented in the form of a boolean array.

Any help? (I will post the code if necessary) Thanks!!

2 réponses

KX Posted messages 19031 Status Modérateur 3 020
 
Hello,

I don't know how you set up the new game system, but if you're experiencing these kinds of side effects, it's probably because you've tried to erase the data from the previous game and you're missing a piece. The simplest and safest approach would have been to not reuse the data from the first game but to recreate it entirely, just like you did the first time.

For example, you have a Game class with a boolean[] privileges attribute; you have a Game object g; and a reset() method that loops through privileges to set everything back to false. This kind of code isn't great. It's better to forget the Game object g and completely rebuild it with a new Game() for each game, both the first and the following ones.
--
Trust does not exclude control.
1
Nelson_EU Posted messages 3 Status Membre
 
Hi! Thanks for your reply. I tried to add a reset method so that at the end of the game everything resets, but it still doesn't work :/
Basically, I have a Warrior class (from which I instantiate my various warriors: barbarian, archer, etc.), a TeamWarriors class (from which I instantiate my two teams), and a Combat class that contains the main method, the class that really "manages" the game. My new games are not therefore instantiated; I have to run the program to start a game...
Edit: My privileges table is located in the Warrior class, it is specific to each warrior and is part of the constructor (when I create, for example, my barbarian, I give him a name, a number of HP, and the number of his privilege).
0
Nelson_EU Posted messages 3 Status Membre
 
I created a reset() method that is directly called in the main when the program starts.

However, this method does not work. Why?
 public static void reset(){ barbare.setPrivileges(2); archer.setPrivileges(3); mage.setPrivileges(3); geant.setPrivileges(1); dragon.setPrivileges(1); sorciere.setPrivileges(2); }

Privilege method:
public void setPrivileges(int privilege) { for (int i = 0; i < tableDesPrivileges.length; i++) { if (i == privilege - 1) { this.privileges[i] = true; } else { this.privileges[i] = false; } } }


And in the main, I called the method like this: EquipeGuerriers.reset();

Any idea what the problem might be?
0
KX Posted messages 19031 Status Modérateur 3 020
 
My explanation may not have been clear yesterday:
For example, you have [...] a reset() method that goes through privileges to set everything back to false. This kind of code is not great. It's better to forget the Game object g, and rebuild it completely with a new Game() [...]

When I look at your EquipeGuerriers.reset(); method, I see that it is static, meaning that you are not constructing an object to have a team, but that the class is your team and that's not good. This means that barbarian, archer, mage, etc. are also static, hence the side effects.

It’s better to have an EquipeGuerrier object, which you instantiate with a new EquipeGuerriers(), that initializes the privileges of barbarian, archer, mage, etc. locally to that instance of EquipeGuerriers, so that when you restart your game, you create a new EquipeGuerriers built from scratch, without relying on previous modifications.

In general, static attributes in a class are supposed to be relatively infrequent; they are useful in certain cases, but they should not replace non-static attributes related to instances of the class.
0