#include <string>
#include <vector>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
using namespace std;
typedef string::size_type str_sz;
typedef vector<string>::size_type vec_str_sz;
typedef vector<int>::size_type vec_int_sz;
string to_upper(string in)
{
for(str_sz i = 0; i < in.size(); ++i)
in[i] = toupper(in[i]);
return in;
}
bool valid_word(string word, string hand)
{
word = to_upper(word);
hand = to_upper(hand);
bool result = true;
str_sz index;
str_sz word_size = word.size();
for(str_sz i = 0; (i < word_size) && (result != false); ++i)
{
index = hand.find(word[i]);
if(index == string::npos)
result = false;
else
hand.erase(index, 1);
}
return result;
}
bool valid_char(string word)
{
word = to_upper(word);
str_sz word_size = word.size();
char letter;
bool result = true;
for(str_sz i = 0; i < word_size; ++i)
{
letter = word[i];
if((letter < 'A') || (letter > 'Z'))
{
result = false;
break;
}
}
return result;
}
bool in_dictionary(string word, vector<string> dictionary)
{
word = to_upper(word);
bool result = false;
vec_str_sz dic_size = dictionary.size();
for(vec_str_sz i = 0; (i < dic_size) && (result != true); ++i)
{
if(valid_word(dictionary[i], word))
result = true;
}
return result;
}
string shuffle(string deck)
{
srand(time(0));
str_sz deck_initial_size = deck.size();
string deck_shuffle;
str_sz rand_letter;
for(str_sz i = 0; i < deck_initial_size; ++i)
{
rand_letter = rand() % deck.size();
deck_shuffle += deck[rand_letter];
deck.erase(rand_letter, 1);
}
return deck_shuffle;
}
string extract_chars(int nChars, string& deck)
{
string deck_result = deck.substr(0, nChars);
return deck_result;
}
void erase_word_on_hand(string word, string& hand)
{
str_sz word_size = word.size();
str_sz index_hand;
for(str_sz i = 0; i < word_size; ++i)
{
index_hand = hand.find(word[i]);
hand.erase(index_hand, 1);
}
}
string fill_hand(int nHand, string& hand, string& deck)
{
int nFill = nHand - hand.size();
hand += deck.substr(0, nFill);
deck.erase(0, nFill);
return hand;
}
int word_score(string word, vector<int> scores)
{
word = to_upper(word);
char letter;
int total_score = 0;
str_sz word_size = word.size();
for(vec_int_sz i = 0; i < word_size; ++i)
{
letter = word[i];
total_score += scores[letter - 'A'];
}
return total_score;
}
void show_file(string filename)
{
ifstream file (filename.c_str());
if(file.is_open())
{
string line;
while(! file.eof())
{
getline(file, line);
cout << line << endl;
}
file.close();
}
}
vector<string> read_dictionary(string filename)
{
vector<string> dic;
ifstream file;
file.open(filename.c_str());
if (file.is_open())
{
int nWords;
string word;
file >> nWords;
for (int i = 0; i < nWords; i++)
{
file >> word;
word = to_upper(word);
dic.push_back(word);
}
file.close();
}
return dic;
}
void read_letters_score (string filename, string& deck, vector<int>& scores)
{
char letter;
int quantity;
int score;
ifstream file;
file.open(filename.c_str());
if(file.is_open())
{
while(! file.eof())
{
file >> letter >> quantity >> score;
scores.push_back(score);
deck += string(quantity, letter);
}
file.close();
}
}
#ifndef SCRABBLE_FUNC_H_
#define SCRABBLE_FUNC_H_
#include <string>
#include <vector>
std::string to_upper(std::string);
bool valid_word(std::string, std::string);
bool valid_char(std::string);
bool in_dictionary(std::string, std::vector<std::string>);
std::string shuffle(std::string);
std::string extract_chars(int, std::string&);
void erase_word_on_hand(std::string, std::string&);
std::string fill_hand(int, std::string&, std::string&);
int word_score(std::string, std::vector<int>);
void show_file(std::string);
std::vector<std::string> read_dictionary(std::string);
void read_letters_score(std::string, std::string&, std::vector<int>& scores);
#endif /*SCRABBLE_FUNC_H_*/
#include "scrabble_func.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
const string DICTIONARY_FILENAME = "dicionario.txt";
const string ALPHABET_FILENAME = "alfabeto.txt";
int main()
{
string deck;
string hand;
string word;
int score_word;
int total_score = 0;
int menu_option;
vector<int> scores;
vector<string> dictionary;
dictionary = read_dictionary(DICTIONARY_FILENAME);
read_letters_score(ALPHABET_FILENAME, deck, scores);
deck = shuffle(deck);
cout << "Bem-vindo ao SCRABBLE" << endl << endl;
cout << "Quantas letras pretende ter na mao? ";
int n_letters;
cin >> n_letters;
fill_hand(n_letters, hand, deck);
while(true)
{
if(! in_dictionary(hand, dictionary))
{
cout << "Nao existem mais palavras possiveis..." << endl;
break;
}
cout << "PONTUACAO: " << total_score << endl;
cout << "LETRAS NA MAO: " << hand << endl << endl;
cout << "MENU" << endl;
cout << "1 - Escolher Palavra" << endl
<< "2 - Terminar o Jogo" << endl << endl;
cout << "OPCAO: ";
cin >> menu_option;
switch(menu_option)
{
case 1:
cout << "PALAVRA: ";
cin >> word;
word = to_upper(word);
cout << endl;
if(! valid_char(word))
{
cout << "Uso de caracteres invalidos" << endl << endl;
break;
}
if(! in_dictionary(word, dictionary))
{
cout << "Palavra inexistente" << endl << endl;
break;
}
if(! valid_word(word, hand))
{
cout << "Palavra nao pode ser formada com as letras da mao..." << endl << endl;
break;
}
score_word = word_score(word, scores);
cout << "A pontuacao total de " << word << " é " << score_word
<< endl << endl;
total_score += score_word;
erase_word_on_hand(word, hand);
fill_hand(n_letters, hand, deck);
break;
case 2:
cout << "Terminou o jogo...";
break;
default:
cout << endl << "Opcao invalida. Tentar novamente..." << endl << endl;
break;
}
if(menu_option == 2)
break;
}
return 0;
}