Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 16th, 2010 | Syntax: C++ | Size: 6.21 KB | Hits: 96 | Expires: Never
Copy text to clipboard
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. typedef string::size_type str_sz;
  11. typedef vector<string>::size_type vec_str_sz;
  12. typedef vector<int>::size_type vec_int_sz;
  13.  
  14.  
  15. string to_upper(string in)
  16. {
  17.         for(str_sz i = 0; i < in.size(); ++i)
  18.                 in[i] = toupper(in[i]);
  19.  
  20.         return in;
  21. }
  22.  
  23.  
  24. bool valid_word(string word, string hand)
  25. {
  26.         word = to_upper(word);
  27.         hand = to_upper(hand);
  28.         bool result = true;
  29.         str_sz index;
  30.         str_sz word_size = word.size();
  31.  
  32.         for(str_sz i = 0; (i < word_size) && (result != false); ++i)
  33.         {
  34.                 index = hand.find(word[i]);
  35.  
  36.                 if(index == string::npos)
  37.                         result = false;
  38.                 else
  39.                         hand.erase(index, 1);
  40.         }
  41.  
  42.         return result;
  43. }
  44.  
  45. bool valid_char(string word)
  46. {
  47.         word = to_upper(word);
  48.         str_sz word_size = word.size();
  49.         char letter;
  50.         bool result = true;
  51.  
  52.         for(str_sz i = 0; i < word_size; ++i)
  53.         {
  54.                 letter = word[i];
  55.                 if((letter < 'A') || (letter > 'Z'))
  56.                 {
  57.                         result = false;
  58.                         break;
  59.                 }
  60.         }
  61.  
  62.         return result;
  63. }
  64.  
  65.  
  66. bool in_dictionary(string word, vector<string> dictionary)
  67. {
  68.         word = to_upper(word);
  69.         bool result = false;
  70.         vec_str_sz dic_size = dictionary.size();
  71.  
  72.         for(vec_str_sz i = 0; (i < dic_size) && (result != true); ++i)
  73.         {
  74.                 if(valid_word(dictionary[i], word))
  75.                         result = true;
  76.         }
  77.  
  78.         return result;
  79. }
  80.  
  81.  
  82. string shuffle(string deck)
  83. {
  84.         srand(time(0));
  85.         str_sz deck_initial_size = deck.size();
  86.         string deck_shuffle;
  87.         str_sz rand_letter;
  88.  
  89.         for(str_sz i = 0; i < deck_initial_size; ++i)
  90.         {
  91.                 rand_letter = rand() % deck.size();
  92.                 deck_shuffle += deck[rand_letter];
  93.                 deck.erase(rand_letter, 1);
  94.         }
  95.  
  96.         return deck_shuffle;
  97. }
  98.  
  99.  
  100. string extract_chars(int nChars, string& deck)
  101. {
  102.         string deck_result = deck.substr(0, nChars);
  103.  
  104.         return deck_result;
  105. }
  106.  
  107. void erase_word_on_hand(string word, string& hand)
  108. {
  109.         str_sz word_size = word.size();
  110.         str_sz index_hand;
  111.  
  112.         for(str_sz i = 0; i < word_size; ++i)
  113.         {
  114.                 index_hand = hand.find(word[i]);
  115.                 hand.erase(index_hand, 1);
  116.         }
  117. }
  118.  
  119.  
  120. string fill_hand(int nHand, string& hand, string& deck)
  121. {
  122.         int nFill = nHand - hand.size();
  123.         hand += deck.substr(0, nFill);
  124.         deck.erase(0, nFill);
  125.  
  126.         return hand;
  127. }
  128.  
  129.  
  130. int word_score(string word, vector<int> scores)
  131. {
  132.         word = to_upper(word);
  133.         char letter;
  134.         int total_score = 0;
  135.         str_sz word_size = word.size();
  136.  
  137.         for(vec_int_sz i = 0; i < word_size; ++i)
  138.         {
  139.                 letter = word[i];
  140.                 total_score += scores[letter - 'A'];
  141.         }
  142.  
  143.         return total_score;
  144. }
  145.  
  146.  
  147. void show_file(string filename)
  148. {
  149.         ifstream file (filename.c_str());
  150.  
  151.         if(file.is_open())
  152.         {
  153.                 string line;
  154.  
  155.                 while(! file.eof())
  156.                 {
  157.                         getline(file, line);
  158.                         cout << line << endl;
  159.                 }
  160.  
  161.                 file.close();
  162.         }
  163. }
  164.  
  165.  
  166. vector<string> read_dictionary(string filename)
  167. {
  168.         vector<string> dic;
  169.         ifstream file;
  170.         file.open(filename.c_str());
  171.         if (file.is_open())
  172.         {
  173.                 int nWords;
  174.                 string word;
  175.                 file >> nWords;
  176.                 for (int i = 0; i < nWords; i++)
  177.                 {
  178.                         file >> word;
  179.                         word = to_upper(word);
  180.                         dic.push_back(word);
  181.                 }
  182.                 file.close();
  183.         }
  184.         return dic;
  185. }
  186.  
  187. void read_letters_score (string filename, string& deck, vector<int>& scores)
  188. {
  189.         char letter;
  190.         int quantity;
  191.         int score;
  192.         ifstream file;
  193.         file.open(filename.c_str());
  194.         if(file.is_open())
  195.         {
  196.                 while(! file.eof())
  197.                 {
  198.  
  199.                         file >> letter >> quantity >> score;
  200.  
  201.                         scores.push_back(score);
  202.                         deck += string(quantity, letter);
  203.                 }
  204.                 file.close();
  205.         }
  206. }
  207.  
  208.  
  209.  
  210.  
  211. #ifndef SCRABBLE_FUNC_H_
  212. #define SCRABBLE_FUNC_H_
  213.  
  214. #include <string>
  215. #include <vector>
  216.  
  217.  
  218. std::string to_upper(std::string);
  219. bool valid_word(std::string, std::string);
  220. bool valid_char(std::string);
  221. bool in_dictionary(std::string, std::vector<std::string>);
  222. std::string shuffle(std::string);
  223. std::string extract_chars(int, std::string&);
  224. void erase_word_on_hand(std::string, std::string&);
  225. std::string fill_hand(int, std::string&, std::string&);
  226. int word_score(std::string, std::vector<int>);
  227. void show_file(std::string);
  228. std::vector<std::string> read_dictionary(std::string);
  229. void read_letters_score(std::string, std::string&, std::vector<int>& scores);
  230.  
  231.  
  232. #endif /*SCRABBLE_FUNC_H_*/
  233.  
  234.  
  235.  
  236.  
  237. #include "scrabble_func.h"
  238. #include <iostream>
  239. #include <string>
  240. #include <vector>
  241. #include <cstdlib>
  242.  
  243. using namespace std;
  244.  
  245. const string DICTIONARY_FILENAME = "dicionario.txt";
  246. const string ALPHABET_FILENAME = "alfabeto.txt";
  247.  
  248. int main()
  249. {
  250.         string deck;
  251.         string hand;
  252.         string word;
  253.         int score_word;
  254.         int total_score = 0;
  255.         int menu_option;
  256.         vector<int> scores;
  257.         vector<string> dictionary;
  258.  
  259.         dictionary = read_dictionary(DICTIONARY_FILENAME);
  260.         read_letters_score(ALPHABET_FILENAME, deck, scores);
  261.         deck = shuffle(deck);
  262.  
  263.         cout << "Bem-vindo ao SCRABBLE" << endl << endl;
  264.         cout << "Quantas letras pretende ter na mao? ";
  265.  
  266.         int n_letters;
  267.         cin >> n_letters;
  268.  
  269.         fill_hand(n_letters, hand, deck);
  270.  
  271.         while(true)
  272.         {
  273.                 if(! in_dictionary(hand, dictionary))
  274.                 {
  275.                         cout << "Nao existem mais palavras possiveis..." << endl;
  276.                         break;
  277.                 }
  278.  
  279.                 cout << "PONTUACAO: " << total_score << endl;
  280.                 cout << "LETRAS NA MAO: " << hand << endl << endl;
  281.  
  282.                 cout << "MENU" << endl;
  283.                 cout << "1 - Escolher Palavra" << endl
  284.                                 << "2 - Terminar o Jogo" << endl << endl;
  285.  
  286.                 cout << "OPCAO: ";
  287.                 cin >> menu_option;
  288.  
  289.                 switch(menu_option)
  290.                 {
  291.                 case 1:
  292.                         cout << "PALAVRA: ";
  293.                         cin >> word;
  294.                         word = to_upper(word);
  295.                         cout << endl;
  296.  
  297.                         if(! valid_char(word))
  298.                         {
  299.                                 cout << "Uso de caracteres invalidos" << endl << endl;
  300.                                 break;
  301.                         }
  302.  
  303.                         if(! in_dictionary(word, dictionary))
  304.                         {
  305.                                 cout << "Palavra inexistente" << endl << endl;
  306.                                 break;
  307.                         }
  308.  
  309.                         if(! valid_word(word, hand))
  310.                         {
  311.                                 cout << "Palavra nao pode ser formada com as letras da mao..." << endl << endl;
  312.                                 break;
  313.                         }
  314.  
  315.                         score_word = word_score(word, scores);
  316.  
  317.                         cout << "A pontuacao total de " << word << " é " << score_word
  318.                                         << endl << endl;
  319.  
  320.                         total_score += score_word;
  321.  
  322.                         erase_word_on_hand(word, hand);
  323.                         fill_hand(n_letters, hand, deck);
  324.  
  325.                         break;
  326.  
  327.                 case 2:
  328.                         cout << "Terminou o jogo...";
  329.                         break;
  330.  
  331.                 default:
  332.                                 cout << endl << "Opcao invalida. Tentar novamente..." << endl << endl;
  333.                                 break;
  334.                 }
  335.  
  336.                 if(menu_option == 2)
  337.                         break;
  338.         }
  339.  
  340.         return 0;
  341. }