Reputation: 37
I am a beginner at C#. I am trying to deal equal amounts of cards to 2 players in a card game. I have to divide my array( of cards ) into two so that each player will have the same amount of cards. I have already made use of my GetImages()
to randomize ALL(which is all 40) images into 1 pictureBox
. So next I will have to split it into two and put them into the pictureBox
when my Form loads.
Any help will be much appreciated, thanks.
Here is my code:
public string[] GetImages(){
string[] img = new string[40];
img[0] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\1.png";
img[1] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\2.png";
img[2] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\3.png";
img[3] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\5.png";
img[4] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\6.png";
img[5] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\7.png";
img[6] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\9.png";
img[7] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\10.png";
img[8] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\11.png";
img[9] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\13.png";
img[10] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\14.png";
img[11] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\15.png";
img[12] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\17.png";
img[13] = @"C:\Users\Student\Documents\APPD\Asgmt\Asgmt\Assignment\Assignment\handcards\18.png";
//so on and so forth
return
img;
}
public void Form2_Load(object sender, EventArgs e)
{
//the code I'm trying now:
string[] Player1Array = GetImages(40/2);
Upvotes: 0
Views: 153
Reputation: 838
Alexei is dead on. If we want to get even more sophisticated you can do something like this:
public static List<string[]> Partition(this string[] source, Int32 size)
{
var output = new List<string[]>();
for (int i = 0; i < Math.Ceiling(source.Count() / (Double)size); i++)
output.Add(source.Skip(size * i).Take(size).ToArray());
return output;
}
inspired from this C# - elegant way of partitioning a list?
So to use it, all you have to do is this:
var chunkSize = 40 / 4;
var chunks = GetImages().GetImages(chunkSize);
and every element in chunk is segment of the original. How many strings are in each chunk depends on the parameter for the chunk size. 40/4 will get you 4 chunks. first item contains the first 10. second contains the next 10, so on to you get to the last 10. This way you can easily segment the arrays anyway you need.
Upvotes: 0
Reputation: 91
First, you have to recognize a problem in your code: you cant use the getImages function with parameters ( I am referring to 40/2 ), because its not defined with parameters.
I have a different suggestion for randomizing the cards. Pass two arrays to the getImg function, which will assign the cards randomly to player1. Then randomly to player2 according to an array that contains the numbers from 0 - 39 arranged randomly. This link will help you in getting an idea of how to generate a random unique set of numbers (it uses lists, but you safely replace that with an array):
https://codereview.stackexchange.com/questions/61338/generate-random-numbers-without-repetitions
So the getImg function will be something like that:
public string [] GetImages(string [] player1Array, string [] player2Array){
int [] randomArray=getRandomNembers(); //this call depends on how you define the function
player1Array[randomArray[0]]=" your location here";
// so on until
player1Array[randomArray[19]]=" your location here";
//then you start with player 2
player2Array[randomArray[20]]=" your location here";
//and so on
}
Upvotes: 0
Reputation: 100547
Assuming you just need to split an array (or List
) of elements into first/second half - usually one would use Enumerable.Take
/Enumerable.Skip
followed by Enumerable.ToArray
to convert to array if necessary:
string[] Player1Array = GetImages().Take(20).ToArray();
string[] Player2Array = GetImages().Skip(20).ToArray();
Upvotes: 5