Random function

For my final project I am supposed to design a game of War. My main question is, after reading a file (Deck of 52) and initializing an array to have all the number cards. My question is, how do you split that array into two random hands?

Here's a sample of my code in case it is needed, but I just need to know how the random function works. And if there's any suggestions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* 
This program will make a game of war between two players
*/

#include<stdio.h>
#include<math.h>
int main(void)
{
	int i,j; //iterators
	int Deck[13][4];//array for the Deck of 52
	int P1[13][2];//Player 1 array
	int P2[13][2];//Player 2 array

	FILE* inCards;//file to read
	inCards=fopen("cards.txt", "r");
	if(inCards==NULL)
	{
		printf("Could not open file.");//either we typed it wrong or the file is fucked
	}

	else
	{
		for(i=0;i<13;i++)//Intialize the array
		{
			for(j=0;j<4;j++)
			{//Initialize the array and make sure it stops at the end of the file
				if (fscanf(inCards,"%d",&Deck[i][j])==EOF)//If the file reaches the end, it'll mark an error.
				{
					printf("error");
				}
			}
		}
	}

	//FIND WAY TO RANDOMIZE THE DECK INTO TWO PLAYER HANDS

	fclose(inCards);
 
 
 
	printf("******\n* *\n* %d *\n* *\n******");//Wartime Display 
	return(0);
}


Search for a 'rand' function on this site
Or...try a Fisher-Yates shuffle:
http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

Then take the first half and make that the first player's hand and make the second half the second player's hand.
Last edited on
Topic archived. No new replies allowed.