Hi please I have a scrabble project in c++ and I want to solve it . I want to ma
ID: 3874969 • Letter: H
Question
Hi please I have a scrabble project in c++ and I want to solve it . I want to make a scrabble game in c++ . I want to use iostream , stdafx.h and cstring libraries only . there are two players , they want to make words from these caracters :ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ RSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRST these 2 players have to make words from these caracters till these caracters done . at the end all the words that the 2 players made by (cin) must be viewed on the screen by (cout ). every time a player enter a word, the characters of this word must be eliminated from the list of characters and make sure to calculate the score of each player according to :
0 point - blank tile
1 point - A, E , I, L, N, O, R, S, T, U
2 point - D, G
3 point - B, C, M, P
4 point - F, H, V, W, Y
5 point - K
8 point - J, K
10 point - Q, Z
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int player_1_score, player_2_score;
string baseCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string baseCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRST"; //Base characters from which player has to make word
//This function will remove characters one by one from base characters string
//If character not available in string then 0 points will be added in score
int RemoveChar(char c)
{
size_t size = baseCharacters.length(); //Size of base characters string
for (size_t i = 0; i < size; i++)
{
char currentChar = baseCharacters[i];
if (currentChar == toupper(c))
{
if(i == 0)
baseCharacters = baseCharacters.substr(i+1, size);
else
baseCharacters = (baseCharacters.substr(0, i) + baseCharacters.substr(i+1, size));
return 1;
}
}
cout<<" No "<<c<<" available.....0 point for this character";
return 0;
}
//Return points based on characters
int getValueOfLetter(char ch)
{
int value =0;
if(RemoveChar(ch)) //If character available in string return 1 else 0
{
switch(ch)
{
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'L': case 'l':
case 'N': case 'n':
case 'O': case 'o':
case 'R': case 'r':
case 'S': case 's':
case 'T': case 't':
case 'U': case 'u':
value += 1; break;
case 'D': case 'd':
case 'G': case 'g':
value += 2; break;
case 'B': case 'b':
case 'C': case 'c':
case 'M': case 'm':
case 'P': case 'p':
value += 3; break;
case 'F': case 'f':
case 'H': case 'h':
case 'V': case 'v':
case 'W': case 'w':
case 'Y': case 'y':
value += 4; break;
case 'K': case 'k':
value += 5; break;
case 'J': case 'j':
case 'X': case 'x':
value += 8; break;
case 'Q': case 'q':
case 'Z': case 'z':
value += 10; break;
}
}
return value;
}
//This function will return total score for current word
int getScore(string word)
{
int total = 0;
int length = word.length();
for(int i = 0; i < length; i++)
{
total += getValueOfLetter(word[i]);
}
return total;
}
int main()
{
string player_1_word, player_2_word; //Hold word typed by playes
//int flag_1, flag_2;
player_1_word = "";
player_2_word = "";
//flag_1 = flag_2 = 0;
while(true && baseCharacters.length() > 0) //Continuous loop to read characters from players
{
char word[80];
cout<<" Available Characters : "<<baseCharacters;
cout << " player_1 Enter a word : ";
cin.getline(word, 80);
if(word[0] != ' ')
player_1_score += getScore(word);//Adding player 1 score
player_1_word = player_1_word + string(word) + ", "; //storing player 1 words
if(baseCharacters.length() <= 0)
break;
cout<<" Available Characters : "<<baseCharacters;
cout << " player_2 Enter a word : ";
cin.getline(word, 80);
if(word[0] != ' ')
player_2_score += getScore(word); //Adding player 2 score
player_2_word = player_2_word + string(word) + ", "; //storing player 2 words
}
cout << " Words typed by player 1 are : " << player_1_word<< " ";
cout << "The score for player 1 is : " << player_1_score<< " ";
cout << "Words typed by player 2 are : " << player_2_word<< " ";
cout << "The score for player 2 is : " << player_2_score<< " ";
return 0;
}
-----------------------------------------------------------------------------------------------
OUTPUT :
Available Characters : ABCDEFGHIJKLMNOPQRSTUVWXYZ
player_1 Enter a word : TEST
No T available.....0 point for this character
Available Characters : ABCDFGHIJKLMNOPQRUVWXYZ
player_2 Enter a word : DEBUG
No E available.....0 point for this character
Available Characters : ACFHIJKLMNOPQRVWXYZ
player_1 Enter a word : CHEGG
No E available.....0 point for this character
No G available.....0 point for this character
No G available.....0 point for this character
Available Characters : AFIJKLMNOPQRVWXYZ
player_2 Enter a word : XYZ
Available Characters : AFIJKLMNOPQRVW
player_1 Enter a word : JBL
No B available.....0 point for this character
Available Characters : AFIKMNOPQRVW
player_2 Enter a word : OMNI
Available Characters : AFKPQRVW
player_1 Enter a word : PQR
Available Characters : AFKVW
player_2 Enter a word : KVW
Available Characters : AF
player_1 Enter a word : AF
Words typed by player 1 are : TEST, CHEGG, JBL, PQR, AF,
The score for player 1 is : 38
Words typed by player 2 are : DEBUG, XYZ, OMNI, KVW,
The score for player 2 is : 49
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.