Is this a good way to do it

I wrote a code to shuffle any number of poker decks. Is this a good way to do it.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<fstream>
using namespace std;
int main()
{
	ifstream in;
	ofstream out;
	srand(time(0));
	out.open("deck.txt");
	out << "";
	out.close();
	string type[4] = { "leaf", "heart", "diamond", "spades" };
	string cards[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
	int record = 0;
	int x, y;
	string a, b;
	while (record < 52)
	{
		int z = 0;
		x = rand() % 4;
		y = rand() % 13;
		out.open("current.txt");
		out << cards[y] << " of " << type[x];
		out.close();
		in.open("current.txt");
		getline(in, a);
		in.close();
		in.open("deck.txt");
		for (int i = 0; i < 52; i++)
		{
			getline(in, b);
			if (a == b)
			{
				z = 1;
			}
			else
			{
				if (z != 1)
				{
					z = 0;
				}
			}
		}
		in.close();
		out.open("deck.txt", ios::app);
		if (z == 0)
		{
			out << a << endl;
			record = record + 1;
		}
		out.close();
	}
	
	cout << "Enter the number of decks you want to shuffle";
	int number;
	cin >> number;
	string list[52];
	in.open("deck.txt");
	for (int i = 0; i < 52; i++)
	{
		getline(in, list[i]);
	}
	in.close();
	out.open("stack.txt");
	out << "";
	out.close();
	int d,e;
	int f = 52;
	record = 0;
	while (record < 52 * number)
	{
		d = 0;
		record = record + 1;
		e = rand() % f;
		out.open("stack.txt", ios::app);
		out << list[e] << endl;
		out.close();
		cout << record << "\t" << list[e] << endl;
		in.open("stack.txt");
		for (int i = 0; i < record; i++)
		{
			getline(in, a);
			if (list[e] == a)
			{
				d = d + 1;

			}
		}
		in.close();
		if (d == number)
		{
			list[e] == "";
			out.open("deck.txt");
			for (int i = 0; i < f; i++)
			{
				if (i!=e)
				{
					out << list[i] << endl;
				}
			}
			out.close();
			f = f - 1;
			in.open("deck.txt");
			for (int i = 0; i < f;i++)
			{
				getline(in, list[i]);
			}
			in.close();
		}
	}
	system("pause");
}


I wrote a shorter code too but it worked too slow if the number of decks was really big like 100. This is the shorter code.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<fstream>
using namespace std;
int main()
{
	ifstream in;
	ofstream out;
	srand(time(0));
	out.open("deck.txt");
	out << "";
	out.close();
	string type[4] = { "leaf", "heart", "diamond", "spades" };
	string cards[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
	int record=0;
	int x, y;
	int number;
	cout << "Enter the number of decks you want to shuffle";
	cin >> number;
	string a, b;
	while (record < 52*number)
	{
		int z = 0;
		x = rand() % 4;
		y = rand() % 13;
		out.open("current.txt");
		out << cards[y] << " of " << type[x];
		out.close();
		in.open("current.txt");
		getline(in, a);
		in.close();
		in.open("deck.txt");
		for (int i = 0; i < 52*number; i++)
		{
			getline(in, b);
			if (a == b)
			{
				z = z + 1;
			}
			else
			{
				if (z == 0)
				{
					z = 0;
				}
			}
		}
		in.close();
		out.open("deck.txt",ios::app);
		if (z < number)
		{
			out << a<<endl;
			record = record + 1;
			cout << record << "\t" << a<<endl;
		}
		out.close();
	}
	system("pause");
}
Last edited on
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
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <algorithm>

int main()
{
    std::srand( std::time(nullptr) ) ;

    const std::string type[4] = { "leaf", "heart", "diamond", "spades" };
    const std::string cards[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
	                                  "10", "Jack", "Queen", "King" };
    const std::size_t NCARDS = 52 ;
    const char* const file_name = "deck.txt" ;

    // create the deck of cards. deck should contain 52 distinct cards
    // 13 distinct cards of each type
    {
        std::ofstream file(file_name) ;
        // http://www.stroustrup.com/C++11FAQ.html#for
        for( const std::string& t : type ) // fpr each type
            for( const std::string& c : cards ) // for each card
                file << c << " of " << t << '\n' ;
    }

    // shuffle the deck of carfs
    {
        std::string deck[NCARDS] ; // array to hold the cards in the deck

        // read the cards from the file into the array
        {
            std::ifstream file(file_name) ;
            for( std::size_t i = 0 ; i < NCARDS ; ++i ) std::getline( file, deck[i] ) 
        }

        // shuffle the deck (library)
        // http://www.cplusplus.com/reference/algorithm/random_shuffle/
        std::random_shuffle( deck, deck+NCARDS ) ;

        // or: 
        // shuffle the deck (hand-coded fisher-yates shuffle)
        // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
        for( auto i = NCARDS-1 ; i > 0 ; --i )
        {
            auto rand_pos = std::rand() % (i+1) ; // pich a random position <= i
            std::swap( deck[i], deck[rand_pos] ) ; // swap with the card in that position
        }

        // print out the shuffled deck
        for( const std::string& c : deck ) std::cout << c << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/a89cf2d6c442be18
Topic archived. No new replies allowed.