-2

I want to write a card exchange game. I have an Arraylist of Colors in which values start from -1 to 6. Each element of this array implies id colors. There is also an ArrayList Player Properties in which cards with colors are stored. I need to create an ArrayList with eight zeros that will imply id Colors. Next, skip the ArrayList Player Properties through this array and if there is some element in the array, then increase the ArrayList Colors index by +1.

Eg:

ArrayList Colors =new ArrayList(){-1,0,1,2,3,4,5,6};
ArrayList Player Properties = new ArrayList(){0,4,6};
This array should imply that its element is an element of the Colors array
ArrayList buffer = new ArrayList(){0,0,0,0,0,0,0,0};
If an array comes to the input in this form: {0,4,6};
Then the index of this array is added by +1, like this: {0,1,0,0,0,1,0,1}; 

How do I implement this in code?

I don't have any code examples because I don't know how to implement it, sorry in advance that I didn't provide more code, I really need help, thank you all in advance for your help

3 Answers3

0

What you want is a histogram of the card values that you accumulate.

Since this is C# you better start thinking of a data model for each player. There are two arrays to consider, one with the card values: -1,0,..6 and one with the card counts, how many of each card a player has.

Here is how this would play out:

static class Program
{
    static void Main(string[] args)
    {
        var player1 = new Player("Player1");

        Console.WriteLine(player1);            
        // Player1 : 0,0,0,0,0,0,0,0

        player1.Hand(0,4,6);
        Console.WriteLine(player1);
        // Player1: 0,1,0,0,0,1,0,1
    }
}

You can to this point with the following Player class that handles the logic inside the .Hand() method.

public class Player
{
    public static readonly int[] CardValues = { -1, 0, 1, 2, 3, 4, 5, 6 };
    public Player(string name)
    {
        CardCounts = new int[CardValues.Length];
        Name = name;
    }
    public int[] CardCounts { get; }
    public string Name { get; }
    public void Hand(params int[] cards)
    {
        foreach (var card in cards)
        {
            int index = Array.IndexOf(CardValues, card);
            if (index >= 0)
            {
                CardCounts[index] += 1;
            }
        }
    }
    public override string ToString()
    {
        return $"{Name} : {string.Join(",", CardCounts)}";
    }
}

Note that ArrayList is really old technology, and not type safe. In it place there is List<>, but in this case the length of the array is fixed since there is a finite set of card values, and that does not change, so no need for a collection, as a plain old Array would suffice.

JAlex
  • 1,486
  • 8
  • 19
  • *and not type safe* - well, that's not really true.. – Caius Jard Nov 30 '21 at 08:22
  • @CaiusJard you can show me how to do it right, just writing a comment is not the best thing, I'm a beginner and I may not understand everything that is written in the comments – Yama Lomama Nov 30 '21 at 10:36
  • I wasn't saying that JAlex's answer is wrong, just that it's a bit misleading to say that ArrayList isn't type safe. I do agree that ArrayList should not be used. I don't have any problem with the main content of the answer, though I think I would have shortcut it a bit. – Caius Jard Nov 30 '21 at 11:44
  • @YamaLomama - to do it "right" there is a lot more that can be done here. For one I would ditch the integers to distinguish card colors, and define an `enum` with the values. The methods under `System.Enum` then allow you to convert between symbols, integers and strings. Add a `[Description()]` attribute and you can have a user readable string for use in the UI. – JAlex Nov 30 '21 at 13:22
  • @CaiusJard - how is `ArrayList` type safe? It casts everything into `object`, even value types and it is up to the consumer to handle the type conversions manually. – JAlex Nov 30 '21 at 14:22
  • What does "type safety" mean, to you? – Caius Jard Nov 30 '21 at 14:48
  • @JAlex Thank you, you helped me a lot, it's clear that you understand C# very well, thank you for your time on this question! – Yama Lomama Nov 30 '21 at 14:49
  • @CaiusJard - https://stackoverflow.com/q/2437469/13813219 – JAlex Nov 30 '21 at 15:21
  • @JAlex https://stackoverflow.com/questions/17984499/is-arraylist-is-typesafe-or-strongly-typed/17984521#17984521 – Caius Jard Nov 30 '21 at 15:46
0

Because your array is a fixed representation:

var Colors = new []{-1,0,1,2,3,4,5,6};
var PlayerProperties = new []{0,4,6};
var buffer = new []{0,0,0,0,0,0,0,0};

You can calculate which indexes in the buffer should be set to 1

foreach(var pp in PlayerProperties)
  buffer[pp+1] += 1;

Value 0 always occurs at index 1. Value 4 always occurs at index 5. Value 6 always occurs at index 7. Thus the formula is value+1..

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

You can use the Linq function Contains to check if a value is present in an array. Something like this:

    var colors = new int[] { -1, 0, 1, 2, 3, 4, 5, 6 };
    var playerProperties = new int[] { 0, 4, 4, 6 };
    var result = colors.Select(c => playerProperties.Where(x => x == c).Count());
    Console.WriteLine(string.Join(", ", result));

prints 0, 1, 0, 0, 0, 2, 0, 1

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • I think the intent is to keep adding 1 in the place values where properties match. So next time around the result could be for example `0, 1, 0, 0, 1, 2, 0, 2` if the properties where `{3, 4, 6}`. – JAlex Nov 30 '21 at 14:25
  • @JAlex yes, if an array containing {1,1,2,4} suddenly arrives, will the index of the number one change to two? I need to increase in this case if there are repetitions – Yama Lomama Nov 30 '21 at 14:30
  • @JAlex is it possible to modify this option to work for me? I understand that you need to do everything right, but how to do this functionality based on the already available data? if I start doing everything right, then something may break in the process and then come to redo a lot – Yama Lomama Nov 30 '21 at 14:32
  • @YamaLomama I updated the answer to give the count of occurences in playerProperties – Hans Kilian Nov 30 '21 at 14:45
  • @HansKilian Thank you so much! This solution suits me very well. I am glad that there are people like you in the world! All the best to you! You're the best! – Yama Lomama Nov 30 '21 at 14:48
  • This answer is no different to what JAlex and I posted – Caius Jard Nov 30 '21 at 14:50