**C++ Synopsis: Type in a word and get a point value. In both Words With Friends
ID: 666585 • Letter: #
Question
**C++
Synopsis: Type in a word and get a point value.
In both Words With Friends and the scrabble game, a individual letters in a word are given specific values.
For this lab, calculate the value of a word with the following specific rules:
Each letter has the following values:
A, E, I, M, Q, U, Y are worth 1 point
B, F, J, N, R, V, Z are worth 2 points
C, G, K, O, S, W are worth 3 points
D, H, L, P, T, X are worth 5 points.
Any word that contains two (or more) of the same letter in a row has it's value doubled. Some examples are 'asset' and 'little'. You can only double things once.
Your program should ask the user for a word (without spaces) and print it's value.
Ignore any special characters or numbers that are entered. [I won't include these in my test cases].
You HAVE to use my rules or you will receive 0 credit!
You also need to check to see if the word is in a dictionary. There's one at
http://www.csit.parkland.edu/~kurban/permanent/lists/
and it's called web2.txt
It contains one word per line and is already sorted. You can make your own smaller dictionary to test it if you like.
Examples:
asset = (1 + 3 + 3 + 1 + 5) * 2 = 26 (double letters)
little = (5 + 1 + 5 + 5 + 5 + 1) * 2 = 44 (double letters)
abacadabra = (1 + 2 + 1 + 3 + 1 + 5 + 1 + 2 + 2 + 1) = 19 (NO double letters)
football = (2 + 3 + 3 + 5 + 2 + 1 + 5 + 5) * 2 = 52 (double letters only double once)
Explanation / Answer
Check this :
#include <string>
#include <vector>
#include<algorithm>
#include <iostream>
using namespace std;
int main()
{
vector<string> letter;
for(int i = 0; i < 12;i++)
{
letter.push_back("E");
}
for(int i = 0; i < 9;i++)
{
letter.push_back("A");
letter.push_back("I");
}
for(int i = 0; i < 8;i++)
{
letter.push_back("O");
}
for(int i = 0; i < 6;i++)
{
letter.push_back("N");
letter.push_back("R");
letter.push_back("T");
}
for(int i = 0; i < 4;i++)
{
letter.push_back("L");
letter.push_back("S");
letter.push_back("U");
letter.push_back("D");
}
for(int i = 0; i < 3;i++)
{
letter.push_back("G");
}
for(int i = 0; i < 2;i++)
{
letter.push_back("B");
letter.push_back("C");
letter.push_back("M");
letter.push_back("P");
letter.push_back("F");
letter.push_back("H");
letter.push_back("V");
letter.push_back("W");
letter.push_back("Y");
}
letter.push_back("K");
letter.push_back("J");
letter.push_back("X");
letter.push_back("Q");
letter.push_back("Z");
random_shuffle( letter.begin(), letter.end() );// is once not enough?
size_t i = 0;
// display entire tile pool
for( i=0; i<letter.size(); ++i )
{
cout << letter[i] << " ";
if( (i+1)%20 == 0 )cout << endl;// 20 to a line
}
string p1tile[8];// the 2 players tile hands
string p2tile[8];
// fill hands
for( i=0; i<8; ++i )
if( !letter.empty() )
{
p1tile[i] = letter.back();
letter.pop_back();
}
for( i=0; i<8; ++i )
if( !letter.empty() )
{
p2tile[i] = letter.back();
letter.pop_back();
}
// show hands
cout << " hand1 = ";
for( i=0; i<8; ++i )
cout << p1tile[i] << " ";
cout << " hand2 = ";
for( i=0; i<8; ++i )
cout << p2tile[i] << " ";
cout << endl;
return 0;
}
Main function :
other functions :
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.