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
|
#include <iostream>
#include <string>
#include <memory>
#include <algorithm>
#include <random>
int main()
{
const std::size_t CAP = 20;
std::string words[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado",
"Cracker", "Cake", "Chocolate", "Car", "Bus", "Bird", "Airplane", "House",
"Umbrella", "Cat", "Dog", "Mouse" };
std::cout << "How many words do you want to use? " ;
std::size_t nwords ;
std::cin >> nwords ;
if( nwords > CAP ) nwords = CAP ;
std::cout << "nwords == " << nwords << "\n\n" ;
const std::size_t array_sz = nwords*2 ;
// http://en.cppreference.com/w/cpp/memory/unique_ptr (2)
std::unique_ptr< std::string[] > game_words( new std::string[array_sz] ) ;
std::unique_ptr< bool[] > same( new bool[array_sz]{} ) ;
// http://en.cppreference.com/w/cpp/algorithm/copy
std::copy( words, words+nwords, game_words.get() ) ; // copy the first 'nwords' words
std::copy( words, words+nwords, game_words.get()+nwords ) ; // append the same set of words
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
// http://en.cppreference.com/w/cpp/numeric/random
std::shuffle(game_words.get(), game_words.get() + array_sz, std::mt19937( std::random_device{}() ) ) ;
for( std::size_t i = 0 ; i <array_sz ; ++i ) std::cout << i+1 << ". " << game_words[i] << '\n' ;
}
|