How to make random letters with a vector

Hi, everyone. Thanks in advance for helping. I have read the related topics to mine, but none of them made sense to me. I am supposed to make a scrabble game and randomize 10 of the letters. Here's my code:
class Spinner
Spinner::Spinner(string things[], int numThings[], int n)
{
currentPosition = 0;
totalPositions = n;
for (int i = 0; i < n; i++)
{
labels.push_back(things[i]);
values.push_back(numThings[i]);
}
}
void Spinner::Spin()
{
currentPosition = RandomInt(0, values.size() - 1);
}
vector<Spinner> wordList;
int scrabblePoints = 0;
string sLabels[26] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int sValues[26] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };
vector<string> sLabels;
for(int i=0; i<26; i++)///similar to what I found in related topic
{
sLabels.push_back('A' + i);
}
Spinner wordSpinner(sLabels, sValues, 26);
for(int i = 0; i < 10; i++)///makes 10 wordSpinners
{
wordList.push_back(wordSpinner);
}
for(int i = 0; i < 10; i++)///Spin and print wordList 10 times but
{ ///doesn't do random letters
wordList[i].Spin();
cout << wordList[i].CurrentLabel();
}
We could just randomize all of the scrabble letters. Here's something I whipped up. It generates a queue (deque) of all scrabble tiles, shuffles them, then the players puts those tiles into his hand. Then I print them out for you.

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
#include <deque>
#include <iostream>

struct Letter
{
    char letter;
    int points;

    Letter( char l, int p ) : letter(l), points(p) {}
};

typedef std::deque<Letter> Letters;

class DrawBag
{
    Letters m_set;
public:
    DrawBag()
    {
        Reset();
    }

    void Reset()
    {
        m_set.clear();
        AddLetter('E', 1, 12);        AddLetter('A', 1, 9 );
        AddLetter('I', 1, 9 );        AddLetter('O', 1, 8 );
        AddLetter('N', 1, 6 );        AddLetter('R', 1, 6 );
        AddLetter('T', 1, 6 );        AddLetter('L', 1, 4 );
        AddLetter('S', 1, 4 );        AddLetter('U', 1, 4 );
        AddLetter('D', 2, 4 );        AddLetter('G', 2, 3 );
        AddLetter('B', 3, 2 );        AddLetter('C', 3, 2 );
        AddLetter('M', 3, 2 );        AddLetter('P', 3, 2 );
        AddLetter('F', 4, 2 );        AddLetter('H', 4, 2 );
        AddLetter('V', 4, 2 );        AddLetter('W', 4, 2 );
        AddLetter('Y', 4, 2 );        AddLetter('K', 5, 1 );
        AddLetter('J', 8, 1 );        AddLetter('X', 8, 1 );
        AddLetter('Q',10, 1 );        AddLetter('Z',10, 1 );
        std::random_shuffle( m_set.begin(), m_set.end() );
    }

    Letter Draw()
    {
        Letter temp = m_set.front();
        m_set.pop_front();
        return temp;
    }

    Letters Draw(int number)
    {
        Letters temp;
        for (int i = 0; i < number; ++i)
            temp.push_back( Draw() );
        return temp;
    }

private:
    void AddLetter(char letter, char points, char frequency)
    {
        for (int i = 0; i < frequency; ++i)
            m_set.push_back( Letter( letter, points) );
    }
};

int main()
{
    srand(time(0));
    DrawBag d;
    Letters hand1 = d.Draw(7); // Draw 7 letters when starting the game
    Letters hand2 = d.Draw(7); // Draw 7 letters when starting the game

    // Two different ways of iterating:
    std::cout << "Player 1's hand:\n";
    for (Letters::iterator it = hand1.begin(); it != hand1.end(); ++it)
        std::cout << it->letter << ':' << it->points << std::endl;

    std::cout << "Player 2's hand:\n";
    for (int i = 0; i < hand2.size(); ++i)
        std::cout << hand2[i].letter << ':' << hand2[i].points << std::endl;
}
Player 1's hand:
O:1
Z:10
S:1
U:1
O:1
R:1
A:1
Player 2's hand:
T:1
G:2
D:2
Q:10
U:1
N:1
Y:4


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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>

std::vector<char> spin( std::string letters, std::size_t n = 10 )
{
    std::random_shuffle( letters.begin(), letters.end() ) ;
    if( n  > letters.size() ) n = letters.size() ;
    return std::vector<char>( letters.begin(), letters.begin() + n ) ;
}


int main()
{
    std::srand( std::time(nullptr) ) ;
    
    for( int i = 0 ; i < 4 ; ++i )
    {
        for( char c : spin( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ) std::cout << c ;
        std::cout << ' ' ;
    
        for( char c : spin( "AAABBCDDEEEFFGGHIIIIJKLLMMMNNNNPQRSSSTTTTUVUWXYZ" ) )
            std::cout << c ;
        std::cout << '\n' ;
    } 
}

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