0
\$\begingroup\$

enter image description here I have a bool[,] Array for keeping track of Nations that are either at war(true) or neutral(false).

The problem Im having is that when going over the matrix, to display al wars in UI elements, it will actually shwo duplicates of all wars, this is ofcourse obvious because: Nation 1 is at war with Nation 2. Nation 2 is at war with Nation 1. So it will ofcourse display both, but I just want one.

So how do I remove/ignore the duplicates?

Heres my code for generating the matrix:

It wont let me post images or code.


    public static void Init(Nation[] nationsArray)
    {
        nations = new List<Nation>(nationsArray);
        int numNations = nations.Count;

        enemyMatrix = new bool[numNations - 1, numNations - 1];

        for (int i = 0; i < numNations - 1; i++)
            nations[i].ID = i;
    }

    public static bool AreNationsEnemies(Nation nation1, Nation nation2)
    {
        return enemyMatrix[nation1.ID, nation2.ID];
    }

    public static void SetAsEnemies(Nation nation1, Nation nation2)
    {
        enemyMatrix[nation1.ID, nation2.ID] = true;
        enemyMatrix[nation2.ID, nation1.ID] = true;

        numberOfActiveWars++;
    }
    public static void SetAsNeutral(Nation nation1, Nation nation2)
    {
        enemyMatrix[nation1.ID, nation2.ID] = false;
        enemyMatrix[nation2.ID, nation1.ID] = false;

        numberOfActiveWars--;
    }
    }

So thats the problem, it displays the wars doubly so!

I appreciate any help, thanks in advance!

\$\endgroup\$
6
  • \$\begingroup\$ I had to add all that random stuff because it simply wouldnt allow me to post, what a whack experience, took me literally 45 minutes \$\endgroup\$ Commented Nov 13, 2022 at 17:30
  • 1
    \$\begingroup\$ You should probably show your code for iterating over the matrix. \$\endgroup\$
    – DMGregory
    Commented Nov 13, 2022 at 17:46
  • \$\begingroup\$ @DMGregory, II didnt actually iterate over it, instead I kept track of all wars that a nation was in, inside that Nation, so each Nation as a List<Nation> atWarWith; I scrapped that and instead now have a custom class called War.cs, which is created everytime a war is started using @Olivier's answer below. This seems to be the cleanest solution. So then I have a List<War> that is filled up using the answer below, and I just iterate over that List of custom classes(War) to fill in the UI elements. \$\endgroup\$ Commented Nov 13, 2022 at 19:27
  • \$\begingroup\$ if (enemyMatrix[x, y]) { War war = new War(nations[x], nations[y]); wars.Add(war); \$\endgroup\$ Commented Nov 13, 2022 at 19:29
  • \$\begingroup\$ You probably noticed that code does not look legible in a comment. When you want to show code, edit your question. \$\endgroup\$
    – DMGregory
    Commented Nov 13, 2022 at 19:34

1 Answer 1

0
\$\begingroup\$

Consider only one half of the matrix. You can do it with

// Reading out the nations having war by eliminating the duplicates
for (int x = 0; x < numNations; x++) {
    for (int y = 0; y < x; y++) {
        // Here y is always < x, so we get e.g. x = 5, y = 3 but never x = 3, y = 5
        if (enemyMatrix[x, y]) {
             TODO: add nations nations[x], nations[y]
        }
    }
}

or, if you want y to be greater than x

   for (int y = x + 1; y < numNations; y++) {

Btw. the declaration of the matrix should be

enemyMatrix = new bool[numNations, numNations]; // Without the -1

Because in C# you specify the size of the matrix dimensions and not the upper bounds as in VB.


The matrix is automatically created with all the positions set to false. So, SetAsNeutral might not be necessary, unless you must change the values later.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Awesome! This is exactly what I was looking for, no if statement spaghetti required :) Im gonna try it out as soon as I can! \$\endgroup\$ Commented Nov 13, 2022 at 18:51
  • 1
    \$\begingroup\$ Works like a charm! Thanks! I will accept your answer.. \$\endgroup\$ Commented Nov 13, 2022 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .